简体   繁体   English

如何检测是否存在DataGridViewComboBoxColumn

[英]How do I Detect If a DataGridViewComboBoxColumn exists

I have a DataGridView with the last column being added on as DataGridViewComboBoxColumn 我有一个DataGridView,最后一列被添加为DataGridViewComboBoxColumn

The problem is, i can determine if the column "Notes" exists by doing 问题是,我可以通过执行以下操作确定是否存在“注释”列

if(grid.Columns.Contains("Notes")){
}

But if i try this same method with the heading of the DataGridViewComboBoxColumn it doesn't work. 但是,如果我尝试使用DataGridViewComboBoxColumn标题的相同方法,则无法正常工作。 Can anyone suggest, why it is so? 谁能建议,为什么会这样呢?

Here is the function that creates the grid and adds the DataGridViewComboBoxColumn, with Mikhail Tregubov suggested code at the bottom to determine if the column exists already...it doesn't pick it up as being there, even though this function is run previously and it is there! 这是创建网格并添加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);
            }

        }

If your question is how to determine if the column type is DataGridViewComboBoxColumn then try use a typeof. 如果您的问题是如何确定列类型是否为DataGridViewComboBoxColumn,请尝试使用typeof。 Ex: 例如:

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

If your problem is that the method Contains doesn't work properly with DataGridViewComboBoxColumn added to grid.Columns collection then please add more code for example of how you creating new instance of DataGridViewComboBoxColumn and add it to your grid. 如果您的问题是“包含”方法无法与添加到grid.Columns集合的DataGridViewComboBoxColumn一起正常工作,请添加更多代码,例如有关如何创建DataGridViewComboBoxColumn的新实例并将其添加到网格的示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何填充DatagridViewComboboxColumn - How do I populate a DatagridViewComboboxColumn 如何将值添加到DataGridViewComboBoxColumn数据源? - How do I add values to a DataGridViewComboBoxColumn DataSource? 如何填充DataGridViewComboboxColumn? - how do I populate my DataGridViewComboboxColumn? 如何将DataGridViewComboBoxColumn绑定到返回列表的对象的属性/方法? - How do I bind a DataGridViewComboBoxColumn to a property/method of an object that returns a list? 我必须使用哪个事件来检测DataGridViewComboBoxColumn的值何时发生变化? - Which event I must use to detect when the value of a DataGridViewComboBoxColumn changes? 如何创建具有网格状下拉菜单的 DataGridViewComboBoxColumn? - How do I create DataGridViewComboBoxColumn that has a grid-like drop down? 如何将DataGridViewComboBoxColumn绑定到父行的绑定对象的list属性? - How do I bind a DataGridViewComboBoxColumn to the list property of the parent row's bound object? 如何修改DataGridViewComboBoxColumn的格式? - How can I modify the format of a DataGridViewComboBoxColumn? 如果datagridviewcombobox的值不在datagridviewcomboboxcolumn数据源中,该怎么办? - What do I do If the value of a datagridviewcombobox is not in the datagridviewcomboboxcolumn datasource? 如何在不使用对象的情况下检查DataGridViewComboBoxColumn中的项? - How can I check for an item in a DataGridViewComboBoxColumn without using an object?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM