C#使用ComboBox,CheckBox和TextBox填充DataGrid

时间:2012-03-23 09:22:01

标签: c# winforms datagrid combobox populate

我有一个包含6列的DataGrid: 组合框;组合框;复选框;复选框;复选框;文本框

我正在尝试使用我保留在GridLine对象列表中的值来填充此DataGrid

    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; }
     (...)
    }

我写了一个函数,它设置DataGrid最后一行params并添加新行,但是它没有正常工作 - 我只在最后一行正确设置了ComboBoxes,其他任何东西:

        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();
    }

我不知道我做错了什么。

非常感谢

1 个答案:

答案 0 :(得分:0)

尝试创建要添加到datagridview的项目(GridLine)的绑定列表http://msdn.microsoft.com/en-us/library/ms132679.aspx。然后使用该列表分配datagrid的属性DataSource。

在此之前,您需要将设计器中的列添加到datagridview。

此处有更多信息:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

相关问题