C# gridview上复选框的事件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/461049/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 04:16:53  来源:igfitidea点击:

Event for checkbox on gridview?

c#.net

提问by nullDev

I have a checkbox column on my gridview in a Windows Application. I want an event as soon as somebody clicks on the checkbox.

我在 Windows 应用程序的 gridview 上有一个复选框列。只要有人点击复选框,我就想要一个事件。

How do I do this?

我该怎么做呢?

采纳答案by balexandre

New answer, because now I know it's Windows Form

新答案,因为现在我知道它是 Windows 窗体

First of all, you need to set the row to be editable in order to the user click in the chekbox, to avoid that you can see when the client click in the CELL of a row.

首先,您需要将该行设置为可编辑,以便用户在复选框中单击,以避免在客户端单击某行的 CELL 时您可以看到。

Lets say that the first Cell is the checkbox:

假设第一个 Cell 是复选框:

and the Second some text...

第二个是一些文字...

my code for Form1.cs

我的 Form1.cs 代码

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        dgv.DataSource = new testData[] {
            new testData{ CheckBox = true, Name = "One" },
            new testData{ CheckBox = true, Name = "Two" },
            new testData{ CheckBox = false, Name = "Three" },
            new testData{ CheckBox = false, Name = "Four" }            
        };
    }

    private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
    {
        if (e.ColumnIndex == 0) // It's the Checkbox Column
        {
            DataGridViewRow dgvr = dgv.Rows[e.RowIndex];
            MessageBox.Show(String.Format("Row {0} was cliked ({1})", (e.RowIndex + 1).ToString(), 
                dgvr.Cells[1].Value));
        }
    }
}

public class testData
{
    public Boolean CheckBox { get; set; }
    public String Name { get; set; }
}

the design ... just drag a DataGridView component into the window form, named dgvand in the Events, double click the event CellMouseClick

设计...只需将 DataGridView 组件拖入名为dgv的窗口窗体中,然后在 Events 中双击事件CellMouseClick

回答by Darin Dimitrov

Here's a sample:

这是一个示例:

<%@ Page Language="C#" AutoEventWireup="true" %>

<script runat="server">
    public class Item
    {
        public string Name { get; set; }
        public bool Checked { get; set; }
    }

    protected void Changed(object sender, EventArgs e)
    {
        CheckBox checkBox = sender as CheckBox;
        Response.Write(checkBox.Checked.ToString());
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            grid.DataSource = new[] 
            { 
                new Item() { Name="1", Checked = true },
                new Item() { Name="2", Checked = false } 
            };
            grid.DataBind();
        }
    }
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
            <Columns>
                <asp:TemplateField>
                    <ItemTemplate>
                        <%# Eval("Name") %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:CheckBox ID="check" runat="server" Checked='<%# Eval("Checked") %>' OnCheckedChanged="Changed" AutoPostBack="true" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
    </form>
</body>
</html>