Fill number for duplicate data in datagridview C#

Home / Uncategorized / Fill number for duplicate data in datagridview C#

Question:
I want to fill number for duplicate data in dataGridView. If duplicate i will fill from 1.1 to 1.(count duplicate) else don’t duplicate i fill 1.1. But i use code as below it’s work faile.
int count = 0;

for (int i = 0; i <= dataGridView3.RowCount - 2; i++) { string abc = dataGridView3.Rows[i].Cells[2].Value.ToString() + "" + dataGridView3.Rows[i].Cells[3].Value.ToString(); for (int j = 1; j <= dataGridView3.RowCount - 2;j++ ) { string def = dataGridView3.Rows[j].Cells[2].Value.ToString() + "" + dataGridView3.Rows[j].Cells[3].Value.ToString(); dataGridView3.Rows[0].Cells[1].Value = "1.1"; if (abc == def) { count = count + 1; dataGridView3.Rows[j].Cells["Wbs"].Value = "1." + "" + (count); } } } I hope everyone will check help me.


Answer:
If the list is ordered for CSIS and BSS you just need to check the row before with the actual row to determine the Wbs.

The trick here is to group before labelling and then just check the one before with the one after.

I think you can reason better if you use poco object before handling them at datagrid view, here some not tested code, but you can use more or less the same code on Rows and Cells.
class Data {
String Wbs { get; set; }
String Csis { get; set; }
String Bss { get; set; }
String CsisAndBss() => Csis + Bss;
}

var list = new Data { … }; // objects

list.OrderBy( … ); // Csis then Bss

for(int i = 0; i < list.Count; list++){ var actual = list[i]; var before = list[i-1]; // check list boundaries if(i==0 || before.CsisAndBss != actual.CsisAndBss){ actual.Wbs == "1.1"; continue; } var values = before.Wbs.Split("."); actual.Wbs = $"{values[0]}.{Int32.Parse(values[1]+1)}"; } // here pass data to the grid Read more

Leave a Reply

Your email address will not be published. Required fields are marked *