Click here to Skip to main content
15,896,063 members

change color of specific row of gridview in windows application developed in c#

sam7one asked:

Open original thread
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...

C#
private void LoadData()
        {
            getLotID_frmLotReport.LotID__frmLotReport = 0; //Table unique ID to fetch records
            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(); // Class to execute select query
            DataSet ds = obj.GetData(sql); // function in above class to execute query & get data in dataset
            try
            {
                if (ds.Tables[0].Rows.Count > 0)
                {
                    BindingSource bsource = new BindingSource();

                    DataGridViewLinkColumn dl = new DataGridViewLinkColumn();
                    dl.DataPropertyName = "Lot Number";
                    dl.Name = "Lot Number";
                    dataGridView1.Columns.Add(dl);
                    //this.dataGridView1.Columns["Lot Number"].SortMode = DataGridViewColumnSortMode.Automatic;
                    //this.dataGridView1.Columns["Lot Name"].SortMode=DataGridViewColumnSortMode.NotSortable;
                    bsource.DataSource = ds.Tables[0];
                    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);

                   
//***************** NOW BELOW IS THE CODE TO CHANGE COLOR OF SPECIFIC ROWS TO YELLOW ********//
//***************** THIS CODE WORKS IF THIS FORM IS DEFAULT FORM AND NOT INVOKED THROUGH master page **************** //                    
                    string RowIndex = "";
                    string[] RowIndex_arr;
                    
                    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);
                    connection.Open();
                    SqlCommand cmd = new SqlCommand(sql1, connection);
                    SqlDataReader rd = cmd.ExecuteReader();
                    int i = 0;
                    if (rd.HasRows)
                    {
                        while (rd.Read())
                        {
                            if (Convert.ToDouble(rd["Total Stock"]) <= 0)
                            {
                                RowIndex = RowIndex + i.ToString() + ",";
                            }
                            i = i + 1;
                        }
                    }
                    connection.Close();
                    connection.Dispose();
                    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;
                        }
                    }
                    
                }
                else
                {
                    MessageBox.Show("No Data found!");
                    this.Close();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }

        }


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,

SAM D
Tags: C#, Gridview, Color, Application

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900