【问题标题】:Restricting the input in DataGridView c#限制DataGridView c#中的输入
【发布时间】:2015-05-21 13:08:35
【问题描述】:

所以我在表单中有一个 DataGridView,我想限制向其单元格添加数据。

我正在尝试使任何添加行的单元格成为组合框,以便用户必须从组合框中选择单元格的数据。

另外,当用户在最后一行添加任何值时,dataGridView 会自动创建一个新行,并将这个新行添加为组合框。

这张图片显示了我的表格,我知道每一列的预期值,这就是为什么我想在每个单元格中使用组合框来限制它。

【问题讨论】:

    标签: c# datagridview combobox


    【解决方案1】:

    创建列时,将它们分别创建为DataGridViewComboBoxColumn。正如你所说:

    [你]知道每一列的期望值

    因此,您可以通过这种方式创建列并绑定每列的源。例如:

    public Form1()
    {
      InitializeComponent();
    
      List<List<string>> options = new List<List<string>>()
      {
        new List<string>() { "Foo 1", "Foo 2", "Foo 3" },
        new List<string>() { "Bar 1", "Bar 2", "Bar 3" },
        new List<string>() { "Baz 1", "Baz 2", "Baz 3" }
      };
    
      List<string> names = new List<string>() { "Foo", "Bar", "Baz" };
    
      for (int i = 0; i < names.Count; i++)
      {
        DataGridViewComboBoxColumn col = new DataGridViewComboBoxColumn();
        col.Name = names[i];
        col.DataSource = options[i];
        this.dataGridView1.Columns.Add(col);
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-01
      • 2010-12-14
      • 2012-02-29
      • 2015-07-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多