I have a GridView to which I bind a dataTable that I manually created. 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:
System.InvalidCastException was unhandled by user code
Message="Specified cast is not valid."
Source="App_Web_zzjsqlrr"
StackTrace:
at ASP.projects_aspx.__DataBinding__control24(Object sender, EventArgs e) in c:\Project\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).
Ok, I got past the error coverting the checkbox value to a bool:
Checked='<%# Convert.ToBoolean(Eval("isBusy")) %>'
Now, I can't seem to find how to manually check a specific checkbox and generate an event when a checkbox is clicked. Any ideas?