I have a DataGridView in a windows form. I defined 5 columns for this DataGridView dgv.
The datatable returned by a stored procedure is bound with this DataGridView.
DataTable table1 = returned by a stored procedure in C#;
dgv.DataSource = table1;
dgv.AutoGenerateColumns = false;
dgv = System.Windows.Forms.DataGridViewEditMode.EditProgrammatically;
Then in another button event. I want to rebind the DataGridView a data source.
DataTable dt1 = new DataTable();
Then
DataRow dr = dt1.NewRow();
dr["Interval"] = txtBox1.Text;
dr["Count"] = txtBox2.Text;
dr["AgeBegin"] = txtBox3.Text;
dr["AgeEnd"] = txtBox4.Text;
dr["Gender"] = txtBox5.Text;
dt1.Rows.Add(dr);
dgv.DataSource = dt1;
dt1 only have one row. Now dgv displays 10 columns. The first 5 columns are empty, the last 5 columns have real data.
Why? Thanks.
Datagridviews automatically add rows on you if there is no spot to put the data. Unless you tell it not to, which is not an option in the designer.
nevermind you have this already.
You shouldn’t have to but have you tried clearing the dgv before rebinding it, just to try to find the problem?
Are you predefining the columns in your datagridview? I would suggest leaving it blank to start and allow the Autogenerate columns, at least until you figure out the flaw