Click here to Skip to main content
15,914,323 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I am trying to
get data from slq Database
Stock and Critical limit

what i want is ...if stock is less< than critical limit
those rows must show as Orange

and is stock == 0
rows Show As Crimson

What I have tried:

C#
listView2.Items.Clear();
listView2.Columns.Clear();
listView2.Columns.Add("ID", 100);
listView2.Columns.Add("Description", 150);
listView2.Columns.Add("Stock", 150);
listView2.Columns.Add("CriticalLimit", 150);

            string sql2 = @"Select * from tblProduct";
                cm = new SqlCommand(sql2, cn);
                dr = cm.ExecuteReader();
                while (dr.Read() == true)
                {

if ((Convert.ToInt64(dr[9]) <= Convert.ToInt32(dr[8].ToString()))&& Convert.ToInt64(dr[9]) >= 1)
               
 {

lst = listView2.Items.Add(dr[0].ToString());
lst.SubItems.Add(dr[1].ToString());
lst.SubItems.Add(dr[9].ToString());
lst.SubItems.Add(dr[8].ToString());

                    if (Convert.ToInt32(dr[9].ToString()) == 0)
                    {

                        lst.ForeColor = Color.Crimson;


                    }
                    else if (Convert.ToInt32(dr[9].ToString()) < Convert.ToInt32(dr[8].ToString()))
                    {
                        lst.ForeColor = Color.Orange;

                    }
                }
            }
            dr.Close();
Posted
Updated 13-Nov-18 22:07pm
v2
Comments
Richard MacCutchan 10-Nov-18 15:14pm    
Why are you converting values to strings in order to convert them back to numbers?
Member 13894223 10-Nov-18 15:22pm    
i need to check all items in the two columns if one is greater than the other
if column1(stock) is less than column two(Critical limit) then all rows that are less than will show as color text
im still very new to this im learning as i go so i might make stupid mistakes
[no name] 12-Nov-18 9:02am    
what issue exactly you are facing?
Member 13894223 12-Nov-18 11:00am    
in between all this the issue magically solved itself
but it gave a error that the string is invalid was something to do when i added the code to check if the value in stock, is less than critical limit to display that row as orange

Hi,

I don`t see any problem in your code. You just need to set the listView2.View to View.Details otherwise it will not show the sub-items and also make sure your conversations to string will work, that is all you need. The rest should work as you expect. However your code can be optimized. Leave a comment if it did not work then I will help you more.

Cheers,
AH
 
Share this answer
 
v2
Hi.
There is error in your conditional expression :
Convert.ToInt32(dr[9].ToString())
will never be checked as "== 0" because you have a condition ">= 1" behind.

Just do like this :

//if ((Convert.ToInt64(dr[9]) <= Convert.ToInt32(dr[8].ToString()))&& Convert.ToInt64(dr[9]) >= 1)
//{

lst = listView2.Items.Add(dr[0].ToString());
lst.SubItems.Add(dr[1].ToString());
lst.SubItems.Add(dr[9].ToString());
lst.SubItems.Add(dr[8].ToString());

        if (Convert.ToInt32(dr[9].ToString()) == 0)
        {

            lst.ForeColor = Color.Crimson;


        }
        else if (Convert.ToInt32(dr[9].ToString()) < Convert.ToInt32(dr[8].ToString()))
        {
            lst.ForeColor = Color.Orange;

        }
        
    }
//}



BR
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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