Barnabeck
Barnabeck

Reputation: 481

Can I apply operation to a range of cell indexes in GridView's RowDataBound Event?

I am setting GridView's layout features in the RowDataBound event based on the content of specific cells

private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[1].Text == " ")
        {
            e.Row.Cells[4].BackColor = System.Drawing.Color.White;
            e.Row.Cells[5].BackColor = System.Drawing.Color.White;
            e.Row.Cells[6].BackColor = System.Drawing.Color.White;

Is there a way to specify the range (cell index 4 to 6) where this operation has to be applied to?

Something like:

private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        if (e.Row.Cells[1].Text == "&nbsp;" && e.Row.CellIndex >= 4 && e.Row.CellIndex <= 6)
        {
            e.Row.BackColor = System.Drawing.Color.White;

Upvotes: 0

Views: 135

Answers (1)

Oliver
Oliver

Reputation: 93

That should be what you need:

private void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
  for (int i = 4; i <= 6; i++)
       if(e.Row.Cells[1].Text == "&nbsp;")
          e.Row.Cells[i].BackColor = System.Drawing.Color.White;
}

If you really need to check if its a DataControlRowType.DataRow, you can insert it to the if in the for loop.

Upvotes: 1

Related Questions