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

Make only certain rows in radgrid bold

18 Answers 733 Views
Grid
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 24 Sep 2012, 05:59 PM
Hi,  I have a dataset that has a column called bold that's a bit.   When it's true I want that row to be bold, otherwise not.  How can I do this in radgrid?  Thanks in advance

18 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 25 Sep 2012, 04:46 AM
Hi John,

Try the following code to achieve your scenario.
C#:
protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
 if (e.Item is GridDataItem)
 {
  GridDataItem item = (GridDataItem)e.Item;
  CheckBox chk = (CheckBox)item["UniqueName"].Controls[0];
  if (chk.Checked == true)
  {
     item.Style.Add("font-weight", "bold");
  }
 }
}

Thanks,
Shinu.
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 02:36 PM
I'm getting an error on the checkbox line
cannot convert type system.web.ui.control to system.windows.forms.checkbox
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 02:50 PM
Hi John,

It sounds like you may have both System.Web.UI and System.Windows.Forms referenced in your app. Try prefacing Checkbox with "System.Web.UI." and see if that helps!

Thanks,
Casey
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 02:54 PM
Yeah I reference both,  however, prefacing it with System.Web.UI.Checkbox claims that Checkbox is not in System.Web.UI
(Thank you though for the suggestion)
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 03:04 PM
Oooops, sorry about that. Try: System.Web.UI.WebControls.Checkbox
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 03:11 PM
Great success in that it accepts it and doesn't complain anymore (Much thanks!).   However the rows where bold is true are not being bolded still.   I'm missing something else perhaps?

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)item["Bold"].Controls[0];
            if (chk.Checked == true)
            {
                item.Style.Add("font-weight", "bold");
            }
        }
    }
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 03:15 PM
I use almost the same approach recommended by Shinu, except I set the property of the GridDataItem instead of adding styles to it. 

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)item["Bold"].Controls[0];
            if (chk.Checked == true)
            {
                item.Font.Bold = true;
            }
        }
    }
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 03:20 PM
Hmm, gave it a go but still no dice.
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 03:24 PM
It doesn't seem to be calling the RadGrid1_ItemDataBound function,   

I should have noted that I'm loading the data through a datatable through NeedDataSource
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 03:28 PM
Have you added the event to the RadGrid in your .aspx page? OnItemDataBound="RadGrid1_ItemDataBound" 

If you put a breakpoint put in that event is it hit? 
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 03:34 PM
That's what I needed,  thanks!   The function is getting hit now.

However I'm now getting a Specified argument was out of the range of valid values now hah.  
I'm guessing it's because within the Bold column, there are some nulls,  I can fix it database side so there aren't nulls, but just out of curiosity what would be the way to check for that?

Also thanks so much for the very good and speedy help!
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 04:07 PM
Actually I lied,   I edited the query to take only values where bold is null and I'm still getting that Specified Argument was out of the range of valid values for 

System.Web.UI.WebControls.CheckBox chk = (System.Web.UI.WebControls.CheckBox)item["Bold"].Controls[0];
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 04:09 PM
What type of column is the Bold column bound to in your RadGrid? 
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 04:12 PM
Within Radgrid itself?   I don't think I explicitly bound it to anything... which is probably the problem?

The only place I defined it was here

<telerik:GridBoundColumn DataField="Bold" HeaderText="Bold" UniqueName="Bold"></telerik:GridBoundColumn>
0
Accepted
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 04:18 PM
That explains the error. I think Shinu was assuming you were using a GridCheckBoxColumn. It's fine that you aren't, but instead of trying to get a reference to the Checkbox, you can just check the value of the text property. I'm assuming you are using 0 and 1 and that you aren't switching to False/True when populating your DataTable.

protected void RadGrid1_ItemDataBound(object sender, Telerik.Web.UI.GridItemEventArgs e)
    {
        if (e.Item is GridDataItem)
        {
            GridDataItem item = (GridDataItem)e.Item;
            if (item["Bold"].Text == "1")
            {
                item.Font.Bold = true;
            }
        }
    }
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 04:26 PM
That did it, you rock,  when it's getting loaded in the datatable it is switching to true/false but when checking for it, success is had.

Now the last million dollar question (it may not be the last..) is,  can I do what's currently happening without actually showing the bold column?
0
John
Top achievements
Rank 1
answered on 25 Sep 2012, 04:28 PM
Figured it out,  just setting the gridboundcolumn for Bold as Display="false" did the trick.


Thanks so much for the assistance Casey!
0
Casey
Top achievements
Rank 1
answered on 25 Sep 2012, 04:48 PM
No problem. Glad I could help!
Tags
Grid
Asked by
John
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
John
Top achievements
Rank 1
Casey
Top achievements
Rank 1
Share this question
or