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

  • SEARCH
  • Home
  • 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 8646333
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:46:49+00:00 2026-06-12T12:46:49+00:00

I create a two column DataTable (id, name): DataTable table = new DataTable(); DataColumn

  • 0

I create a two column DataTable (id, name):

        DataTable table = new DataTable();
        DataColumn id = table.Columns.Add("id", typeof(int));
        id.AutoIncrement = true;
        id.AutoIncrementSeed = 1;
        id.AutoIncrementStep = 1;
        id.ReadOnly = true;                  
        table.Columns.Add("name", typeof(String));

The DataColumn named id is AutoIncrementing (e.g. identity).

Then I read a file, and add each line of the file as a row.

//for each line in file
table.Rows.Add(null,line);

Next, I sort the DataTable on the DataColumn named name using a DataView:

        DataView dv = table.DefaultView;           
        dv.Sort = "name";
        table = dv.ToTable();

Problem is, the id column gets sorted when I would prefer it not (basically I just want to sort the name column independently of the other column).

Only solution I could find was to have an intermediate step where I populate a List<string> with the file lines, then sort that, then populate the table. Wondering if there was a more elegant (simple) solution?

  • 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-12T12:46:50+00:00Added an answer on June 12, 2026 at 12:46 pm

    You can use Linq-To-DataTable:

    var orderedRows = table.AsEnumerable()
                           .OrderBy(r => r.Field<String>("name"));
    

    or, if you just want the names:

    var orderedNames = orderedRows.Select(r => r.Field<String>("name"));
    

    Remember to add using System.Linq;.

    Edit:

    Each name belongs to an id. However, I only care about the id,name
    key,value pair after name has been sorted. E.g. the first
    alphanumerically sorted name should be 1, and the next 2, and so on

    Then you need to add the rows after you’ve sorted the lines:

    var txtLines = File.ReadLines(path)
                       .OrderBy(l => l);
    
    foreach(String  line in txtLines)
        tables.Rows.Add(null, line);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Excel sheet with two columns and I need to create new
I have a very simple table with two columns, but has 4.5M rows. CREATE
First.. here are the two tables I've created (sans irrelevant columns).. CREATE TABLE users_history1
Is it possible to create a table with a column that combines two column
I'm trying to create a standard two-column form, where the first column is a
I have a state column in a dataframe and I want to create two
I want to create an array which has two columns. The output should contain
in MySQL, If I create a unique key on two columns, say UNIQUE KEY
I have two columns in my table and the rows are created dynamically. <table
I have created a table in Visual Studio. That table has two columns. First

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.