Click here to Skip to main content
15,895,256 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Every Time i input some integer value in DataGridView TextBox which is column number 2 with the name "quantity". it returns 0 in another textBox

What I have tried:

private void metroGrid1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            int val = 0;
            foreach (DataGridViewRow row in metroGrid1.Rows)
            {
                val = Convert.ToInt32(row.Cells["quantity"].Value);
            }
            totPenTxt.Text = val.ToString();
}
Posted
Updated 8-Nov-17 1:43am
Comments
F-ES Sitecore 8-Nov-17 7:29am    
That's a statement, not a question.

1 solution

You are going through the entire DataGridView so you are probably just picking up the last "Row" which is where the user can add a row.

Try stepping through your code to confirm this, or set val to a strange number at the start of the loop to prove the point e.g.
C#
private void metroGrid1_RowValidating(object sender, DataGridViewCellCancelEventArgs e)
        {
            int val = -99;
            foreach (DataGridViewRow row in metroGrid1.Rows)
            {
                val = Convert.ToInt32(row.Cells["quantity"].Value);
            }
            totPenTxt.Text = val.ToString();
}

Learn how to debug your own code by reading this article - Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
 
Share this answer
 

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