无法选择数据绑定的 DataGridView 中的复选框

发布于 2024-09-28 06:42:56 字数 384 浏览 3 评论 0原文

我有一个运行良好的 DataGridView。我用它只是为了显示数据。

现在,我希望能够通过复选框选择行,并通过单击按钮仅对选定的行执行操作(此按钮在同一表单上的网格之外)。 为此,我遵循 这些步骤将复选框列添加到 datagridview。

运行应用程序时看到的是我无法通过鼠标单击或键盘选中该复选框。从它的外观我可以理解它不处于禁用/只读状态。因此,每当我尝试单击该复选框时,它都会像启用的复选框一样正常更改其边框。但最终它没有选中复选框。

I've a DataGridView which is working perfectly fine. I use it just to show data.

Now I want ability to select rows by check box and perform an operation for only selected rows on click of a button (this button is out of the grid on the same form).
For this purpose, I'm following these steps to add checkbox column to datagridview.

On running the application what is see is I can't check the check box either by mouse click or keyboard. And by its looks I can understand that its not in disabled/readonly state. So whenever I try to click on the checkbox, it changes it's borders normally as an enabled check box does. But finally it is not checking the check box.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂归处 2024-10-05 06:42:56

尝试一下。

 private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewCheckBoxColumn ck = new DataGridViewCheckBoxColumn();
        dataGridView1.Columns.Insert(0,ck);
    }

可能会帮助你。

Ismail 这是您的困惑的解决方案Dgv-DatabindingCompleteEvent

Try it.

 private void Form1_Load(object sender, EventArgs e)
    {
        DataGridViewCheckBoxColumn ck = new DataGridViewCheckBoxColumn();
        dataGridView1.Columns.Insert(0,ck);
    }

may help you.

Ismail here is your solution of your confusion Dgv-DatabindingCompleteEvent

缪败 2024-10-05 06:42:56

我遇到了同样的问题。对我来说,解决方案非常简单。我的 datagridview 有一个禁用的编辑选项(因为我不希望用户更改数据),并且我希望能够选中/取消选中我的 DataGridViewCheckBoxColumn。因此,在 dataGridView 属性中,我选中了“启用编辑”选项,但在代码中,我已为除我所需的 checkBoxColumn 之外的每一列禁用它。
希望它会对某人有所帮助。

I went through the same problem. For me the solution was pretty simple. My datagridview had a disabled editing option (because I didn't want the user to change the data) and I wanted to be able to check/uncheck my DataGridViewCheckBoxColumn. So in the dataGridView properties I checked the 'Enable Editing' option, but in the code I've disabled it for every column except of mine desired checkBoxColumn.
Hope it will help to somebody.

嗼ふ静 2024-10-05 06:42:56

如果你想检查 dgv 中所有复选框的状态:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;

    if (Convert.ToBoolean(chk.Value) == true)
      MessageBox.Show("this cell checked");

}

if you want to check the state of all checkBoxes in the dgv:

foreach (DataGridViewRow row in dataGridView1.Rows)
{
    DataGridViewCheckBoxCell chk = row.Cells[0] as DataGridViewCheckBoxCell;

    if (Convert.ToBoolean(chk.Value) == true)
      MessageBox.Show("this cell checked");

}
天冷不及心凉 2024-10-05 06:42:56

就我而言,我在 page_load 中有 gridview 加载代码,因此单击按钮后,网格会重新加载并丢失其选择。所以将代码移到里面

if(!isPostback)
{ LoadGrid();}

对我有用。

在page_load中放置一个断点以便更好地理解。

In my case I had the gridview loading code in page_load, so after a button click the grid was reloading and lost its selection. So Moving the code inside

if(!isPostback)
{ LoadGrid();}

worked for me.

Put a breakpoint in page_load to understand better.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文