0

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

问题是,我可以通过执行来确定“注释”列是否存在

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

        }
4

1 回答 1

0

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

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

如果您的问题是方法 Contains 不能与添加到 grid.Columns 集合的 DataGridViewComboBoxColumn 一起正常工作,那么请添加更多代码,例如如何创建 DataGridViewComboBoxColumn 的新实例并将其添加到网格中。

于 2013-06-17T09:39:04.090 回答