I have stuck in a weird problem... I have a girdview to display stock data. In this stock data I have to highlight NEGATIVE stock records in YELLOW.
I did some google search for this and found a solution, which worked fine when I set this form as default form of application through program.cs file. But whenever I invoke the form through a MASTER form (i.e. MDI Parent), code for changing row color does not work. I debugged the code and found that program go through the logic properly but does not change row color.
Below is the code which I have written...
private void LoadData()
getLotID_frmLotReport.LotID__frmLotReport = 0; string sql = "Select LotID, LotNumber as 'Lot Number', LotName as 'Lot Name', Size_ as Size, convert(varchar(10),LotCreateDate,105) as 'Create Date', round(TotalLotPurchased,2) as 'Lot Purchased', round(TotalLotSold,2) as 'Lot Sold', isnull(round(TotalLotPurchased,2),0)-isnull(round(TotalLotSold,2),0) as 'Total Stock' from dbo.Stock_Lot";
clsPurchase obj = new clsPurchase(); DataSet ds = obj.GetData(sql); try
if (ds.Tables.Rows.Count > 0)
BindingSource bsource = new BindingSource();
DataGridViewLinkColumn dl = new DataGridViewLinkColumn();
dl.DataPropertyName = "Lot Number";
dl.Name = "Lot Number";
bsource.DataSource = ds.Tables;
dataGridView1.DataSource = bsource;
this.dataGridView1.Columns["LotID"].Visible = false;
this.dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 9);
this.dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 9);
string RowIndex = "";
clsConnection objConn = new clsConnection();
string conn = objConn.ConnDB();
string sql1 = "Select isnull(round(TotalLotPurchased,1),0)-isnull(round(TotalLotSold,1),0) as 'Total Stock' from dbo.Stock_Lot";
SqlConnection connection = new SqlConnection(conn);
SqlCommand cmd = new SqlCommand(sql1, connection);
SqlDataReader rd = cmd.ExecuteReader();
int i = 0;
if (Convert.ToDouble(rd["Total Stock"]) <= 0)
RowIndex = RowIndex + i.ToString() + ",";
i = i + 1;
RowIndex = RowIndex.Remove(RowIndex.Length - 1);
RowIndex_arr = RowIndex.Split(',');
foreach (string s in RowIndex_arr)
if (s != "")
this.dataGridView1.Rows[Convert.ToInt32(s)].DefaultCellStyle.BackColor = Color.Yellow;
MessageBox.Show("No Data found!");
catch (Exception ex)
If possible try to run this code at your end by making this form default form as well as invoke through MASTER page, or provide me another alternative if u have already developed something like that. Please let me know if u don't understand anything from my code...
This task is imperative for the completion of project and I have already crossed a deadline. This is a show Stopper feature of application.
Any help would be GREATLY APPRECIATED!
Thanks and regards,