Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8668059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T18:07:15+00:00 2026-06-12T18:07:15+00:00

I want to modify my table in a Datatable. I know that I have

  • 0

I want to modify my table in a Datatable. I know that I have to use linq and group the results.
Before:

ID     Name   LastName
1      Kiki    ha 
3      lola    mi 
2      ka      xe 
2      Kiki    ha

After:

Name  LastName   1   3   2
Kiki   ha        x       x
lola   mi            x
ka     xe                x

My original code:

    DataTable table1 = new DataTable("table"); 
    table1.Columns.Add("ID", typeof(String)); 
    table1.Columns.Add("Name", typeof(String)); 
    table1.Columns.Add("Lastname", typeof(String)); 

    object[] a1 = { 1, "Kiki", "ha" }; 
    object[] a2 = { 3, "lola", "mi" }; 
    object[] a4 = { 2, "ka", "xe" }; 
    object[] a5 = { 2, "kiki", "ha" };
    table1.Rows.Add(a1); 
    table1.Rows.Add(a2); 
    table1.Rows.Add(a4);        
    table1.Rows.Add(a5); 

I also tried this but it didn’t work:

    var result = from t1 in table1.AsEnumerable()
                 group t1 by new {ID = t1.Field<String>("ID")} into grp
                  select new
                     {
                         ID = grp.Key.ID,
                         //something must be there
                     };
     DataGridView1.DataSource = result.ToList();
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T18:07:16+00:00Added an answer on June 12, 2026 at 6:07 pm

    This should do what you need:

    var nameGroups = from row in table1.AsEnumerable()
                     group row by new
                     {
                         Name = row.Field<string>("Name").ToLower(),
                         LastName = row.Field<string>("Lastname").ToLower(),
                     } into NameGroups
                     select NameGroups;
    
    var tblOut = new DataTable();
    tblOut.Columns.Add("Name");
    tblOut.Columns.Add("LastName");
    var distinctIDs = table1.AsEnumerable()
                            .Select(r => r.Field<string>("ID"))
                            .Distinct();
    
    foreach (var id in distinctIDs)
        tblOut.Columns.Add(id);
    
    foreach (var grp in nameGroups)
    {
        var row = tblOut.Rows.Add();
        row.SetField<string>("Name", grp.Key.Name);
        row.SetField<string>("LastName", grp.Key.LastName);
        foreach (DataColumn idCol in tblOut.Columns.Cast<DataColumn>().Skip(2))
        {
            bool userHasID = grp.Any(r => r.Field<string>("ID") == idCol.ColumnName);
            row.SetField<string>(idCol, userHasID ? "x" : "");
        }
    }
    

    Note that i output the lowercase names because i needed to group by case insensitive.

    Edit: Here’s a screenshot of the DataTable in the debugger window:

    enter image description here

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hello I have a table that may have many rows. I want to modify
I have a DataTable where I want to modify the font size. The user
I have an application that shows some data in p:DataTable.... This table is accessible
I have a docx document that I want to modify via OpenXML SDK. This
I have a database table. I want the user to be able to modify
I want to modify my table view cell apparence when the delete button came
I want to run this on my table: ALTER TABLE table_name MODIFY col_name VARCHAR(255)
I want to modify (Remove all white spaces) all the values of Column Name
I want to modify a connection string that's hard-coded in a Java application (jar
I want to use a table valued function and call it from within multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.