Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I build a Work management software and am having a small problem as follows:

I have a label "Hard work" display and 1 Data_Gird_view , I want to show the form if there is line >= 30 in column "Total Time" then label "Hard work" +1

If there are 4 lines >= 30 then label "Hard work" +4

But I don't know which string will compare an integer with a column in Data_Gird_view. Everyone please help or suggest me

Please see the picture below to understand what I mean


https://ibb.co/qJLQ1fh
As shown in the picture above. in column "Total Time" I have 4 rows greater than 30. so I want label HarkWork = 4


What I have tried:

<pre>db.fillDataGridView("select * from [tbl_RequestSMT]", dataGridView1);
            
            IEnumerable<DataGridViewRow> rows = dataGridView1.Rows

         .Cast<DataGridViewRow>()
         .Where(r => r.Cells["TotaTime"].Value.ToString().Equals("30"));


            if (rows.Count() == 0)
            {
                // Not Found .
                
            }

            else
            {
                // Found
                // if label "Hard Work ++"  but i don't know code here :(

                
            }
Posted
Updated 23-Oct-20 10:25am
v3
Comments
BillWoodruff 23-Oct-20 0:35am    
the picture link does not work
Sơ Mi Tv 23-Oct-20 14:14pm    
You can copy and paste new tag browser

Below source code use in project. please see and help me fix it:
Images:
https://ibb.co/ssP8VqG
https://ibb.co/dGjFFwp
(Please copy and paste new tag browse)


public void BtnSearch_Click(object sender, EventArgs e)

       {
           db = new DbConnector();
           lbTotal.Text = "00";

           db.fillDataGridView("select *from tbl_WorkLoad where TimeComplete Between'" + dateTimePicker1.Value.ToString("dd-MM-yy| HH:mm:tt") + "' and '" + dateTimePicker2.Value.ToString("dd-MM-yy| HH:mm:tt") + "'", dataGridView1);
           const string HardWorkLabelText = "Hard Work Count: {0}";
           const int HardWorkThreshold = 30;

           try
           {
               IEnumerable<DataGridViewRow> rows = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => ((Int32)r.Cells["TotalTime"].Value) >= HardWorkThreshold);

               lbHardwork.Text = string.Format(HardWorkLabelText, rows.Count());

               {
                   for (int i = 0; i < dataGridView1.Rows.Count; i++)
                   {
                       lbTotaltime.Text = (Convert.ToString(double.Parse(lbTotaltime.Text) + double.Parse(dataGridView1.Rows[i].Cells[7].Value.ToString())));

                   }

                   {
                       int iCount = 0;
                       for (int i = 0; i <= dataGridView1.RowCount - 1; i++)
                       {
                           if (dataGridView1.Rows[i].Cells[0].Value != null)
                           {
                               //if ((bool)dataGridView1.Rows[i].Cells[0].Value)
                               //{

                               //}
                               iCount++;
                               lbTotal.Text = iCount.ToString();
                           }
                           else continue;
                       }

                   }
               }
           }
           catch (Exception ex)
           {

               MessageBox.Show(ex.Message.ToString());
           }
       }
 
Share this answer
 
v2
Comments
BillWoodruff 23-Oct-20 21:10pm    
I'd like to help you, but, I've probably done as much as I can. Suggestion:

1. update your original post with the information in this post, then delete this post: this post is not a solution.

2. put a breakpoint at the start of the method, then single-step (F11) thought the code: examine the values at each step looking for unexpected results. identify exactly where the error is thrown.

3. define constants outside a method body, at the class level. what you do here will not throw an error ... imho, it should ! ... but, it's a very bad practice.

4. verify that the data in the TotalTime column really is a "normal" string that represents only integer values.
 // assume the Label is named 'lblHardWorkCount

const string HardWorkLabelText = "Hard Work Count: {0}";
const int HardWorkThreshold = 30;

// to be executed in some method or event handler:

IEnumerable<DataGridViewRow> rows = dataGridView1.Rows.Cast<DataGridViewRow>().Where(r => ((Int32) r.Cells["TotalTime"].Value) >= HardWorkThreshold);

lblHardWorkCount.Text = string.Format(HardWorkLabelText, rows.Count());
Note: I suggest you wrap this in a try-catch block while developing in case you have bad data.
 
Share this answer
 
v3
Comments
Sơ Mi Tv 23-Oct-20 9:29am    
I've try code but not run. errors appear:
'System.InvalidCastException: 'Specified cast is not valid.'

I try breackpoint > IEnumerable<datagridviewrow> rows = dataGridView1.Rows.Cast<datagridviewrow>().Where(r => ((int)r.Cells["TotalTime"].Value) == HardWorkThreshold);


results return: rows null
BillWoodruff 23-Oct-20 9:47am    
the code you show is not the same as the code i posted: i verified that code running against a DataGridView with a Column of Type Int32.

Is the Column named "Total Time," or "TotalTime" ?
Sơ Mi Tv 23-Oct-20 9:54am    
Columns in my Datagirdview name "TotalTime" or [07]. I tried 2 name but not run
Sơ Mi Tv 23-Oct-20 10:00am    
I also tried your code but the results are still the same
BillWoodruff 23-Oct-20 10:43am    
then you need to examine the structure of your DataGridView carefully. do you have an integer column named "TotalTime" ?

put breakpoints in your code and single step through it: identify exactly where the error occurs.

i guarantee you the code i posted works !
Just show the "total lines" and the number / index of the current / selected line; e.g. "15 of 34". (If the user resizes the screen, your calcs go off anyway).
 
Share this answer
 
Comments
Sơ Mi Tv 22-Oct-20 18:15pm    
I have label total lines already. But I need show difficult work in other label (if on columns Total Time have columns >= 30, labels "Hard Work ++")

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