'Formatting Excel sheet row color based on a cell value
i have a grid with column of orders numbers, with this columns i check excels file, if specific cell in the excel file is empty - the row will be red else is will be green. but i dont why everythis is getting the red color. this is my code:
GridView View = gridView2 as GridView;
for (int i = 0; i <= gridView2.RowCount - 1; i++)
{
string status = (string)View.GetRowCellValue(i, View.Columns[0]);
if (File.Exists(@"I:\OPTIMIZER\" + status + @"_הזמנת_רכש_חומרים.xls"))
{
//Create an excel application object
Microsoft.Office.Interop.Excel.Application excelAppObj = new Microsoft.Office.Interop.Excel.Application();
excelAppObj.DisplayAlerts = false;
//Open the excel work book
Microsoft.Office.Interop.Excel.Workbook workBook = excelAppObj.Workbooks.Open(@"I:\OPTIMIZER\" + status + @"_הזמנת_רכש_חומרים.xls", 0, false, 5, "", "", false,
Microsoft.Office.Interop.Excel.XlPlatform.xlWindows, "",
true, false, 0, false, false);
//Get the first sheet of the selected work book
Microsoft.Office.Interop.Excel.Worksheet worksheet = (Microsoft.Office.Interop.Excel.Worksheet)workBook.Worksheets.get_Item(1);
var cellValue = (string)(worksheet.Cells[10, 5] as Microsoft.Office.Interop.Excel.Range).Value;
if (cellValue != null)
{
View.Appearance.Row.BackColor = Color.LightGreen;
View.Appearance.Row.ForeColor = Color.Black;
}
else
{
View.Appearance.Row.BackColor = Color.IndianRed;
View.Appearance.Row.ForeColor = Color.White;
}
workBook.Close(false);
excelAppObj.Quit();
}
}
what am i doing wrong?
Solution 1:[1]
You are applying the color value to entire grid view.
Please use the specific ROW to which this cell belongs and then set that specific ROW's color property. You already have the specific ROW in which the cell value may be null. Use 'i' to get the specific row from grid and set the color.
Here you go...hopefully this works.
GridViewRow gvCurrentRow = (GridViewRow)gridView2.Controls[0].Controls[i];
if (cellValue != null)
{
gvCurrentRow.BackColor = //set it to whatever you want
gvCurrentRow.ForeColor = //set it to whatever you want
}
else
{
gvCurrentRow.BackColor = //set it to whatever you want
gvCurrentRow.ForeColor = //set it to whatever you want
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
