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

[Solved] GridCheckBoxColumn with boolean data is NOT working

3 Answers 831 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Rita15
Top achievements
Rank 1
Rita15 asked on 16 Mar 2010, 06:06 PM
hi,

  I have RadGrid with columns like "Discontinued " which can have valid values as Yes/No or 1/0. I am using SQL server database bit column to store the "Discontinued " data. 

When I try to display "Discontinued "  as a checkbox item inside the grid, I get the error message that says " can not convert string to Boolean". 

<telerik:GridCheckBoxColumn DataField="Discontinued" HeaderText="Discontinued" SortExpression="Discontinued"
                        UniqueName="Discontinued" EditFormColumnIndex="1">
</telerik:GridCheckBoxColumn>

Is there a way to convert the database value to boolean before it gets attached to the RadGrid?

Thanks!



3 Answers, 1 is accepted

Sort by
0
Accepted
Shinu
Top achievements
Rank 2
answered on 17 Mar 2010, 06:44 AM
Hello,

You can achieve this result by checking the value of 'Discontinued' field and set the Checked property of GridCheckBoxColumn accordingly, inside ItemDataBound event.

ASPX:


<
telerik:GridCheckBoxColumn UniqueName="Discontinued" HeaderText="Discontinued"></telerik:GridCheckBoxColumn> 

C#:

protected
 void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    if (e.Item is GridDataItem) 
    { 
        GridDataItem item = (GridDataItem)e.Item; 
        DataRowView row = (DataRowView)item.DataItem; 
        CheckBox chk = (CheckBox)item["Discontinued"].Controls[0]; 
        string value = row["Discontinued"].ToString(); 
 
        if (value == "Yes" || (value == "1")) 
            chk.Checked = true
        else 
            chk.Checked = false
    } 

-Shinu.
0
D
Top achievements
Rank 1
answered on 15 Sep 2011, 07:09 PM
this works to initially display the value, but causes issues when using inline editing of the value.  It keeps the value in its existing (checked or unchecked) state.  Is there a way to determine within the ItemDataBound event that we are in Update mode so that we can skip this code?  
0
Carlos Sesma
Top achievements
Rank 1
answered on 25 Apr 2013, 10:42 PM
This is an old post and I don't know if by now there is a better solution,but since is not answered Ill post how I'm doing this.
first use a template column like this:

<telerik:GridTemplateColumn HeaderText="enabled" UniqueName="TemplateColumn">
  <EditItemTemplate>
      <asp:CheckBox ID="CheckBox2" runat="server"
          Checked='<%#  Convert.ToInt32(Eval("enabled")) == 1 %>'
          oncheckedchanged="CheckBox2_CheckedChanged" />
  </EditItemTemplate>
  <ItemTemplate>
      <asp:CheckBox ID="CheckBox1" runat="server" Checked='<%# Convert.ToInt32(Eval("enabled")) == 1 %>'
          Enabled="False" />
  </ItemTemplate>
</telerik:GridTemplateColumn>

In this case I'm using 1 for true 0 for false.
on server catch the checkbox checkedchange event and save in a session:

    
bool NewEnabled
{
    get
    {
 
        return (Session["NewEnabled"] == null) ? false : Convert.ToBoolean(Session["NewEnabled"]);
    }
    set
    {
        Session["NewEnabled"] = value;
    }
}
protected void CheckBox2_CheckedChanged(object sender, EventArgs e)
{
    NewEnabled = (sender as CheckBox).Checked;
}
then in updating or inserting Events pass the session variable as parameter. you could take this value from sessionparameter or do it manually like this:

        protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
        {
            e.Command.Parameters["enabled"].Value = NewEnabled;
        }
and that's it.
Tags
Grid
Asked by
Rita15
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
D
Top achievements
Rank 1
Carlos Sesma
Top achievements
Rank 1
Share this question
or