【问题标题】:DataGridView bound to DataTable. Getting comboboxes for enumsDataGridView 绑定到 DataTable。获取枚举的组合框
【发布时间】:2009-04-14 15:55:48
【问题描述】:

如果你有一个 DataTable 有一个 Enum 类型的列。

然后你将DataGridView 绑定到这个DataTable (myDgv.DataSource = myDataTable)..

如何让DataGridView 在此列的每个单元格中显示一个组合框(或者它是下拉列表?您唯一能做的就是选择)?组合框应选择当前值,并且可选择其他可能的枚举值。

目前,这些单元格显示为普通的旧可编辑文本单元格,其中包含枚举值的字符串表示形式。

【问题讨论】:

    标签: .net winforms ado.net datagridview datatable


    【解决方案1】:

    我建议你阅读Forcing the DataGridView to do my bidding - a tale of ComboBox hackery:

    起初我对 DataGridView 示例(概述)(下载 DataGridView 示例)持乐观态度,但我没有看到任何我想做的事情:获取一个枚举值并使用组合框在网格中表示它.所以,这就是我的做法。

    你也应该看看How to: Bind Objects to Windows Forms DataGridView Controls:

    以下代码示例演示如何将对象集合绑定到 DataGridView 控件,以便每个对象显示为单独的行。此示例还说明了如何在 DataGridViewComboBoxColumn 中显示具有枚举类型的属性,以便组合框下拉列表包含枚举值。

    【讨论】:

      【解决方案2】:

      好吧,我不知道我要说的是否适合这里,但是我最近有一个类似的要求:在绑定到DataSet的DataGridView中显示一个Link,这是

      protected void grvResultado_RowDataBound(object sender, GridViewRowEventArgs e) {
          if (grvResultado.HeaderRow == null || grvResultado.HeaderRow.Cells.Count == 0) return;
          bool hasLink = false;
          int ind = 0;
          foreach (TableCell c in grvResultado.HeaderRow.Cells) {
              if (c.Text == "link") {
                  hasLink = true;
                  break;
              }
              ind++;
          }
          if (!hasLink) return;
      
      
          if (e.Row.RowType == DataControlRowType.DataRow) {
              TableCell c = e.Row.Cells[ind];
              var lnk = new HyperLink();
              lnk.Text = "Ver";
              lnk.NavigateUrl = c.Text;
              c.Controls.Clear();
              c.Controls.Add(lnk);
          }
      }
      

      你可以像我一样适应你需要的东西

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-08-04
        • 2018-11-12
        • 2020-02-07
        • 1970-01-01
        • 2014-10-04
        • 2020-07-26
        • 2016-02-06
        相关资源
        最近更新 更多