【问题标题】:Change to comboBox in datagridview At runtime在运行时更改为 datagridview 中的组合框
【发布时间】:2015-07-22 00:49:21
【问题描述】:

我正在使用默认的 datagridview 在我的 windows 窗体应用程序中显示数据。

如果有人点击了特定的列,我想为该单元格显示一个组合框。因此,在运行时,用户只能为该列单元格选择固定值。

我想在点击事件上这样做。

我正在以编程方式将列和行添加到 dataridview。

当我向 datagridView 添加一个数据表时,我无法将 DataGridViewComboBoxColumn 添加到数据表。

下面是代码。

           using (StringReader reader = new StringReader(new  StreamReader(fileStream, Encoding.Default).ReadToEnd()))
            {
                while (reader.Peek() != -1)
                {
                    string line = reader.ReadLine();

                    if (line == null || line.Length == 0)
                        continue;

                    string[] values = line.Split(',');

                    output.Add(values);
                    if (!isColumnCreated)
                    {

                        for (int i = 0; i < values.Count() - 1; i++)
                        {

                          table.Columns.Add(values[i]);

                        }
                        isColumnCreated = true;
                        continue;
                    }

                    DataRow row = table.NewRow();
                      for (int i= 0; i < values.Count() - 1; i++)
                    {



                        row[i] = values[i];


                    }

                   table.Rows.Add(row);

                }
            }

            dataGridView1.DataSource = table;

【问题讨论】:

    标签: c# winforms datagridview combobox windows-forms-designer


    【解决方案1】:

    我不建议在单击时更改 DataGridView 单元格的类型。只需使用 particular column 作为 DataGridViewComboBoxColumn 正常填充 DataGridView,然后试试这个:

    (your particular column).DisplayStyle = DataGridViewComboBoxDisplayStyle.Nothing;
    

    当它不处于编辑模式时,DataGridViewComboBoxCell 显示时没有下拉按钮。阅读更多here.

    【讨论】:

      猜你喜欢
      • 2011-08-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多