tags:

views:

3499

answers:

2

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

How do I do this?

+2  A: 

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"&gt;

<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>
Darin Dimitrov
+1 I think this was more what he was looking for.
Andrew Hare
Its a good practice to check vs null after this. CheckBox checkBox = sender as CheckBox; if(checkBox = null) return;or use direct cast then CheckBox checkBox = (CheckBox)sender;Otherwise you will form bad habit for new developers ;)Welcome to nitpickers club :D ...
Alexey Shcherbak
Thanks darin, but I needed the solution for windows forms.
nullDev
bext time nullDev, add windowsform in the tag of your question so all can know that :)
balexandre
+2  A: 

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

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.

Lets say that the first Cell is the checkbox:

and the Second some text...

my code for 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 dgv and in the Events, double click the event CellMouseClick

balexandre