Click here to Skip to main content
15,909,737 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
I have 2 datagridview controls in same form in windows application.
I have written the following code:
C#
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();
            }


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
C#
if (dr.Cells[2].Value != "")
{
     com = dr.Cells[0].Value.ToString();
     amd = Convert.ToDecimal(dr.Cells[2].Value); 
}


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 !!!
Posted

1 solution

I wrote this code instead of First Foreach Loop :-

C#
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.
 
Share this answer
 
Comments
Ank_ush 15-Aug-12 11:13am    
If You find this solution Correct then plz vote for this Solution.

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