C # Заполните DataGrid, имеющий ComboBox, CheckBox и TextBox - PullRequest
0 голосов
/ 23 марта 2012

У меня есть DataGrid с 6 столбцами: ComboBox; ComboBox; CheckBox; CheckBox; CheckBox; TextBox

Я пытаюсь заполнить этот DataGrid значениями, которые я храню в Списке объектов GridLine

    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 и добавляет новую строку, но она работает неправильно - я получаю только ComboBox на последней установленной строке, все остальное:

        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 голосов
/ 23 марта 2012

Попробуйте создать список связывания http://msdn.microsoft.com/en-us/library/ms132679.aspx элементов (GridLine), которые вы хотите добавить в представление данных.Затем присвойте свойству DataSrid свойство DataSource с этим списком.

Прежде чем сделать это, вам нужно добавить столбцы в конструкторе в datagridview.

Дополнительная информация здесь: http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.datasource.aspx

...