Search This Blog

2010/07/19

How to find particular row, column and cell from Gridview?

The Gridview control is widely used control to display data. The data is displayed in tabular format i.e. in rows & column.
GridView_RowCreated & GridView_RowDataBound are the event that can be used to manipulate the each row,column or cell morever.

Here ,I added a gridview control and bound it products table from northwind database.

a)In the following listing I will set backcolor of each alternate row perticular color.

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
GridViewRow grow = e.Row;
if (grow.RowIndex % 2 == 0)
{
grow.BackColor = System.Drawing.Color.Cyan;
}
}

b) Here I am identifying the first column and modifying it’s back-color

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataControlFieldCell dcf = (DataControlFieldCell)e.Row.Cells[0];
string str = dcf.ContainingField.HeaderText;
dcf.BackColor = System.Drawing.Color.DarkKhaki;

}
c)Here I am identifying the cell from first row and checking if it’s contain perticular text I will modify it’s back-color

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
DataControlFieldCell dcf = (DataControlFieldCell)e.Row.Cells[0];
if (this.IsInteger(e.Row.Cells[0].Text))
{
if ((Convert.ToInt32(e.Row.Cells[0].Text) % 2) == 0)
{
e.Row.Cells[0].BackColor = System.Drawing.Color.DarkKhaki;
}
}
}

No comments:

Post a Comment