如何检测DataGridViewComboBoxColumn是否存在

时间:2013-06-17 09:24:06

标签: c# datagridview

我有一个DataGridView,最后一列被添加为DataGridViewComboBoxColumn

问题是,我可以通过

确定“Notes”列是否存在
if(grid.Columns.Contains("Notes")){
}

但是,如果我使用DataGridViewComboBoxColumn的标题尝试相同的方法,它不起作用。任何人都可以建议,为什么会如此?

这是创建网格并添加DataGridViewComboBoxColumn的函数,Mikhail Tregubov在底部建议代码确定列是否已经存在...它不会在那里拾取它,即使此函数是以前跑,它就在那里!

private void _buildDataTable()
        {

            // create a data table object
            DataTable dataTable = new DataTable();

            // add columns
            dataTable.Columns.Add("Channel");
            dataTable.Columns.Add("Date");
            dataTable.Columns.Add("First Name");
            dataTable.Columns.Add("Last Name");
            dataTable.Columns.Add("DOB");
            dataTable.Columns.Add("NI Number");
            dataTable.Columns.Add("Notes");
            dataTable.Columns.Add("Agg. Max[%]");
            dataTable.Columns.Add("Agg. Inclination[%/min]");
            dataTable.Columns.Add("Agg. LagPhase[s]");

            // CONCENTRATION COL
            // TEST TYPE DROP DOWN COL

            // add initial rows row to data grid for 4 channels - only 4 possible
            DataRow dataRow1 = dataTable.Rows.Add();
            DataRow dataRow2 = dataTable.Rows.Add();
            DataRow dataRow3 = dataTable.Rows.Add();
            DataRow dataRow4 = dataTable.Rows.Add();

            // Add initial data to the new rows
            dataRow1[0] = "1"; // channel number
            dataRow1[1] = DateTime.Now.ToString("d/M/yyyy");
            dataRow1[7] = "0";
            dataRow1[8] = "0";
            dataRow1[9] = "0";
            dataRow2[0] = "2"; // channel number
            dataRow2[1] = DateTime.Now.ToString("d/M/yyyy");
            dataRow2[7] = "0";
            dataRow2[8] = "0";
            dataRow2[9] = "0";
            dataRow3[0] = "3"; // channel number
            dataRow3[1] = DateTime.Now.ToString("d/M/yyyy");
            dataRow3[7] = "0";
            dataRow3[8] = "0";
            dataRow3[9] = "0";
            dataRow4[0] = "4"; // channel number
            dataRow4[1] = DateTime.Now.ToString("d/M/yyyy");
            dataRow4[7] = "0";
            dataRow4[8] = "0";
            dataRow4[9] = "0";

            // Add drop downs to the table for test type
            DataGridViewComboBoxColumn testTypeCol = new DataGridViewComboBoxColumn();

            // create a new datatable for the test type
            DataTable dataTable2 = new DataTable();

            // Add the column
            dataTable2.Columns.Add(new DataColumn("Value", typeof(string)));

            // Add drop down rows
            dataTable2.Rows.Add("A23187");
            dataTable2.Rows.Add("ADP");
            dataTable2.Rows.Add("Arachidonic Acid");
            dataTable2.Rows.Add("Collagen");
            dataTable2.Rows.Add("CRP");
            dataTable2.Rows.Add("Epinephrine");
            dataTable2.Rows.Add("PMA");
            dataTable2.Rows.Add("Ristocetin");
            dataTable2.Rows.Add("TRAP6");
            dataTable2.Rows.Add("U46619");
            dataTable2.Rows.Add("γ-Thrombin");

            // set the columns data source
            testTypeCol.DataSource = dataTable2;

            // set properties
            testTypeCol.ValueMember = "Value";
            testTypeCol.HeaderText = "Aggregation Test Type";

            // set the data in the gridview
            this._dataGrid.DataSource = dataTable;

            if (this._dataGrid.Columns.Contains("Aggregation Test Type") && this._dataGrid.Columns["Aggregation Test Type"].GetType() == typeof(DataGridViewComboBoxColumn))
            {
                MessageBox.Show("ALREADY");
            }
            else
            {
                // add column to the datagridview
                this._dataGrid.Columns.Add(testTypeCol);
            }

        }

1 个答案:

答案 0 :(得分:0)

如果您的问题是如何确定列类型是否为DataGridViewComboBoxColumn,那么请尝试使用typeof。 例如:

        if(grid.Columns.Contains("Notes") && grid.Columns["Notes"].GetType() == typeof(DataGridViewComboBoxColumn))
        {
            //do something
        }

如果您的问题是包含的方法无法正常使用添加到grid.Columns集合中的DataGridViewComboBoxColumn,那么请添加更多代码,例如如何创建DataGridViewComboBoxColumn的新实例并将其添加到网格中。