I wanna compare datagrid rows and then make do something .
For Example ;
Number Name
90 ABC
80 XYZ
60 XXX
90 ABC
for (int i = 0; i < dataGridView1.Rows.Count; i++)
if (dataGridView1.Rows[i].Cells[0].Value == dataGridView1.Rows[i].Cells[0].Value)
{
//Do Something//
}
else
{
//Do Something//
}
}
}
}
Sivaraman DhamodaranRank #40 · 34.5K reputationPosted May 21, 2014, 11:11 PM EDT
Demir Acar94 reputationPosted May 21, 2014, 11:15 PM EDT
if u add this line you dont take that error .
for (int i = 0; i < dataGridView1.Rows.Count; i++)
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 11:16 PM EDT
Birol I think
dataGridView1.Rows[i].Cells[0].Value == dataGridView1.Rows[i].Cells[0].Value
should be replaced with
dataGridView1.Rows[i].Cells[0].Value == dataGridView2.Rows[i].Cells[0].Value // grid which you want to compare.
Demir Acar94 reputationPosted May 21, 2014, 11:28 PM EDT
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 11:37 PM EDT
please elaborate ?
Demir Acar94 reputationPosted May 21, 2014, 11:41 PM EDT
if (dataGridView1.Rows[i].Cells[0].Value == dataGridView1.Rows[i].Cells[0].Value)
Anupam SinghRank #643 · 3.7K reputationPosted May 21, 2014, 11:54 PM EDT
then try
this is not tested. so please refactor before using.
Demir Acar94 reputationPosted May 22, 2014, 2:18 AM EDT
if my grid has a this value
Number
90
90
or
Number
90
80
this code is working .
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
for (int ii = i + 1; ii < dataGridView1.Rows.Count; ii++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[ii].Cells[0].Value.ToString())
{
MessageBox.Show("Test");
}
else
{
MessageBox.Show("mest");
}
}
}
}
if my grid has a this value ;
Number
90
80
70
80
90
80
above code in looped and not working .
Anupam SinghRank #643 · 3.7K reputationPosted May 22, 2014, 2:39 AM EDT
then try :
you must be binding grid for datatable or dataset.
try to make copy of it and then you have to compare using 2 for loops
datatable1 t1=new datatable1();// fill data in table
datatable2 t2=t1.Copy();
for(int i = 0; i < datatable1.Rows.Count; i++)
{
for(int j= 0; j < datatable2.Rows.Count; j++)
{
if(datatable1.Rows[i][0]. ==datatable2.Rows[j][0])
{
// do your work..
}
}
}Demir Acar94 reputationPosted May 22, 2014, 3:02 AM EDT
Actually I am reading value from Excel and write to it to XML therefore I can't use datatable or dataset My XML tags is different .
Maybe someone did it before .
Anupam SinghRank #643 · 3.7K reputationPosted May 22, 2014, 3:12 AM EDT
Demir Acar94 reputationPosted May 22, 2014, 3:18 AM EDT
xmlDocument.Formatting = Formatting.Indented;
xmlDocument.Indentation = 5;
xmlDocument.WriteStartDocument();
xmlDocument.WriteStartElement("SALES_INVOICES");
for (int i = 0; i < dataGridView1.Rows.Count; i++)
{ xmlDocument.WriteElementString("INVOICE"); xmlDocument.WriteElementString("TYPE", dataGridView1.Rows[i].Cells[0].Value.ToString()); xmlDocument.WriteEndElement();
}
}
xmlDocument.WriteEndElement(); xmlDocument.Close();
MessageBox.Show("Xml Created."); }
Anupam SinghRank #643 · 3.7K reputationPosted May 22, 2014, 3:29 AM EDT
Demir Acar94 reputationPosted May 22, 2014, 3:36 AM EDT
Actullay one way can be I want ;
if my grid has a this value
Number
90
90
or
Number
90
80
this code is working .
for (int i = 0; i < dataGridView1.Rows.Count - 1; i++) {
for (int ii = i + 1; ii < dataGridView1.Rows.Count; ii++)
{
if (dataGridView1.Rows[i].Cells[0].Value.ToString() == dataGridView1.Rows[ii].Cells[0].Value.ToString())
{
MessageBox.Show("Test");
}
else
{
MessageBox.Show("mest");
}
}
}
}
if my grid has a this value ;
Number
90
80
70
80
90
80
above code in looped and not working .
Amit Thorat36 reputationPosted Jun 18, 2014, 2:25 AM EDT
If you are looking to remove rows with duplicate entries you can try something like this
foreach (currentrow=0;currentrow
var rowtoComp = datagridview1.rows[currentrow];
foreach(var row in datagridview1.rows)
if (rowtoComp.equls(row)
{
// Your Logic.
}
}