none
Split Datatable based on column RRS feed

Answers

  • Hi Babak_bsn,

    You can do something like this with DataView.ToTable method.

    Dim dv As DataView = New DataView(yourDataTable)
    DataGridView1.DataSource = dv.ToTable("DataTableName", False, "ColumnName1", "ColumnName2")
    DataGridView2.DataSource = dv.ToTable("DataTableName", False, "ColumnName3", "ColumnName4", "ColumnName5")
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Babak_bsn Friday, July 2, 2010 6:46 AM
    Friday, July 2, 2010 4:04 AM
    Moderator

All replies

  • Please explain in more detail what you mean by split.  Perhaps with example tables.

     

    Thursday, July 1, 2010 12:50 AM
  • Hi Babak,

    Could you please explain more about what do you mean by "split" ? If you want to get just some of the columns of the original datatable into a new datatable, then you can use DataView.ToTable method, passing the column names as parameter of the method. More information about DataView.ToTable method please refer to :

    http://msdn.microsoft.com/en-us/library/h2b6ehaa.aspx

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    Thursday, July 1, 2010 9:22 AM
    Moderator
  • Thanks experts

    imagin, i have a Stored procedure in Database that returns many fields that can't show all of them in one Datagridview.i want to show some fields in first Datagridview and some of them in second one. i.e it returns Name,Lastname,Phone,City,Fathername ...... i want to show Name,last name in One datagridview and Phone,city,Fathername.... in another

    Thanks

    Thursday, July 1, 2010 6:48 PM
  • Here is the simplest way.  There might be ways that are more efficient, but based on what you described, this is likely to work well and be easy to understand.

    Once you get your DataTable back, call its dt.Copy method.

    Now process each copy of the DataTable.  Call dt.Columns.Remove("columnname") on every column that you do not want in that DataTable.  Remove half of the columns from the first and half of the columns from the second (keeping of course any columns you want to leave in common).  Depending on your needs, you might be able to loop through dt.Columns instead of specifying the names.  (If you do this with foreach, however, you'll need to observe the usual rules and workarounds for removing during an enumeration.)

     

    Thursday, July 1, 2010 11:44 PM
  • Hi Babak_bsn,

    You can do something like this with DataView.ToTable method.

    Dim dv As DataView = New DataView(yourDataTable)
    DataGridView1.DataSource = dv.ToTable("DataTableName", False, "ColumnName1", "ColumnName2")
    DataGridView2.DataSource = dv.ToTable("DataTableName", False, "ColumnName3", "ColumnName4", "ColumnName5")
    

    Best regards,
    Alex Liang


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
    • Marked as answer by Babak_bsn Friday, July 2, 2010 6:46 AM
    Friday, July 2, 2010 4:04 AM
    Moderator