Congratulations to all monthly winners of May 2013 !!! They have won INR 2900 cash and INR 27497 worth prize.
Go to DotNetFunda.com
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 1968 |  Welcome, Guest!   Register  Login
Home > Articles > ASP.NET > Assign / Update the gridview Checkbox's Checked property value from a datafield that is of non-boolean datatype that is bound to a SQLDataSource.

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

2 vote(s)
Rating: 4 out of 5
Article posted by Ashokdiggavi on 4/23/2010 | Views: 16219 | Category: ASP.NET | Level: Beginner red flag

Advertisements

Advertisements
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,

Advertisements

If you like this article, subscribe to our RSS Feed. You can also subscribe via email to our Interview Questions, Codes and Forums section.

Page copy protected against web site content infringement by Copyscape
Found interesting? Add this to:



Please Sign In to vote for this post.

Experience:9 year(s)
Home page:http://www.dotnetfunda.com
Member since:Saturday, April 03, 2010
Level:Starter
Status: [Member]
Biography:
>> Write Response - Respond to this post and get points
Related Posts

We are In many sites to save HINDI data in SQL data base. But is too difficult to insert HINDI font in data base. I am tried to solve this problem. Then he work exactly .

This article demonstrate how to do CRUD operation using GridView and jQuery seamlessly (without page refresh) and also describes some simple UI effects in ASP.NET GridView control using jQuery. This is the Part 2 and last part of this article.

In this article, we are going to learn how to store view state at server side in a file and retrieve the state for asp.net page processing.

In this article, I have described the ways of keeping DropDownList in the GridView and binding the data by preserving the default SelectedValue. Apart from DropDownList, I have also shown how to keep CheckBox, RadioButtonList, TextBox in the GridView and preserving the default data.

This is a example of populating three different DetailsView based on selection of of record in a GridView using Multiple DataKeyNames in C sharp and ASP .NET In this example GridView is populated from a table called Website using SqlDataSource, on GridVies i have defined multiple (3) DataKeyNames separated by comma, which will be used to fetch the record related to those DataKeyName from 3 tables in 3 DetailsViews

More ...
About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 6/19/2013 12:44:32 PM