Hi @Padmanabhan, Venkatesh ,welcome to Microsoft Q&A.
You could directly add a null value directly from the second line when creating it.
You could create a HashSet<string>. Start traversing from the first row, if the current row value is not in it, keep it and save it in HashSet; if it exists, make it empty.
The specific code is as follows:
DataTable dt = new DataTable();
dt.Columns.Add("col1");
dt.Columns.Add("col2");
dt.Columns.Add("col3");
dt.Rows.Add("Region", "Cell 2", "XXXX 3");
dt.Rows.Add("Region", "Cell 5", "XXXX 4");
dt.Rows.Add("Region", "Cell 6", "XXXX 7");
dt.Rows.Add("Green", "cell5", "XXXX 8");
dt.Rows.Add("Green", "cell7", "XXXX 9");
HashSet<string> seenCol1Values = new HashSet<string>();
for (int i = 0; i <= dt.Rows.Count - 1; i++)
{
DataRow row = dt.Rows[i];
string col1Value = row["col1"].ToString();
if (seenCol1Values.Contains(col1Value))
{
row["col1"] = DBNull.Value;
}
else
{
seenCol1Values.Add(col1Value);
}
}
dataGridView1.DataSource = dt;
Best Regards,
Jiale
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.