Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!
Guest

Event for checkbox on gridview

By: a guest on Feb 26th, 2012  |  syntax: None  |  size: 2.53 KB  |  views: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. public partial class Form1 : Form
  2. {
  3.     public Form1()
  4.     {
  5.         InitializeComponent();
  6.     }
  7.  
  8.     private void Form1_Load(object sender, EventArgs e)
  9.     {
  10.         dgv.DataSource = new testData[] {
  11.             new testData{ CheckBox = true, Name = "One" },
  12.             new testData{ CheckBox = true, Name = "Two" },
  13.             new testData{ CheckBox = false, Name = "Three" },
  14.             new testData{ CheckBox = false, Name = "Four" }            
  15.         };
  16.     }
  17.  
  18.     private void dgv_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
  19.     {
  20.         if (e.ColumnIndex == 0) // It's the Checkbox Column
  21.         {
  22.             DataGridViewRow dgvr = dgv.Rows[e.RowIndex];
  23.             MessageBox.Show(String.Format("Row {0} was cliked ({1})", (e.RowIndex + 1).ToString(),
  24.                 dgvr.Cells[1].Value));
  25.         }
  26.     }
  27. }
  28.  
  29. public class testData
  30. {
  31.     public Boolean CheckBox { get; set; }
  32.     public String Name { get; set; }
  33. }
  34.        
  35. <%@ Page Language="C#" AutoEventWireup="true" %>
  36.  
  37. <script runat="server">
  38.     public class Item
  39.     {
  40.         public string Name { get; set; }
  41.         public bool Checked { get; set; }
  42.     }
  43.  
  44.     protected void Changed(object sender, EventArgs e)
  45.     {
  46.         CheckBox checkBox = sender as CheckBox;
  47.         Response.Write(checkBox.Checked.ToString());
  48.     }
  49.  
  50.     protected void Page_Load(object sender, EventArgs e)
  51.     {
  52.         if (!IsPostBack)
  53.         {
  54.             grid.DataSource = new[]
  55.             {
  56.                 new Item() { Name="1", Checked = true },
  57.                 new Item() { Name="2", Checked = false }
  58.             };
  59.             grid.DataBind();
  60.         }
  61.     }
  62. </script>
  63. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  64.  
  65. <html xmlns="http://www.w3.org/1999/xhtml" >
  66. <head runat="server">
  67.     <title></title>
  68. </head>
  69. <body>
  70.     <form id="form1" runat="server">
  71.     <div>
  72.         <asp:GridView ID="grid" runat="server" AutoGenerateColumns="false">
  73.             <Columns>
  74.                 <asp:TemplateField>
  75.                     <ItemTemplate>
  76.                         <%# Eval("Name") %>
  77.                     </ItemTemplate>
  78.                 </asp:TemplateField>
  79.                 <asp:TemplateField>
  80.                     <ItemTemplate>
  81.                         <asp:CheckBox ID="check" runat="server" Checked='<%# Eval("Checked") %>' OnCheckedChanged="Changed" AutoPostBack="true" />
  82.                     </ItemTemplate>
  83.                 </asp:TemplateField>
  84.             </Columns>
  85.         </asp:GridView>
  86.     </div>
  87.     </form>
  88. </body>
  89. </html>
clone this paste RAW Paste Data