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

    Row back color property changed but not showing on row.

    I have datagridview(dgv) in windows form application I need to set its back color to white in one method and i did it ,I cheked in debugging the expected row color changed but not showing in dgv ,what could be the reason behind this?

    here is the code, What I have tried:

    //////Row color changing here///////

    public void _timer_Tick(object sender, EventArgs e)
    {
    try
    {
    if (flagwithoutprocess == true)
    {
    Appendlblmflash("Indent Discarded , send another indent");

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    if (row.Index == indexxbox)
    {
    for (int i = 0; i <= row.Cells.Count - 1; i++)
    {
    row.Cells[i].Style.BackColor = Color.White;
    }
    }
    }
    flagwithoutprocess = false;
    }
    }
    }

    ////event where traced colors and change as expected///


    private void dataGridView1_CellContentClick(object sender,DataGridViewCellEventArgs e)
    {
    try
    {
    var senderGrid = (DataGridView)sender;

    ///summary///
    ///for checkbox column///

    if (senderGrid.Columns[e.ColumnIndex] is DataGridViewCheckBoxColumn && e.RowIndex >= 0)
    {

    indexxbox = e.RowIndex;

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {

    if (row.DefaultCellStyle.BackColor == Color.Aqua)
    {
    row.DefaultCellStyle.BackColor = Color.Aqua;
    }

    row.Cells[SerialNo.Name].Value = false;
    }

    if (dataGridView1.CurrentCell is DataGridViewCheckBoxCell)
    {

    //Below code for getting checked value of checkbox
    bool isChecked = (bool)dataGridView1[e.ColumnIndex, e.RowIndex].EditedFormattedValue;

    // Checking value of checkbox
    if (isChecked == true)
    {
    data = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
    fm = dataGridView1.Rows[e.RowIndex].Cells[3].Value.ToString();
    to = dataGridView1.Rows[e.RowIndex].Cells[4].Value.ToString();

    //changing color of selected row
    var r = this.dataGridView1.Rows[e.RowIndex];

    r.DefaultCellStyle.BackColor = Color.Aqua;
    }

    ifile.IniWriteValue("Config Section", "INDENT_NO", data);
    txtindentcode.Text = data;
    }
    }
    }
    }
  • #764800
    You have to change the color in "RowPrePaint" event
    Follow are the sample code for "RowPrePaint"

    private void MyGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e)
    {
    //Handle your code
    }

    By Nathan
    Direction is important than speed

  • #764805
    Found the solution I color row cell by cell everywhere and check cell by cell in for loop as follows ,

    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    for (int k = 0; k <= row.Cells.Count - 1; k++)
    {
    if (row.Cells[k].Style.BackColor == Color.Aqua)
    {
    for (int n = 0; n <= row.Cells.Count - 1; n++)
    {
    row.Cells[n].Style.BackColor = Color.White;
    }
    }
    }
    row.Cells[SerialNo.Name].Value = false;
    }

    this gives me expected result.

  • #764811
    Hi priyanka,

    You can do this in a single line with the help of LINQ as follows:
    dataGridView1.Rows.Cast<DataGridViewRow>().ToList().ForEach(f => f.DefaultCellStyle.BackColor = Color.Aqua);

    Hope it helps.
    Regards,
    Shashikant Gurav
    shashikantgurav22@gmail.com

  • #764822
    Hai Priyanka,
    You need to use DefaultCellStyle back color property instead:

    public void _timer_Tick(object sender, EventArgs e)
    {
    try
    {
    if (flagwithoutprocess == true)
    {
    Appendlblmflash("Indent Discarded , send another indent");
    foreach (DataGridViewRow row in dataGridView1.Rows)
    {
    if (row.Index == indexxbox)
    {
    for (int i = 0; i <= row.Cells.Count - 1; i++)
    {
    row.DefaultCellStyle.BackColor = Color.White;
    }
    }
    }
    flagwithoutprocess = false;
    }
    }
    }

    Hope it will be helpful to you.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #764848
    Check if any other event get called after cell_click event ? there is should be a possibility of another event execution after cell click
    Try to debugger on all event and check what is execute after cell click

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


Sign In to post your comments