comparing data form datagridview c#

Category: c# language

Question

kaswin07 on Thu, 04 Oct 2012 03:08:38


  • thanks


Replies

Mitja Bonca on Thu, 04 Oct 2012 03:20:32


If your DGVs are not bind to some datasource (and even if they are), you can do a loop through both and check for conditions.

Example:

foreach(DataGridViewRow row1 in dataGridView1.Rows)
{
    foreach(DataGridViewRow row2 in dataGridViewNFC.Rows)
    {
        int id1 = int.Parse(row1.Cells["ID"].Value.ToString());
        int id2 = int.Parse(row2.Cells["ID"].Value.ToString());
        if(id1 == id2)
        {
            int inquiry = int.Parse(row2.Cells["Inquiry"].Value.ToString());
            //pass it to dgv3:
            //NOTE: make sure you have a column(s) in dgv3!!
            dataGridView2.Rows.Add();
            dataGridView2["Inquiry", dgv3.Rows.Count-1].Value = inquiry.ToString();            
        }
    }
}


Mitja


Mitja Bonca on Thu, 04 Oct 2012 05:19:52


change this code:

 foreach(DataGridView row2 in dataGridViewNFC.Rows)

to:

 foreach(DataGridViewRow row2 in dataGridViewNFC.Rows)

---

Check my upper post ones again, I edited it, so it suits to your needs. And btw, use my full code, with BOTH loops!



Mitja

Mitja Bonca on Thu, 04 Oct 2012 23:36:30


Since your dataGridViewNFC is databound, you must check the name of columns and which values are in there (the type of them). You must make sure there are integer in the Id Column, else you will get this exception you got ("Input string was not in correct format").

The same goes if Inquiry column!

One more thing, change this line of code to (since I see you named the columns in DGV2 as "No":

dataGridView2["No", dataGridView2.Rows.Count - 1].Value = inquiry.ToString();

Mitja Bonca on Thu, 04 Oct 2012 23:42:14


I cant, since I dont know your column names in datasourse (that you set to dgvNFC) and since I dont know what value you have in there.

Check it by your self, put a break point and by hovering a mouse over a dataSurce (dataset variable) check which is the correct column name and if there are ONLY numbers in that column!

I cannot do more for now!

Mitja Bonca on Fri, 05 Oct 2012 03:25:42


I would suggest you to do it a bit differently, to use column Indexes instrad of Colunm names, this way you will avoid some unwanted error, like finding column name when binding data to dgv.

One more thing, on your image, there is NO columns names in datagridview3! Just to mention (if there is none, the code bellow will fail again).

Ok, so lets do:

  foreach (DataGridViewRow row1 in dataGridView1.Rows)
            {
                foreach (DataGridViewRow row2 in dataGridView2.Rows)
                {
                    int id1 = int.Parse(row1.Cells[0].Value.ToString());
                    int id2 = int.Parse(row2.Cells[0].Value.ToString());
                    if (id1 == id2)
                    {
                        int inquiry = int.Parse(row2.Cells["Inquiry"].Value.ToString());
                        dataGridView3.Rows.Add();
                        dataGridView3["Inquiry", dataGridView3.Rows.Count - 1].Value = inquiry.ToString();
                    }
                }
            }

I hope we salve this issue right here,

bye

Mitja Bonca on Fri, 05 Oct 2012 04:44:20


Depends what kind of data you want to have there. Refering to some of your previous post, you said you want "Inquiry" column from DGV2, isn`t that so?

So add a new column and name it that way, you can add it before that foreach loop:

dataGridView3.Columns.Add("Inquiry", "Inquiry");
1st parameter is the column name (you refter to it when specifying the column name; when asigning value to particular column), 2nd parameter is the column header text (this is the name you actually see on top of that column).

Mitja Bonca on Fri, 05 Oct 2012 05:38:50


No problem, I am glad we salved the issue, and that you have learnt something in between :)

anytime mate,

bye

Lisa Zhu on Thu, 18 Oct 2012 02:32:52


Hi kaswin07,

Please don’t delete your posts. These posts will help others searching for solutions to a similar problem.

So, please undelete your posts and also restore the original question.

Thanks for your support and understanding.

Regards,