Click here to Skip to main content
16,015,559 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I need to highlight the rows for Edit,Select,Delete Events. My problem is if click select in 1st row means that row highlighted by yellow color and then i click edit in 3rd means 1st backs to backs to normal color and 3rd will get highlighted.But for Delete button if 1st row select by edit or select then after i click Delete in 4th row means both 2 rows get highlighted. when i click Delete button means i need to only display that particular row only highlighted And i am using Select,Edit in Gridview command buttons and Delete i am using image button . Kindly help me to do this. Thanks in advance

What I have tried:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{

GridView1.SelectedRow.BackColor = System.Drawing.Color.Yellow;
}
  private void ClearBackColor()
        {
            foreach (GridViewRow row1 in GridView1.Rows)
            {
                row1.BackColor = System.Drawing.Color.White;
            }
        }

        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
             ClearBackColor();
             GridView1.SelectedRow.BackColor = System.Drawing.Color.Yellow;
        }

 protected void GridView1_RowDataBound1(object sender, GridViewRowEventArgs e)
        {

            if (e.Row.RowType == DataControlRowType.DataRow)
            {
ImageButton l = (ImageButton)e.Row.FindControl("ImageButton1");
                l.Attributes.Add("OnClick", "this.originalcolor=this.style.backgroundColor;" + " this.parentNode.parentNode.style.backgroundColor='red';" +
                "if (confirm('Are you sure you want to delete this record " +
                DataBinder.Eval(e.Row.DataItem, "UKC_LEDGER_CODE") + "'))return true;else {this.parentNode.parentN
}
}
Posted
Updated 15-Sep-17 22:50pm

1 solution

See question here: .net - C# How to set a specific cell style for a complete row without constantly changing the DefaultCellStyle - Stack Overflow[^]

private void myDataGridView_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
{
    if (e.ColumnIndex == 1)
    {
        if (myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == "abc")
        {
            //e.CellStyle.BackColor = Color.Yellow;
            e.CellStyle.ForeColor = Color.Red;
        }
    }

    if (e.ColumnIndex == 4)
    {
        var check = myDataGridView.Rows[e.RowIndex].Cells[e.ColumnIndex].Value;

        if (check != null && (bool)check == true)
        {
            e.CellStyle.BackColor = Color.Gray;
            e.CellStyle.ForeColor = Color.Red;
        }
    }
}

For ASP.NET see: Changing the color of a row in a GridView in ASP.NET[^]
How to make a Gridview Row Color/ Cell Color/ Text Color « Devils Work[^]
 
Share this answer
 
v2
Comments
Dinesh Kumar 16-Sep-17 8:11am    
This works on window forms and i am using web forms. can you help how to do this on web forms

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