DataGridView根据枚举与Combobox列链接到DataTable [英] DataGridView linked to DataTable with Combobox column based on enum

查看:124
本文介绍了DataGridView根据枚举与Combobox列链接到DataTable的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个DataGridView链接到Datatable。其中一列是枚举,我希望将其显示为Combobox列。



我发现这个链接在DataGridView中的枚举中创建下拉列表选项,该选项具有使用以下内容的答案...

  DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn(); 
col.Name =我的枚举列;
col.DataSource = Enum.GetValues(typeof(MyEnum));
col.ValueType = typeof(MyEnum);
dataGridView1.Columns.Add(col);

我已经尝试过,但是当用户创建新的记录时,从下拉列表中选择一个选项(正确的选项显示),然后移出该字段,他们得到消息DataGridViewComboBoxCel值无效。我已经找到一些解决方案来搜索关于如何捕获这个错误的谈话,然后什么也不做(因此隐藏错误),但我想解决它不只是隐藏它。如果用户OK的消息他们得到它重复另外两次。



我还看到了循环遍历枚举中的值并创建一个包含int的datatable的解决方案和每个的字符串,然后使用datatable作为组合上的数据源。在使用后端MSSQL数据库时,我使用了一个datatable作为组合框的源代码。



另一个变体是循环和写入直接进入这样的组合...

  foreach(MyEnum.GetValues(typeof(MyEnum))中的MyEnum栏)
{
string barName = MyEnum.GetName(typeof(MyEnum),bar);
MyComboColumn.Items.Add(barName);
}

,如此链接中的问题。 如何将一些枚举值添加到组合框中< a>



我的问题:可以使用Enum.GetValues(typeof(MyEnum))进行工作吗?方法?
datatable方法看起来很长时间。循环然后使用MyComboColumn.Items.Add(barName);也会有一些长时间的缠绕,并将导致枚举的字符串版本记录在datatable中不是整数(我宁愿是整数)。



I找不到网格链接到datatable的Enum.GetValues(typeof(MyEnum))方法的示例。当我搜索时,我只是遇到其他方法。



我认为问题可能在于基础表列中的数据类型。我已经尝试过这个整数,像一个字符串,我试过没有定义它。我不能想到还有什么可以尝试的那种类型。



这是我的简化代码。 (DVG是我表单上的DataGridView)。

 枚举引擎类型
{
无= 0,
EngineType1 = 1,
EngineType2 = 2
}
public partial class MyClass:Form
{
DataTable DtTbl;

public MyClass()
{
InitializeComponent();
CreateTableStructure();
}

private void CreateTableStructure()
{
DGV.AutoGenerateColumns = false;
DGV.DataSource = DtTbl;

DtTbl = new DataTable();

DtTbl.Columns.Add(new DataColumn(Name,System.Type.GetType(System.String)));
DataGridViewTextBoxColumn NameCol = new DataGridViewTextBoxColumn();
NameCol.DataPropertyName =Name;
NameCol.HeaderText =Name;
DGV.Columns.Add(NameCol);

DtTbl.Columns.Add(new DataColumn(Engine,System.Type.GetType(System.Int32)));
DataGridViewComboBoxColumn EngineCol = new DataGridViewComboBoxColumn();
EngineCol.DataPropertyName =Engine;
EngineCol.HeaderText =Engine;
//EngineCol.DataSource = EngineType.GetValues(typeof(EngineType));

foreach(EngineType.GetValues(typeof(EngineType))中的EngineType引擎)
{
string engineName = EngineType.GetName(typeof(EngineType),engine);
EngineCol.Items.Add(engineName);
}


DGV.Columns.Add(EngineCol);

}
}


解决方案

我放弃了将枚举直接链接到combox。我以为Koryu已经解决了这个问题,但是当用户输入数据时,如果我尝试以编程方式添加一行,枚举的有效值,那么它仍然会出错。



根据我上面的代码,使用Koryu的更改,我添加了一个这样的行。

  private void CreateDefaultRows ()
{
DataRow Row = DtTbl.NewRow();
Row [Name] =FIN;
Row [Engine] = EngineType.EngineType1;
DtTbl.Rows.Add(Row);
DGV.DataSource = DtTbl;
}

但是,尽管调试确保有效的值我仍然收到错误。 p>

我通过使用datatable的方法解决了它。我在我的帮助类中写了一个泛型方法,它为任何枚举返回一个数据表。

  public static DataTable Enum2DataTable&T; 
{
DataTable EnumTable = new DataTable();
EnumTable.Columns.Add(new DataColumn(Value,System.Type.GetType(System.Int32)));
EnumTable.Columns.Add(new DataColumn(Display,System.Type.GetType(System.String)));
DataRow EnumRow;
foreach(Enum.GetValues(typeof(T))中的T E)
{
EnumRow = EnumTable.NewRow();
EnumRow [Value] = E;
EnumRow [Display] = E.ToString();
EnumTable.Rows.Add(EnumRow);
}

返回枚举表;
}

如果我在我的代码中定义组合框列时添加以下行在这个问题上,一切正常,没有其他变化,没有错误。

  EngineCol.DataSource = Enum2DataTable< EngineType>(); 

虽然这有效,但我有一个可重用的方法来再次做到这一点,它仍然觉得应该可以直接将枚举分配给组合框,因为几乎可以工作。



想知道为什么它不起作用,但是这个解决方案至少有效。


Having spent a lot of yesterday searching on this one I have to give up.

I have a DataGridView linked to a Datatable. One of the columns is an enum and I want that to show as a Combobox column.

I found this link Create drop down list options from enum in a DataGridView which has an answer of using the following...

    DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
    col.Name = "My Enum Column";
    col.DataSource = Enum.GetValues(typeof(MyEnum));
    col.ValueType = typeof(MyEnum);
    dataGridView1.Columns.Add(col);

I've tried that but when the user creates a new record, chooses an option from the drop down (the correct options show) then moves off the field they get the message "DataGridViewComboBoxCel value is not valid". I've found some solutions in searching that talk about how to trap this error, then do nothing (thus hiding the error) but I want to solve it not just hide it. If the user OK's the message they get it repeat another two times.

I've also seen solutions that loop through the values in the enum and create a datatable containing the int and the string for each one, then using the datatable as a datasource on the combo. I've used a datatable as the source for a combobox in the past when working with a back-end MSSQL database.

Another variant of this is to loop through and write straight into the combo such as this...

    foreach (MyEnum bar in MyEnum.GetValues(typeof(MyEnum)))
    {
        string barName = MyEnum.GetName(typeof(MyEnum), bar);
        MyComboColumn.Items.Add(barName);
    }

such as in the question in this link. How can I add some Enum values to a combobox

MY QUESTION: Can it be made to work using the Enum.GetValues(typeof(MyEnum)); method? The datatable method seems long winded. The looping then using MyComboColumn.Items.Add(barName); is also somewhat long winded and will result in the string version of the enum being recorded in the datatable not the integer (and I would rather it was the integer).

I can't find examples of the Enum.GetValues(typeof(MyEnum)) method where the grid is linked to a datatable. When I search on that I just come across the other methods.

I think the problem is likely to lie in the data type on the underlying table column. I've tried this as an integer, as a string and I've tried not defining it. I can't think what else to try on that type.

Here is my simplified code. (DVG is my DataGridView on the form).

enum EngineType
{
    None = 0,
    EngineType1 = 1,
    EngineType2 = 2
}
public partial class MyClass : Form
{
    DataTable DtTbl;

    public MyClass()
    {
        InitializeComponent();
        CreateTableStructure();
    }

    private void CreateTableStructure()
    {
        DGV.AutoGenerateColumns = false;
        DGV.DataSource = DtTbl;

        DtTbl = new DataTable();

        DtTbl.Columns.Add(new DataColumn("Name", System.Type.GetType("System.String")));
        DataGridViewTextBoxColumn NameCol = new DataGridViewTextBoxColumn();
        NameCol.DataPropertyName = "Name";
        NameCol.HeaderText = "Name";
        DGV.Columns.Add(NameCol);

        DtTbl.Columns.Add(new DataColumn("Engine", System.Type.GetType("System.Int32")));
        DataGridViewComboBoxColumn EngineCol = new DataGridViewComboBoxColumn();
        EngineCol.DataPropertyName = "Engine";
        EngineCol.HeaderText = "Engine";
        //EngineCol.DataSource = EngineType.GetValues(typeof(EngineType));

        foreach (EngineType engine in EngineType.GetValues(typeof(EngineType)))
        {
            string engineName = EngineType.GetName(typeof(EngineType), engine);
            EngineCol.Items.Add(engineName);
        }


        DGV.Columns.Add(EngineCol);

    }
}

解决方案

I gave up on doing this linking the enum directly to the combox. I thought that Koryu had solved it but while it then worked when a user entered data, if I tried to programmatically add a row, with a valid value for the enum, it still gave the error.

Based on my above code, with Koryu's change, I added a row like this.

private void CreateDefaultRows() 
{
  DataRow Row = DtTbl.NewRow();
  Row["Name"] = "FIN";
  Row["Engine"] = EngineType.EngineType1;
  DtTbl.Rows.Add(Row);
  DGV.DataSource = DtTbl;
}

But despite debugging to ensure a valid value I still got the error.

I solved it by using the method of using a datatable. I wrote a generic method in my helper class which returns a datatable for any enum.

    public static DataTable Enum2DataTable<T>()
    {
        DataTable EnumTable = new DataTable();
        EnumTable.Columns.Add(new DataColumn("Value", System.Type.GetType("System.Int32")));
        EnumTable.Columns.Add(new DataColumn("Display", System.Type.GetType("System.String")));
        DataRow EnumRow;
        foreach (T E in Enum.GetValues(typeof(T)))
        {
            EnumRow = EnumTable.NewRow();
            EnumRow["Value"] = E;
            EnumRow["Display"] = E.ToString();
            EnumTable.Rows.Add(EnumRow);
        }

        return EnumTable;
    }

If I then add the following line when defining the combo box column in my code in the question everything works fine with no other changes and with no errors.

EngineCol.DataSource = Enum2DataTable<EngineType>();

While this works, and I have a reusable method to do this again, it still feels like it should have been possible assigning the enum directly to the combo box as that 'almost' works.

Would love to know why it doesn't work but this solution works at least.

这篇关于DataGridView根据枚举与Combobox列链接到DataTable的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆