You must Sign In to post a response.
  • Category: .NET

    How to change color of row by which have specific value in 2nd column?

    I have dataGridView in which last column is buttoncolumn and by presssing this button row color changed to red . but i want to change row color to white which is already red , whose cell1 has specific value say "abc". by applying (string str) ; if (str == abc) then change color. and i receive one character from client in another method say "$" from there i want to change color . how to do that ?
    i provide my code for better understanding.

    thanks,
    public class form1()
    {
    static int index; /// this object gets value from datagridview event.

    public void getdata()
    {
    case "$":
    if (res.StartsWith("$"))
    {
    DataGridViewRow row = new DataGridViewRow();
    string d = ifile.IniReadValue("Config Section", "INDENT_NO");


    foreach (DataGridViewRow rw in dataGridView1.Rows)
    {
    if (dataGridView1.Rows[indexx].Cells[1].Value.ToString() == d)
    {
    for (int i = 0; i <= rw.Cells.Count - 1; i++)
    {
    rw.Cells[i].Style.BackColor = Color.White;
    }
    }
    }
    }
    }
    else { }
    break;
    }
    }
  • #763537
    You need to change row color on row button clicked right ? There no direct button clicked event exist for gridview button you need to use cell click event and check if the button is clicked
    check below code
    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
    var senderGrid = (DataGridView)sender;

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewButtonColumn &&
    e.RowIndex >= 0)
    {
    //TODO - Button Clicked - Execute Code Here
    //get specific cell and change row color
    if( gridview1.Rows[e.RowIndex].cells[1].text == "test")
    gridview1.Rows[e.RowIndex].backColor = color.White;
    }
    }

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #763539
    If you want to do it in the server side you can do the following in the event "GridView1_DataBound"

    protected void dataGridView1_DataBound(object sender, EventArgs e)
    {
    for (int i =0 ; i <= dataGridView1.Rows.Count -1 ;i++)
    {
    // Implement your logic
    dataGridView1.Rows[i].Cells[6].BackColor = Color.Green;

    }
    }

    By Nathan
    Direction is important than speed


Sign In to post your comments