Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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 ...
C#
        totPenTxt.Text = Convert.ToInt32(0).ToString();
// why not just:
        totPenTxt.Text = "0";
 
Share this answer
 
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, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900