Thread: Help with datagridviews?

  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 11:24 AM.

Popular pages Recent additions subscribe to a feed