Click here to Skip to main content
14,667,947 members
Rate this:
Please Sign up or sign in to vote.
See more:
The Code is working fine if i generate some rows and then i input Text box Value ............. but first if i input Text box value and then generate rows. It do not perform action and the total value shows in totPenTxt.Text zero.

What I have tried:

private void penTxt_TextChanged(object sender, EventArgs e)
       {
           int sum = 0;
           for (int i = 0; i < metroGrid1.Rows.Count - 1; ++i)
           {
               sum += Convert.ToInt32(metroGrid1.Rows[i].Cells["quantity"].Value);
           }
           if (penTxt.Text == string.Empty && totPenTxt.Text != string.Empty)
           {
               totPenTxt.Text = Convert.ToInt32(0).ToString();
           }
           else
           {
               int value = Convert.ToInt32(penTxt.Text);

                   int result = value * sum;
                   totPenTxt.Text = result.ToString();

           }
Posted
Updated 4-Jan-18 4:45am

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

If there are no rows in the GridView then sum will be zero. You need to add a proper check for the number of rows and take a different action if there are none.

Also ...
        totPenTxt.Text = Convert.ToInt32(0).ToString();
// why not just:
        totPenTxt.Text = "0";
   
Comments
Member 13510001 4-Jan-18 11:28am
   
Dear thank u for helping me out can you please elaborate what should i do in this scenario if user First input textBox value and then he generates the rows as many as he wants. as user generate a row totPenTxt.text changes it value according to rows count.
Richard MacCutchan 5-Jan-18 4:22am
   
You just need to check if there are any rows at the beginning of the penTxt_TextChanged method. If the row count is zero then return from the method immediately.

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




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