In my database, I do not use boolean for the field that i need to show in gridview by using checkbox (1 for check and 0 for uncheck).
May I know how can I do this?
Beside that, may I know, how can I programmatically change the checkbox value? Please advice.
10 Answers, 1 is accepted
You can use a GridCheckBoxColumn to display a check box to represent a boolean value. Bind this column type to a boolean field by setting its DataField property. If this column type is bound to a data value that is not boolean, the grid throws an exception.
You can dynamically change the CheckBox value of a GridCheckBox column as shown below.
|protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)|
|if (e.Item is GridDataItem)|
|GridDataItem item = (GridDataItem)e.Item;|
|CheckBox chkbx = (CheckBox)item["CheckColumn"].Controls;|
|string strtxt = item["ProductName"].Text.ToString();|
|if (strtxt == "Cola")|
|chkbx.Checked = true;|
could you please give a complete example, my database didn't support boolean data type for columns.
Here is the full code that I tried which worked as expected.
Your code work fine to just view the checkbox item in grid but i can't update my column.
My database is oracle i have column with data type is (number) this column accept (0 or 1) only. how can i view this column as checkbox in radgrid and also apply the insert and update correctly. I user automatic insert,update and delete
GridCheckBoxColumn will accept only boolean value. But you can achieve the desired scenario using a GridTemplateColumn with a CheckBox in its ItemTemplate. Give a try with the following approach and whether it is working.
Thanks for your reply.
your code is working in viewing the grid only. In edit form it's not appear, i update your code adding edit template item now it's appear in edit form but no functionality.
i.e if you check it value will not go to database as 1. Nothing happen.
telerik:GridTemplateColumn UniqueName="BUILT_IN_CHK" DataField="BUILT_IN" HeaderText="CheckCol" >
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"BUILT_IN").ToString()!="0"?true:false %>' />
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# DataBinder.Eval (Container.DataItem,"BUILT_IN").ToString()!="0"?true:false %>' />
Try the following code.
Thanks, but I think you didn't got me correct.
checkbox is working fine in view data in grid and also fine in edit form.
my problem is when check/uncheck this checkbox in edit form then column value in database is not affected. I use automatice insert/update/delete
You would need to manually convert back the checked/unchecked state of the checkbox to a 0 or 1 for the Oracle DB, as the checkbox can keep only a true/false value by itself.
If you bind the grid to an SqlDataSource, on Update and Insert, manually set the UpdateParameter for the field in question. This could be done in the UpdateCommand/InsertCommand event.
If you bind your grid through NeedDataSource, you cannot have automatic operations, so it is up to you how you pass the 0/1 values to the database.
the Telerik team