I fill in the DataGrid from Excel, then I need to compare the filled DataGrid with the database, so as not to pull the database each time I created another temporary one, and in the process of checking, if the field in the second DataGrid missing, select it with a different color, and if there is, then see if they match I need the data in it, if something does not match, you need to paint the cell.
The problem is that in the field where you need to paint only the cells, and leave the rest as is, the code colors the cells correctly but also paints the line.
Here is my code, how to make it more efficient?
Button handler code:
dgvTemp.DataSource = exp.GetProduct(); dgvTemp.Columns["price"].DefaultCellStyle.Format = "N4"; string NeedSku = ""; for (int i = 0; i < exDgvData.Rows.Count; i++) { NeedSku = exDgvData.Rows[i].Cells["Код"].Value.ToString().Trim(); for (int k = 0; k < dgvTemp.Rows.Count; k++) { if (dgvTemp.Rows[k].Cells["sku"].Value.ToString().Trim() == NeedSku) { if (dgvTemp.Rows[k].Cells["price"].Value.ToString().Trim() != exDgvData.Rows[i].Cells["Цена"].Value.ToString().Trim()) { paint(i, "Цена", false); } if (dgvTemp.Rows[k].Cells["model"].Value.ToString().Trim() != exDgvData.Rows[i].Cells["Модель"].Value.ToString().Trim()) { paint(i, "Модель", false); } if (dgvTemp.Rows[k].Cells["quantity"].Value.ToString().Trim() != exDgvData.Rows[i].Cells["Количество"].Value.ToString().Trim()) { paint(i, "Количество", false); } } else paint(i, "", true); } } paint code:
private void paint(int RowsID, string ColumnName, bool All) { Color defultCellColor = exDgvData.DefaultCellStyle.BackColor; if (All) { exDgvData.Rows[RowsID].DefaultCellStyle.BackColor = Color.LightSalmon; } else { exDgvData.Rows[RowsID].DefaultCellStyle.BackColor = defultCellColor; exDgvData.Rows[RowsID].Cells[ColumnName].Style.BackColor = Color.Orange; } }