如何从另一个文本框中的datagridviewrows文本框中获取值 [英] How do I get the value from a textbox of datagridviewrows in another textbox

查看:92
本文介绍了如何从另一个文本框中的datagridviewrows文本框中获取值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我在DataGridView TextBox中输入一些整数值,它是第2列,名称为quantity。它在另一个文本框中返回0



我尝试过:



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();
}

推荐答案

您正在浏览整个DataGridView,因此您可能只是拿起最后一个行,这是用户可以在哪里添加一行。



尝试单步执行代码来确认这一点,或者在循环开始时将val设置为一个奇怪的数字来证明这一点例如

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.
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();
}



通过阅读本文了解如何调试自己的代码 - 掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]


这篇关于如何从另一个文本框中的datagridviewrows文本框中获取值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆