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

Format 1st row of radGrid

4 Answers 76 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Kurt Kluth
Top achievements
Rank 1
Kurt Kluth asked on 31 Jul 2014, 10:03 PM
Is it possible to set the backcolor of the very 1st item in my RadGrid?  I would like to format it green while alternating the colors white and gray on the remaining fields.

Also I would like to add a number count in the 1st column.  What would be the best way going about this?

4 Answers, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 01 Aug 2014, 04:27 AM
Hi Kurt Kluth,

Please try the following code snippet to format first row and add row number to a column.

ASPX:
<telerik:GridTemplateColumn UniqueName="RowNumber">
    <ItemTemplate>
        <asp:Label ID="lblRowNumber" runat="server" />
    </ItemTemplate>
</telerik:GridTemplateColumn>

C#:
void rgrdSample_PreRender(object sender, EventArgs e)
{
  if (rgrdSample.Items.Count > 0)
 {
  //Format first row of grid
  rgrdSample.Items[0].BackColor = Color.Green;
 }
}
protected void rgrdSample_ItemDataBound(object sender, GridItemEventArgs e)
{   
 //Set row count
 if (e.Item is GridDataItem)
 {
  int rowCounter = 0;
  Label lblRowNumber = e.Item.FindControl("lblRowNumber") as Label;
  rowCounter = rgrdSample.MasterTableView.PageSize * rgrdSample.MasterTableView.CurrentPageIndex;
  lblRowNumber.Text = (e.Item.ItemIndex + 1 + rowCounter).ToString();
 }
}

Thanks,
Princy
0
Kurt Kluth
Top achievements
Rank 1
answered on 04 Aug 2014, 01:33 PM
That worked beautifully thank you for that response.  Is there anyway to tweak it so the green background is only for record #1.  I realized that after I did it that while paging the 1st row on each page was green.

0
Accepted
Princy
Top achievements
Rank 2
answered on 05 Aug 2014, 04:11 AM
Hi Kurt Kluth,

You can check if its the first page using CurrentPageIndex property and set the color to the row.

C#:
if (rgrdSample.Items.Count > 0 && rgrdSample.CurrentPageIndex==0)
{
  rgrdSample.Items[0].BackColor = Color.Green;
}

Thanks,
Princy
0
Kurt Kluth
Top achievements
Rank 1
answered on 05 Aug 2014, 12:16 PM
Princy,

Thank you for all your help it is greatly appreciated.
Tags
Grid
Asked by
Kurt Kluth
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Kurt Kluth
Top achievements
Rank 1
Share this question
or