Click here to Skip to main content
11,637,002 members (69,114 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: .NET2.0 C#2.0 .NET3.5 , +
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();
            }

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

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 8-Aug-12 1:43am
Ank_ush2.9K

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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.
  Permalink  
Comments
Ank_ush at 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)

  Print Answers RSS
0 stibee 313
1 jyo.net 295
2 OriginalGriff 285
3 Suvendu Shekhar Giri 205
4 Sergey Alexandrovich Kryukov 194
0 OriginalGriff 8,941
1 Sergey Alexandrovich Kryukov 8,493
2 Mika Wendelius 6,909
3 F-ES Sitecore 2,388
4 Suvendu Shekhar Giri 2,320


Advertise | Privacy | Mobile
Web04 | 2.8.150728.1 | Last Updated 8 Aug 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100