Help with datagridviews?

This is a discussion on Help with datagridviews? within the C# Programming forums, part of the General Programming Boards category; I have a datagridview in my windows application. The user can click to sort columns and can move them around. ...

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    14

    Help with datagridviews?

    I have a datagridview in my windows application. The user can click to sort columns and can move them around.

    How do I obtain a datable (or similar container) with data in the same order?
    Thanks.

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Try
    Code:
    DataTable table = yourDataGridView.DataSource as DataTable;
    If that doesn't work you can find what the actual type of DataSource is. Then cast/as it on that and get that.
    Code:
    DataView view = yourDataGridView.DataSource as DataView;
    DataTable table = view.Table as DataTable; //not sure but something similar

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    Thanks for your response.
    While this gives me the right data, it does not have it in the same order as the data grid view, if the user decides to rearrange the columns.
    Is there anyway to account for this?

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    DataGridViewColumnCollection.itemssorted has what I need but is private. Any thoughts ((

  5. #5
    Registered User
    Join Date
    Jul 2010
    Posts
    14
    Aha! This is what I did: I iterated through the Columns and used their DisplayIndex property to populate a list.
    Last edited by euroazn; 08-18-2010 at 12:24 PM.

Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21