Edit/Access data from a CheckBox column in an ASPX:GridView - c#

Posted by Endo on Stack Overflow See other posts from Stack Overflow or by Endo
Published on 2010-04-20T02:05:58Z Indexed on 2010/04/20 2:13 UTC
Read the original article Hit count: 1005

Filed under:
|
|
|
|

Hi, I have a GridView to which I bind a dataTable I manually create. Both the GridView and the dataTable contain 2 columns, Name and isBusy. My GridView looks like this

<Columns>
   <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">           
   </asp:BoundField>       
   <asp:CheckBoxField DataField="isBusy" HeaderText="Busy" SortExpression="isBusy" />   
</Columns>

That works fine, except that the Busy column is non-editable unless you set a specific row to edit mode. I require the entire column of checkboxes to be checkable. So I converted the column to a template, and so the columns look like this:

<Columns>
   <asp:BoundField HeaderText="Name" DataField="Name" SortExpression="Name">           
   </asp:BoundField>       
   <asp:TemplateField HeaderText="Busy" SortExpression="isBusy">
       <ItemTemplate>
       <asp:CheckBox ID="isBusy" runat="server" Checked='<%# Eval("isBusy") %>' oncheckedchanged="CheckBoxBusy_CheckedChanged" />
       </ItemTemplate>
   </asp:TemplateField>       
</Columns>

Now, this throws an error at runtime, saying

System.InvalidCastException was unhandled by user code
  Message="Specified cast is not valid."
  Source="App_Web_zzjsqlrr"
  StackTrace:
       at ASP.proyectos_aspx.__DataBinding__control24(Object sender, EventArgs e) in c:\Proyect\Users.aspx:line 189
       at System.Web.UI.Control.OnDataBinding(EventArgs e)
       at System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding)
       at System.Web.UI.Control.DataBind()
       at System.Web.UI.Control.DataBindChildren()
  InnerException:

Any idea why this is happening? The next step I would need is to know how to set and get a checkbox's state (haven't been able to find how to manually check a checkbox).

I appreciate very much any help.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about gridview