nullDev
nullDev

Reputation: 11597

Event for checkbox on gridview?

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?

Upvotes: 2

Views: 12069

Answers (2)

balexandre
balexandre

Reputation: 75083

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

Upvotes: 2

Darin Dimitrov
Darin Dimitrov

Reputation: 1038800

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>

Upvotes: 2

Related Questions