Click here to Skip to main content
14,690,956 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to change the particular cell color in gridview based on if condition
Posted
Updated 8-Jul-18 19:20pm
Comments
ChauhanAjay 8-Sep-14 1:47am
   
Try this link
http://www.aspsnippets.com/Articles/Dynamically-change-GridView-Row-Background-Color-based-on-condition-in-ASPNet-using-C-and-VBNet.aspx

Hope it helps

you can do this stuff using gridview RawDataBound event

protected void grdDoc _RowDataBound(object sender, GridViewRowEventArgs e)
{
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if ((string.IsNullOrEmpty(e.Row.Cells[3].Text) != true)
                  || (e.Row.Cells[3].Text != " "))
            {
                string result = Convert.ToInt32(e.Row.Cells[2].Text);
                if (result == "M.S")
                    e.Row.Cells[3].BackColor= System.Drawing.Color. Aquamarine;
                else
                    e.Row.Cells[3].BackColor= System.Drawing.Color. BlanchedAlmond;
            }
        }
    }
}

prefer to view original article on CodeProject
Changing the color of a row in a GridView in ASP.NET[^]
   
1.In the case of ASP.NET projects, in the grid view's RowDataBound event you could do it like in the next example:
protected void _yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                if (e.Row.Cells[0].Text == "someValue") //Here is the condition!
                {
                    //   
                    //Change the cell color.
                    e.Row.Cells[0].ForeColor = System.Drawing.Color.Red;
                    //
                    //Change the back color.
                    e.Row.Cells[0].BackColor = Color.Yellow;
                    
                }
            }
        }

2.In the case of Windows Forms you should use DataGridView objects, and to change the colors you have to do it by using DataBindingComplete event like in the next example:
private void _yourDataGridView_DataBindingComplete(object sender,
    DataGridViewBindingCompleteEventArgs e)
{
 //...
 //
if(condition) //Your condition!
{
_yourDataGridView.Item(0, 1).Style.ForeColor = Color.Red;
_yourDataGridView.Item(0, 1).Style.BackColor = Color.Yellow;
}
}
   
v2
Comments
Member 11065510 8-Sep-14 1:55am
   
sorry i am new to .net and i think Rowdatabound event is in asp.net ,but i want this code in c#.net win form.............thanks
Raul Iloc 8-Sep-14 2:14am
   
1."GridView" objects are used in ASP.NET application and in Windows Forms "DataGridView" objects.
2.See my solution update (2nd point)!
I normally use the CellFormatting event. It's fired everytime a row is showed in the Datagrid. You've got an example here[^]
   
https://www.aspsnippets.com/demos/1082/
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900