Fill number for duplicate data in datagridview C# -
i want fill number duplicate data in datagridview. if duplicate fill 1.1 1.(count duplicate) else don't duplicate fill 1.1. use code below it's work faile.
int count = 0; (int = 0; <= datagridview3.rowcount - 2; i++) { string abc = datagridview3.rows[i].cells[2].value.tostring() + "" + datagridview3.rows[i].cells[3].value.tostring(); (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 check me.
there couple of things wrong code. not re-setting count within loop, , (if understand correctly), need check previous values in datagridview (ie j < i).
so code should this:
int count; datagridview3.rows[0].cells[1].value = "1.1"; (int = 1; <= datagridview3.rowcount - 1; i++) { count = 1; string abc = datagridview3.rows[i].cells[2].value.tostring() + "" + datagridview3.rows[i].cells[3].value.tostring(); (int j = 0; j < i; j++) { string def = datagridview3.rows[j].cells[2].value.tostring() + "" + datagridview3.rows[j].cells[3].value.tostring(); if (abc == def) { count++; } } datagridview3.rows[i].cells["wbs"].value = "1." + count.tostring(); }
this work regardless of data order. if data ordered on csis , bss, can use:
int count = 1; datagridview3.rows[0].cells[1].value = "1.1"; (int = 1; <= datagridview3.rowcount - 1; i++) { if (datagridview3.rows[i].cells[2].value == datagridview3.rows[i - 1].cells[2].value && datagridview3.rows[i].cells[3].value == datagridview3.rows[i - 1].cells[3].value) { count++; } else { count = 1; } datagridview3.rows[i].cells["wbs"].value = "1." + count.tostring(); }
in case, because comparing 1 line predecessor more efficient not store variables in new variable, compare them directly. storing separate variable has speed gain, when saving compiled code trouble of de-referencing same value multiple times.
Comments
Post a Comment