Click here to Skip to main content
13,764,207 members
Rate this:
 
Please Sign up or sign in to 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:

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 3 days ago
Updated 2hrs 5mins ago
v2
Comments
Richard MacCutchan 3 days ago
   
Why are you converting values to strings in order to convert them back to numbers?
Member 13894223 3 days ago
   
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
Vinod Jangle yesterday
   
what issue exactly you are facing?
Member 13894223 yesterday
   
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
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

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
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 3

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
  Permalink  

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.181113.4 | Last Updated 14 Nov 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100