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.
Please advice.
10 Answers, 1 is accepted

Hi,
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.
Column types
You can dynamically change the CheckBox value of a GridCheckBox column as shown below.
CS:
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[0]; |
string strtxt = item["ProductName"].Text.ToString(); |
if (strtxt == "Cola") |
{ |
chkbx.Checked = true; |
} |
} |
} |
Thanks
Princy.

could you please give a complete example, my database didn't support boolean data type for columns.
Thanks.
Emad

Here is the full code that I tried which worked as expected.
ASPX:
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
AutoGenerateColumns
=
"False"
DataSourceID
=
"SqlDataSource1"
OnItemDataBound
=
"RadGrid1_ItemDataBound"
>
<
MasterTableView
>
<
Columns
>
<
telerik:GridCheckBoxColumn
UniqueName
=
"CheckCol"
></
telerik:GridCheckBoxColumn
>
<
telerik:GridBoundColumn
UniqueName
=
"EmployeeID"
DataField
=
"EmployeeID"
>
</
telerik:GridBoundColumn
>
</
Columns
>
</
MasterTableView
>
</
telerik:RadGrid
>
Thanks,
Princy.

Your code work fine to just view the checkbox item in grid but i can't update my column.
Scenario..
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
Thanks,
Emad

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.
ASPX:
<
telerik:GridTemplateColumn
UniqueName
=
"CheckCol"
DataField
=
"TestField"
HeaderText
=
"CheckCol"
>
<
ItemTemplate
>
<
asp:CheckBox
ID
=
"CheckBox1"
runat
=
"server"
Checked='<%# DataBinder.Eval (Container.DataItem,"TestField").ToString()!="0"?true:false %>' />
</
ItemTemplate
>
</
telerik:GridTemplateColumn
>
Thanks
Princy.

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" >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem,"BUILT_IN").ToString()!="0"?true:false %>' />
</ItemTemplate>
<EditItemTemplate >
<asp:CheckBox ID="CheckBox2" runat="server" Checked='<%# DataBinder.Eval (Container.DataItem,"BUILT_IN").ToString()!="0"?true:false %>' />
</EditItemTemplate>
</telerik:GridTemplateColumn>
Thanks
Emad

Try the following code.
C#:
protected
void
RadGrid1_ItemDataBound(
object
sender, Telerik.Web.UI.GridItemEventArgs e)
{
if
(e.Item
is
GridEditableItem && e.Item.IsInEditMode)
{
GridEditableItem item = (GridEditableItem)e.Item;
CheckBox chk = (CheckBox)item.FindControl(
"CheckBox1"
);
if
(chk.Checked)
{
//code here
}
}
}
Thanks,
Princy.

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
Please advice.
Thanks,
Emad

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.
SqlDataSource1.UpdateParameters[
"Active"
].DefaultValue = chk.Checked ?
"1"
:
"0"
;
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.
Kind regards,
Tsvetina
the Telerik team