This is a migrated thread and some comments may be shown as answers.

Manually set the checkbox value from database value 1 or 0

10 Answers 315 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ching-Yen
Top achievements
Rank 1
Ching-Yen asked on 20 May 2008, 07:51 AM
Hi,

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

Sort by
0
Princy
Top achievements
Rank 1
answered on 20 May 2008, 08:05 AM

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.

0
Emad
Top achievements
Rank 1
answered on 17 Dec 2011, 05:34 PM
Hi,

could you please give a complete example, my database didn't support boolean data type for columns.
Thanks.
Emad
0
Princy
Top achievements
Rank 1
answered on 19 Dec 2011, 07:29 AM
Hello,

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.
0
Emad
Top achievements
Rank 1
answered on 19 Dec 2011, 08:02 AM
Hello,

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
0
Princy
Top achievements
Rank 1
answered on 20 Dec 2011, 07:56 AM
Hello 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.
0
Emad
Top achievements
Rank 1
answered on 20 Dec 2011, 09:07 AM
Hello 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
0
Princy
Top achievements
Rank 1
answered on 21 Dec 2011, 07:49 AM
Hello 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.
0
Emad
Top achievements
Rank 1
answered on 21 Dec 2011, 10:31 AM
Hi 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
0
Emad
Top achievements
Rank 1
answered on 29 Dec 2011, 07:11 AM
Please advice
0
Tsvetina
Telerik team
answered on 29 Dec 2011, 10:47 AM
Hi 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
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
Ching-Yen
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 1
Emad
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or