If we could have accessed the Bind method ,that implicitly performs two-way binding, in the code-behind then we could have just override the RowDatabound event and assigned the value to the gridview's checkbox control using the Bind method. But unfortunately the Bind is not available in ASP.Net 2.0 instead Eval only is available.
1. Wrap the Eval(fieldname) HTML code in a function eg: Checkvalue that checks the datafield value, performs transformations to True OR False values and assigns to Chekced property.
2. Override the RowUpdating event of gridview and update only the Checked property's value since the SQLDataSource that is bound to gridview will take care of remaining fields to update.
Please let me know if this has helped anyone solve the problem!!
Introduction
This is bit tricky but possible!!
When you try to assign expected boolean value to Checked property of a chekcbox using Eval or Bind methods in HTML the application throws exception - Specified cast is invalid.
The issue is about achieving Two-way binding with the checkbox!!
Actually we could assign the checked property using different methods eg: one way is to overriding the RowDatabound event of gridview.
Find the checkbox control from the selected gridviewdatarow object and assign the value using either Eval(fieldname) OR Container.DataItem(fieldname) methods.
But I am not sure how we would update this Checked property since the field would become ReadOnly. The SQlDataSource will not update this checkbox's Checked property value.
I know one method that uses less code and is easy. It has 2 steps as follows...
Step 1
We could instead assign the checked property by wrapping the Eval(fieldname) inside a function Checkvalue(Eval(fieldname)).
eg:
.
.
.
<EditItemTemplate>
<asp:Checkbox = id = "chk1" runat="sever" Checked = '<%# Checkvalue(Eval(fieldname)) %>' />
</EditItemTemplate>
.
.
.
in the code behind the Checkvalue(...) function is as follows...
Public bool Checkvalue(object obj)
{
if(obj != null)
{
//Assuming the datafield returns 0s and 1s and we need to transform the values
//to a boolean datatype.
return obj.ToString() == 1? true:false;
}
else
{
return false;
}
}
By performing above coding we would avoid the error - Specified cast is invalid.
Step 2
Then we need to override the gridview's RowUpdating event. In this event you can write code to get the newvalue of checkbox's checked property only. The Sqldatasource that is bound to gridview will pick up the other remaining fields new values, to update!!
Event Code folows...
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//Override GridView's RowUpdating event to update the datafield values.
GridViewRow grdvwrw_UPD =((GridView)sender).Rows[e.RowIndex];
bool value;
value = ((CheckBox)grdvwrw_UPD.Cells[0].FindControl("chk1")).Checked;
if (value == true)
{
e.NewValues.Add("RESULTS", 1);
}
else
{
e.NewValues.Add("RESULTS", 0);
}
}
Conclusion
If we could have accessed the Bind method,that implicitly performs two-way binding, in the code-behind then we could have just override the RowDatabound event and assigned the value to the gridview's checkbox control using the Bind method. But unfortunately the Bind is not available in ASP.Net 2.0 instead Eval only is available.
Instead the above code performs the magic of two-way binding for the checkbox.
Please let me know if this has helped anyone solve the problem!!
Thanks,