.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Post New Web Links

Sort datatable

Posted By:      Posted Date: October 22, 2010    Points: 0   Category :ASP.Net

How do I sort a data table based on a particular column which has integer values in ascending order?


I wrote this:

'Sort the datatable based on sequence id

leadtable.DefaultView.Sort ="Id"

But it doesnt seem to work

Datatble is defined as:

leadtable = New DataTable("LeadTable")

        Dim leadid As New DataColumn("Id")
        leadid.DataType = GetType(Integer)
        leadid.Unique = True
        leadid.AllowDBNull = False
        leadid.Caption = "Id"

        Dim leadname As New DataColumn("Name")
        leadname.DataType = GetType(System.String)
        leadname.AllowDBNull = False

        Dim leadsizeget As New DataColumn("Size")
        leadsizeget.DataType = GetType(Integer)
        leadsizeget.AllowDBNull = False

        leadtable.PrimaryKey = New DataColumn() {leadid}


View Complete Post

More Related Resource Links

Datatable sort in a particular order


How do I sort a data table based on a particular column which has integer values in ascending order?


I wrote this:

leadtable.DefaultView.Sort = "Id"

But it doesnt seem to work, it displays fine but when i get try to get the values of the sorted datatable in an array they dont come in an sorted ordere

Datatble is defined as:


  • leadtable = New DataTable("LeadTable")   
  •         Dim leadid As New DataColumn("Id")   
  •         leadi
  • How to sort GridView?

    In this article I will discuss how to sort columns in grid and more. At the end it will help answer some very frequently asked questions like following

    How to bind GridView and DropDownList controls using DataTable object

    In .net framework 2.0, you can load a DataTable in memory by consuming a data source using a DataReader. In this version you will find the flexibility in accessing the DataReader means write access code once and use again and again using DataReader. To see the functionality of DataTable consider an example that is created using VS2005.

    DataTable - Load from IDataReader - CreateDataReader - ReadXml WriteXml - Free ADO.NET Tutorials

    The DataTable in ADO.NET 2.0 has been seriously beefed up and I absolutely love it. Not everything we consume deals with multiple resultsets and requires the ever popular DataSet. With the cool new features in the ADO.NET 2.0 DataTable, we can get many of the cool features in the DataSet without all the extra baggage and memory footprint particular to multiple resultsets.

    Load DataTable from IDataReader

    Datatable in session


    I am creating a simple shopping cart using a datatable that is passed into a session object.  I have a small mini cart that displays the total number of items in the cart as well as the running total - this is displayed in a web control.  When the web control starts up I run a try/catch to see if the session object exists, if it doesn't I create the datatable for the cart and store it in the session.  I then have a datalist control that contains all the information for my products including an "add to cart" button.  When the page loads everything looks fine.  If I click on an "add to cart" button nothing happens to the session information.  If I click on it again, the session information updates - displaying only one item is in the cart and then counts up correctly there after.  Additionally, after adding an item, if I select a different item in the list, the same issue occurs.  I am not sure why this is happeing, if anyone can give me an idea I would appreciate it. 

    holding datatable value using session



    iam insert record using datable in gridivew,iam using  session to hold the record,

    there is any other way to hold the record in datable.


            DataRow row;
            row = dtempty.NewRow();
            row["ratetypeid"] = ddlratetype.SelectedValue;
            row["ratedesc"] = ddlratetype.SelectedItem.Text;
            row["slabs"] = ddlslabs.SelectedValue;   

    Sort by gridview SortExpression parameter via Stored Procedure


    I have a gridview that calls data via a stored procedure.  I am unable to enable the gridview columns to be sortable. I need to set the parameter in the Stored Procedure, can someone help me with this?

    Here is my gridview:

    <asp:GridView ID="AllUsersGrid" runat="server" AutoGenerateColumns="False" DataKeyNames="UserName"
                            GridLines="Vertical" Width="900px" DataSourceID="SqlDataSource1" AllowSorting="True"
                            SelectedRowStyle-Height="30px" CellPadding="4" BackColor="White">
                                <asp:TemplateField HeaderText="Full Name" SortExpression="lastname">
                                        <asp:Label ID="DisplayName" runat="server" Text='<%# Eval("firstname").ToString() & " " & Eval("lastname").ToString() %>' />
                                <asp:BoundField HeaderText="User Name" DataField="UserName" />

    removing item from datatable


    hi to all ..thanks in advance


    I have Two datatables dtA,dtB ,both contains column name is "FiledName" I need to remove item

    from dtA when same row data exists in dtB.  please tell me how to do it

    dtA    dtB   

     A        A

     B        F

     C        C

     D        Z

    I want o/p like this







    Bulk Copy From Datatable To SQL Server Table


     I have read the data of a excle file and captured the data into the dataset to a datatable, now that data is to be inserted into a SQL Server table using bulk copy option. I am using -

     public bool BulkEnterData(DataTable dt, string tblName)
            SqlBulkCopy bulk = new SqlBulkCopy(con);
            bulk.DestinationTableName = tblName;
            return true;

    ERROR Getting-
    A transport-level error has occurred when receiving results from the server. (provider: Shared Memory Provider, error: 0 - The pipe has been ended.) 

    How to resolve the above problem or any other solution is available.


    While Importing Excel 2007 file to Datatable - headerrow problem


    Hi there,


    I am trying to simply extract an excel data from an uploaded file an put it into a datatable. In this case the excel file has 3 rows but when I fill the datatable I only see row count of 2.

    I tried changing HDR:NO; to HDR:YES and vice versa, but no luck. 

    What am I doing wrong? (Note: the excel file cannot have a  headerrow)


    string connstr = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + pFilePath + ";Extended Properties=\"Excel 12.0;IMEX=1;HDR:NO;\"";
                OleDbConnection conn = new OleDbConnection(connstr);
                DataTable dtTables = conn.GetOleDbSchemaTable(System.Data.OleDb.OleDbSchemaGuid.Tables, null);
                string strTablename = dtTables.Rows[0]["TABLE_NAME"].ToString();
                string strSQL = "SELECT * FROM [" + strTablename + "]";
                OleDbCommand cmd = new OleDbCommand(strSQL, conn);
                DataTable dt = new DataTable();
                OleDbDataAdapter da = new OleDbDataAdapter(cmd);
                //At this point row count=2 which doesn't make sense




    Conversion of List to DataTable and ViceVersa


    hi ,

               Iam having a requirement where I wil get datatable or Dataview from the DataAccessLayer I need to convert them into GanaricList of entites and ViceVersa from GenericList to Datatable or DataView.

                 I tried it but I cant. Can anybody help me out.

    Data Points: DataSet and DataTable in ADO.NET 2.0


    ADO. NET 2. 0 sports some exciting enhancements to the core classes found in ADO. NET 1. x and introduces a variety of new classes, all of which promise to improve performance, flexibility, and efficiency.

    John Papa

    MSDN Magazine November 2005

    who to load DataRow to DataTable



    Who can I copy data from DataRow[] to DataTable....

    DataRow[] TempDataRowFilter;



    DataTable TempFilterTable = new DataTable();
    TempFilterTable.LoadDataRow(TempDataRowFilter, true);

    It is error why ?         

    Linq query to DataTable


    Is there option to convert from Linq.IQueryable to DataTable with some sort of CopyToDataTable() function. I am trying this.
         var row = from p in dc.ProductModels
                      select p;
        DataTable dt = row.CopyToDataTable();

    Is there option to convert from Linq.IQueryable to DataTable with some sort of CopyToDataTable() function. I am trying this.

         var row = from p in dc.ProductModels


    DataTable --> Oracle DB



    Can someone offer advice on the below? 

    I am connecting to an AR Server and pulling out data into a DataSet.  There will be 15 different DataTables within the set.  I would like to find a way to either insert the entire DataSet or each DataTable into an Oracle table.  The Oracle table has the same format as the DataTables I'm creating.

     private void button1_Click(object sender, EventArgs e)
                    //Get Credentials
                    ID = userNameTextBox.Text;
                    PW = passWordTextBox.Text;
                    connString = gstrConnectBaseLineStart + "UID=" + ID + ";PWD=" + PW + gstrConnectBaseLineEnd; //Connection string
                    conn = new OdbcConnection(connString); //Create Connection
                    conn.Open(); //Open Connection
                    Console.Write("Connection Successful" + "\n");
                    dataAdapter = new OdbcDataAdapter(SQL, conn);
                    dataAdapter.Fill(IncidentsDataSet, "dataTableTest");
                    DataTable dataTable = new DataTable();
                    dataTable = IncidentsDataSet.Tables["dataTableTest"];
                    dataGridView1.DataSource = dataTable;
                    //Would like to create som

    datatable select from ddl


    I have a datatable with 3 columns: id, AdGrp, Desc.  I bind it to a drop downlist that has id as the selected value and AdGrp as the selected text. I also have a label that i would like to display the Desc column value when ever the ddl selected index changes.  I was thinking of using  a select on the datatable with the selected value but not sure how i to do that.  Here is the code i have so far:

     string filter = "Id={0}";

    ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
    Sql Server  SharePoint  Silverlight  Others  All   

    Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend