0

I have a gridview in winforms which displays bill details. There is a column named total. I created a label with a text "0". Then I used a for loop to get the sum of total column in labels text property.

decimal tot1 = 0;
for (int i = 0; i < dgv_SaleReport.RowCount - 1; i++)
{
    tot1 += Convert.ToDecimal(dgv_SaleReport.Rows[i].Cells[7].Value);
}
label_Total1SaleReport.Text = tot1.ToString();

But this code is not working. I added same code in other menu items datagridview and it's working. Right now, I have a tabcontrol which has 4 tabs. In all these 4 tabs dgv the code is not working. Can't just figure it out.

Sebi
  • 3,879
  • 2
  • 35
  • 62
Abhijith
  • 71
  • 8
  • Check dgv_SaleReport.Rows[i].Cells[7].Value – andy Jan 05 '17 at 06:39
  • Its right. Total value is displayed in that column. is it a bug? :3 – Abhijith Jan 05 '17 at 06:41
  • Try adding a breakpoint inside the loop so you can see the value of tot1 and the value of the dgv cell – Sjors Ottjes Jan 05 '17 at 06:42
  • The sentence "Everything is right" should be removed i think. Why you have a question than? ;-). Make sure you don't overwrite the Label.Text Value in some event. – Sebi Jan 05 '17 at 06:51
  • The code is in load event. It works on other forms load event. But not this :/ – Abhijith Jan 05 '17 at 06:59
  • @Abhijith Can you update your question and post the complete loadevent? We have to make sure that you don't overwrite the value later. Further debug and make sure that the tot1 Value is not 0. I would further recommend you to use the Form.Shown Event. Then you can be absolutly sure that all Gui Components are loaded. Maybe your Grid isn't filled up to the time you calculate your total value or sth. – Sebi Jan 05 '17 at 09:46
  • This could be the UI thread being "locked" when you load the event. Try adding the Application.DoEvents(); after assigned a value to your label. If it updates the label, then consider using a background worker or similar for processing the updating of UI component values since Application.DoEvents(); is consider a No No. Look at this article for basics on using background worker -> http://stackoverflow.com/questions/6481304/how-to-use-a-backgroundworker/30753411#30753411 – TheDanMan Jan 05 '17 at 12:26

0 Answers0