【问题标题】:How to create radiobuttons programatically on a datagridview如何在 datagridview 上以编程方式创建单选按钮
【发布时间】:2013-07-09 03:07:54
【问题描述】:

我在 Visual Studio 2010 中使用 C#。我在 winform 上有一个 datagridview。我希望 1 列仅包含单选按钮。每行将有 3 个单选按钮。我不确定我是否理解为此需要什么。任何帮助将不胜感激。

【问题讨论】:

  • datagridview 中不支持单选按钮。因此,您可以使用复选框,然后为用作单选按钮的复选框编写代码...然后提出一些问题:如果仍然没有成功,您尝试做什么

标签: winforms visual-studio-2010 datagridview radio-button


【解决方案1】:

创建一个DataGridViewCheckBoxColumn 并连接到CellContentClick 事件。然后使用此代码:

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
  {
     if (e.ColumnIndex == 0)// checkbox column
     {
        object curr =
           dataGridView1[e.ColumnIndex, e.RowIndex].Value;

        if (curr == null || (bool)(curr) == false)
        {
           for (int i = 0; i < dataGridView1.RowCount; i++)
           {
              if (i != e.RowIndex)
              {
                 dataGridView1[e.ColumnIndex, i].Value = false;
              }
           }
        }
     }
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-25
    • 2014-10-16
    • 2019-03-25
    • 2019-10-23
    • 2018-06-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多