比较数据网格视图和传递变量的值. [英] Compare values of Data Grid View and Pass in variable.

查看:126
本文介绍了比较数据网格视图和传递变量的值.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Windows应用程序中有2个相同形式的datagridview控件.
我编写了以下代码:

I have 2 datagridview controls in same form in windows application.
I have written the following code:

if (cbCust_Name.SelectedIndex >= 0)
            {
                foreach (DataGridViewRow dr in dgvResult.Rows)
                {
                    if (dr.Cells[2].Value != "")
                    {
                        com = dr.Cells[0].Value.ToString();
                        amd = Convert.ToDecimal(dr.Cells[2].Value);
                    }
                }
                foreach (DataGridViewRow drs in dgvCurr_Stk.Rows)
                {
                    ibm = drs.Cells[0].Value.ToString();
                    intel = Convert.ToDecimal(drs.Cells[1].Value);
                    if ((com == ibm) && (amd > intel)) //drs.Cells[1].Value != ""
                    {
                        string result = NoMessageBox.ShowBox("Sale quantity cannot  exceed Current Stock quantity.", "Customer Delivery");
                        foreach (DataGridViewRow drg in dgvResult.Rows)
                        {
                            dgvResult.CurrentRow.Cells[2].Value = "";
                            dgvResult.CurrentRow.Cells[3].Value = "";
                        }
                        if (dgvResult.CurrentRow.Cells[2].Value == "")
                        {
                            decimal a = Convert.ToDecimal(dgvResult.CurrentRow.Cells[1].Value);
                            decimal b = 0;
                            dgvResult.CurrentRow.Cells[3].Value = a * b;
                            decimal tot = 0;
                            for (int j = 0; j < dgvResult.Rows.Count; j++)
                            {
                                if (dgvResult.Rows[j].Cells[3].Value != "")
                                {
                                    tot += Convert.ToDecimal(dgvResult.Rows[j].Cells[3].Value);
                                }
                            }
                            dgvPayment.Rows[0].Cells[1].Value = tot.ToString();
                        }
                    }
                    else
                    {
                        if (dgvResult.CurrentRow.Cells[2].Value != "")
                        {
                            decimal a = Convert.ToDecimal(dgvResult.CurrentRow.Cells[1].Value);
                            decimal b = Convert.ToDecimal(dgvResult.CurrentRow.Cells[2].Value);
                            dgvResult.CurrentRow.Cells[3].Value = a * b;                            
                            decimal tot = 0;
                            for (int j = 0; j < dgvResult.Rows.Count; j++)
                            {
                                if (dgvResult.Rows[j].Cells[3].Value != "")
                                {
                                    tot += Convert.ToDecimal(dgvResult.Rows[j].Cells[3].Value);
                                }
                            }
                            dgvPayment.Rows[0].Cells[1].Value = tot.ToString();
                        }
                    }
                }
            }
            else
            {
                string result = NoMessageBox.ShowBox("Please select the Customer Name.", "Customer Delivery");
                clear();
            }



代码运行正常,但是在第一个datagridview控件(dgvResult)中,用户将输入该值,并将该值与另一个datagridview控件(dgvCurr_Stk)值进行比较,如果用户值大于另一个Datagridview Conrtol值,则会显示该消息:-(销售数量不能超过当前库存数量.",客户交货").

在第一个Foreach循环中,使用条件将用户值传递给变量



the Code is running fine, but in first datagridview control(dgvResult) user will enter the value and that value is compared with the another datagridview control(dgvCurr_Stk) value and if the user value is greater than another Datagridview Conrtol value then it shows the message:- "(Sale quantity cannot exceed Current Stock quantity.", "Customer Delivery")".

In first Foreach loop the user value is passed to variable using condition

if (dr.Cells[2].Value != "")
{
     com = dr.Cells[0].Value.ToString();
     amd = Convert.ToDecimal(dr.Cells[2].Value); 
}



如果第二列中的值不为空,则传递该值,然后循环继续进行,直到秒中的值不为空.例如datagridview中有5行,用户在第一行中输入值,然后在第三,第四行中输入,如果用户返回并在第二行中输入值,即使用户值大于另一个datagridview值,也不会传递该值并显示消息未显示,因为根据在第一个foreach循环中提到的条件,它将继续检查值是否不是空值,并且foreach循环一直持续到最后一行,当该值为空时,将传递最后一个值,并且不进行验证. br/>
我希望如果用户从第四行返回到第二行并输入值,则应该完成验证,这不会发生.

如有任何疑问,请让我知道:-

在此先感谢!!!



if the value is not empty in second column, the value is passed, and loop continues till the value in second is not empty. for eg. there are 5 rows in datagridview and user enters the value in first row and then in third, fourth and if user comes back and enters value in second row and even if user value is greater than another datagridview value then that value is not passed and message is not shown, because according to if condition mentioned in first foreach loop, it continues to check whether value is not emnpty and foreach loop continues till last row, when the value is empty then the last value is passed and validation is not done.

I want that if the user comes back from fourth row to second row and enter the value then the validation should be done which is not happening.

Any queries, plz let me know:-

Thanks in Advance !!!

推荐答案

我写了这段代码,而不是First Foreach循环:-

I wrote this code instead of First Foreach Loop :-

if (dgvResult.Rows[e.RowIndex].Cells[2].Value != "")
                {
                    com = dgvResult.Rows[e.RowIndex].Cells[0].Value.ToString();
                    amd = Convert.ToDecimal(dgvResult.Rows[e.RowIndex].Cells[2].Value.ToString());
                }



我的查询就解决了.



And my query is solved.


这篇关于比较数据网格视图和传递变量的值.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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