I’ve got a DataGrid with 6 columns:
ComboBox;ComboBox;CheckBox;CheckBox;CheckBox;TextBox
I’m trying to populate this DataGrid with values I’m keeping in List of GridLine objects
public class GridLine
{
public string sColumnA { get; set; }
public bool bError { get; set; }
public string sColumnB { get; set; }
public bool bNullableB { get; set; }
public bool bCollateB { get; set; }
public bool bStaticB { get; set; }
public string sStaticB { get; set; }
(...)
}
I’ve wrote a function that is setting DataGrid last line params and add new line but it is not working correctly – I’m getting only ComboBoxes on last line properly set, anything else:
private void AddLine(GridLine gl)
{
DataGridViewComboBoxCell cellMs = (DataGridViewComboBoxCell)this.Rows[this.Rows.Count - 1].Cells[0];
DataGridViewComboBoxCell cellOra = (DataGridViewComboBoxCell)this.Rows[this.Rows.Count - 1].Cells[1];
DataGridViewCheckBoxCell cellNull = (DataGridViewCheckBoxCell)this.Rows[this.Rows.Count - 1].Cells[2];
DataGridViewCheckBoxCell cellColl = (DataGridViewCheckBoxCell)this.Rows[this.Rows.Count - 1].Cells[3];
DataGridViewCheckBoxCell cellStat = (DataGridViewCheckBoxCell)this.Rows[this.Rows.Count - 1].Cells[4];
DataGridViewTextBoxCell cellStatText = (DataGridViewTextBoxCell)this.Rows[this.Rows.Count - 1].Cells[5];
cellMs.Value = gl.sColumnA;
cellOra.Value = gl.sColumnB;
cellNull.Selected = gl.bNullableB;
cellColl.Selected = gl.bCollateB;
cellStat.Selected = gl.bStaticB;
cellStatText.Value = gl.sStaticB;
this.Rows.Add();
}
I have no idea what I’m doing wrong.
Many thanks
Try creating a binding list http://msdn.microsoft.com/en-us/library/ms132679.aspx of items (GridLine) you want to add to datagridview. Then assign datagrid’s property DataSource with that list.
Before you do that you will need to add columns in a designer to datagridview.
More info here: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx