Search
Winners

Win Prizes

Social Presence
Twitter Twitter LinkedIn YouTube Google

Like us on Facebook
Advertisements
Top Articles Author
Thu, 31-Jul-2014 Authors
All Time Authors
Sourav.Kayal
39750
Sheonarayan
37300
Niladri.Biswas
33350

Latest members | More ...


(Statistics delayed by 5 minutes)
Ads

Assign / Update the gridview Checkbox's Checked property value from a datafield that is of non-boolean datatype that is bound to a SQLDataSource.

Ashokdiggavi
Posted by under ASP.NET category on for Beginner level | Views : 18418 red flag
If you found plagiarised (copied) or inappropriate content,
please let us know the original source along with your correct email id (to communicate) for further action.
Rating: 4 out of 5
2 vote(s)
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.

We could still achieve this in two simple and easy steps.
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,

Page copy protected against web site content infringement by Copyscape
About the Author

Ashokdiggavi

Full Name: ashok diggavi
Member Level: Starter
Member Status: Member
Member Since: 4/3/2010 12:13:30 PM
Country: United States
ashok
http://www.dotnetfunda.com

Login to vote for this post.
Found interesting? Add this to:


Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)