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//
}
}
}
}
Amit ThoratPosted Jun 18, 2014, 6:25 AM
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.
}
}
Demir AcarPosted May 22, 2014, 7:36 AM
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 .
Anupam SinghPosted May 22, 2014, 7:29 AM
Demir AcarPosted May 22, 2014, 7:18 AM
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 SinghPosted May 22, 2014, 7:12 AM
Demir AcarPosted May 22, 2014, 7:02 AM
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 SinghPosted May 22, 2014, 6:39 AM
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 AcarPosted May 22, 2014, 6:18 AM
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 SinghPosted May 22, 2014, 3:54 AM
then try
this is not tested. so please refactor before using.
Demir AcarPosted May 22, 2014, 3:41 AM
if (dataGridView1.Rows[i].Cells[0].Value == dataGridView1.Rows[i].Cells[0].Value)
Anupam SinghPosted May 22, 2014, 3:37 AM
please elaborate ?
Demir AcarPosted May 22, 2014, 3:28 AM
Anupam SinghPosted May 22, 2014, 3:16 AM
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 AcarPosted May 22, 2014, 3:15 AM
if u add this line you dont take that error .
for (int i = 0; i < dataGridView1.Rows.Count; i++)
Sivaraman DhamodaranPosted May 22, 2014, 3:11 AM