【问题标题】:Databinding DatagridviewComboboxColumn with enum带有枚举的数据绑定 DatagridviewComboboxColumn
【发布时间】:2013-10-24 17:20:22
【问题描述】:

我有一个通过从 SQL 服务器获取数据来填充的 datagridview。

gridview.Datasource = dataSet.Tables[0] 

-> 没问题。

在这个网格中,一列是 ComboboxColumn...

对于这个的填充(我的意思是,只绑定一个数据源),没问题:

cmb.DataSource = Enum.GetValues(typeof(MyEnum));
cmb.ValueType = typeof(MyEnum);
cmb.DataPropertyName = "MyEnum";

我想知道如何对datagridviewcomboboxcolumn进行数据绑定(该列在DB中的值是这个combobox的选定值的索引。这个combobox的数据源是一个Enum)。

DB 中的值:2 -> 这是要显示的项目的索引

我可以在某处精确说明 DB 列名吗?如果我在 datapropertyname 中这样做,我会收到错误 -> DataGridViewComboboxCell 值无效...

提前致谢!

编辑:问题“已解决”。我用数据表替换了枚举。简单得多。

【问题讨论】:

  • 您到底需要什么?...您的数据网格已绑定到表,并且您将未绑定的组合框列映射添加到枚举。在此之后您需要什么,例如组合框映射中的选定项目到某一列中的某个值?
  • 我有一个绑定到数据集的网格。
  • 是的,你是对的。通过知道网格已经绑定到数据集......我只需要知道如何绑定 datagridviewcomboboxcolumn(DB 中的值是组合框的索引。和组合框的数据源是一个枚举)。
  • 啊,我想我猜对了,您有一个包含一些值的列和未绑定的 datagridviewcomboboxcolumn,您希望每个组合框中的选定值等于数据库中该行列的值,对吗?例如,假设您的数据库列名为“test”,在 rowindex 2 列 [“test”] 中的值为 2,并且在同一行中,但在组合框列中,显示的值是那里项目的索引 2?...让我知道是不是这样,我提出了一些选择。
  • 是的,这就是我需要的:)

标签: c# datagridview enums


【解决方案1】:

如上面的 cmets 所述,我为您提供以下选项:

这是一个示例,您可以根据需要对其进行细粒度处理,因此首先在设置 gridview 数据源之前添加未绑定的 comboboxColumn 为其命名,然后设置数据源,然后设置 datagridview 数据源并订阅例如CellEndEdit 和 RowStateChanged 事件如下:

 DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
 col.DataSource = Enum.GetValues(typeof(MyEnum));
 col.Name = "testcolumn";

 int index = dataGridView1.Columns.Add(col);
 //"index" is if you want to set properties and so on to this column
 //but no need for this example.
 //dataGridView1.Columns[index].Name = "testcolumn";

 dataGridView1.DataSource = test;

 //the 2 event-handlers         
 dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit);
 dataGridView1.RowStateChanged += new DataGridViewRowStateChangedEventHandler(dataGridView1_RowStateChanged);

然后在这 2 个处理程序中执行此操作(处理 CellEndEdit,因此每次编辑包含数据库值的单元格时,comboboxcell 也会更新);

void dataGridView1_CellEndEdit(object sender, DataGridViewCellEventArgs e)
{   
      //here i use TestCol as a name for the column i want to check
      //you replace it with the column you have from the database
      //if you didnt change it of course...
      if (e.ColumnIndex == dataGridView1.Columns["TestCol"].Index)
      {
           //and here i assign the value on the current row in the testcolumn column
           //thats the combobox column...
           dataGridView1.Rows[e.RowIndex].Cells["testcolumn"].Value = (MyEnum)((int)dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value);
      }
}

private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
{
      //here i assign the initial values to the each cell in the combobox column
      //this whole example could be done in other ways but in a nutshell
      //it should provide you a good kickstart to play around.
      if (e.Row.DataBoundItem != null)
      {
           e.Row.Cells["testcolumn"].Value = (MyEnum)((int)e.Row.Cells["TestCol"].Value);
      }
}

我在这里假设数据库中的列只有数字值,例如 0 或 2 或 5,并且您的枚举必须具有相同数量的值,例如,如果在数据库列中您的值达到最大值 5那么你的枚举将是这样的,例如:

public enum MyEnum
{
    zero,
    one,
    two,
    three,
    four,
    five
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-26
    • 1970-01-01
    相关资源
    最近更新 更多