0

I want to display in datagridview a specified column value. Example: from table user_account I want to display and search by textbox data from column account in datagridview

Here is the code so far:

private void button3_Click(object sender, EventArgs e)
{
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + textBox1.Text + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT * FROM user_account";
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "account");
        connection.Close();
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "account";
    }

2 Answers 2

1
private void button3_Click(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + textBox1.Text + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT account FROM user_account";
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "account");
        connection.Close();
        dataGridView1.DataSource = ds;

    }
Sign up to request clarification or add additional context in comments.

15 Comments

give break points and check step by step..then you will know where you are lacking
With dataGridView1.DataMember = "account"; shows all table but not what i want.
if you want to filter values with textbox.text,then use SELECT * FROM user_account where account=" + textBox.Text + ";...this only will filter..But you have done code to diaply all..
for textbox i got answay down. Beside I can't give break piont because it returns no error.
use this...SELECT * FROM user_account where account=" + textBox.Text + ";
|
0

Solution:

        private void button3_Click(object sender, EventArgs e)
    {
        string connectionString = @"Data Source=" + textBox4.Text + ";" + "Initial Catalog=" + "lin2db" + ";" + "User ID=" + textBox2.Text + ";" + "Password=" + textBox3.Text;
        string sql = "SELECT account ,pay_stat FROM user_account where account='" + textBox7.Text + "' ";          
        SqlConnection connection = new SqlConnection(connectionString) ;
        SqlDataAdapter dataadapter = new SqlDataAdapter(sql, connection);
        DataSet ds = new DataSet();
        connection.Open();
        dataadapter.Fill(ds, "pay_stat");
        connection.Close();
        dataGridView1.DataSource = ds;
        dataGridView1.DataMember = "pay_stat";


    }

Thank you for help.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.