I cannot display the results sets from my query in the datagridview. When I step through the code, I can see the data in the data adapter table and the binding source, but it will not display in the grid? Can anyone tell me what I'm missing?
{
SqlConnection conn = new SqlConnection ("my conn string");
//open
conn.Open();
////pass the conn to command object
string query = "select * from main (nolock) where platter = 'first' and uk_5 in (" + List + ")";
//creat开发者_StackOverflowe adapter to get data
SqlDataAdapter dAdapter = new SqlDataAdapter(query, conn);
// Get data set instance
DataSet dTable = new DataSet();
// Fill data set
dAdapter.Fill(dTable);
//binding source to the data set
BindingSource bSource = new BindingSource();
bSource.DataSource = dTable;
//displaying in datagridview
DataGridView dgView = new DataGridView();
dgView.AutoGenerateColumns = true;
dgView.DataSource = bSource;
//close
conn.Close();
}
You're not adding the DataGridView that you're programmatically creating to any form, so it will not be displayed on your form . Add this code after you bind your BindingSource to your DataGridView:
Controls.Add(dgView);
Note that a DataGridView added to a form in this manner will be positioned and sized using defaults you probably don't want.
You can either create the DataGridView in the form designer or set these values programmatically: (example: dgView.Location = new System.Drawing.Point(100, 100); )
精彩评论