Click here to Skip to main content
15,312,540 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a datatable like below:

Name               Department
John Alter         D01
Prakash Kiran      D01
Steffy Cold        D01
Julia Roberts      D02
Candy Mitchel      D02
Monika Diesel      D03


I want it like below: 

Name               Department
John Alter         D01
Prakash Kiran      
Steffy Cold        
Julia Roberts      D02
Candy Mitchel      
Monika Diesel      D03


when its ordered by a department, it should not repeat for the same department.
But the row should be there, only if there is same value in next row under Department column, that needs to be BLANK

help ASAP


What I have tried:

Used the below code from someone.. but its not working


C#
DataTable newDt = dt.Clone();
for (int i = 0; i < dt.Rows.Count; i++)
{
    DataRow newRow = newDt.NewRow();
    newRow["Name"] = dt.Rows[i]["Name"];
    int originalIndex = dt.AsEnumerable().ToList().FindIndex(x => x["Department"] == dt.Rows[i]["Department"]);
    if (originalIndex < i)
    {
        newRow["Department"] = "";
    }
    else
    {
        newRow["Department"] = dt.Rows[i]["Department"];
    }
    newDt.Rows.Add(newRow);
}
Posted
Updated 1-Aug-17 23:29pm

try

DataTable dt = new DataTable();
           dt.Columns.Add("Name");
           dt.Columns.Add("Department");
           dt.Rows.Add("John Alter", "D01");
           dt.Rows.Add("Prakash Kiran", "D01");
           dt.Rows.Add("Steffy Cold", "D01");
           dt.Rows.Add("Julia Roberts", "D02");
           dt.Rows.Add("Candy Mitchel", "D02");
           dt.Rows.Add("Monika Diesel", "D03");
           var departments = dt.AsEnumerable().Select(k => k.Field<string>("Department")).Distinct().OrderBy(k=>k).ToArray();

           foreach (string dept in departments)
           {
               int i = 0;
               foreach (DataRow row in dt.Rows)
               {
                   if (row["Department"].ToString() == dept) {
                       if (i > 0) {
                           row["Department"] = "";
                       }
                       i++;
                   }
               }
           }
   
That code was mine and as we established in our comment discussion, replacing the == operator by the .Equals method works.

For future reference, here's why that worked:
The return type of x["Department"] and dt.Rows[i]["Department"] is object (and not string, because the table can contain other data as well). When == is used on object, this is the equivalent of calling Object.ReferenceEquals[^] - here it doesn't matter that the actual type is a string, Object.ReferenceEquals is still used (and that's what I forgot when writing the original code). If you use .Equals however, then it will use String.Equals[^] (which is what you want), because you have a string and the String type overrides Object.Equals.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900