Change the Devexpress Grid Layout Dynamically based on data source

1k views Asked by At

Trying to build in an admin tool in VB.Net that will display display data based upon a dynamic sql selection from any table. Works fine the first time through, the second time the columns on the DevExpress XtraGrid displayed match the new SQL statement and result set, the data table has data, the grid properties should the data source correctly with data but the data does not show up on the grid during display.

    Try
        'Initialize Datatable and Grid
        dt.Reset()
        GridView1.Columns.Clear()
        grdAdminSQL.DataSource = Nothing

        'Get the data and load into data table
        myCmd = myConn.CreateCommand
        myCmd.CommandText = tbAdmSQL.Text.Trim
        myConn.Open()
        myreader = myCmd.ExecuteReader()
        dt.Load(myreader)

        'Reset the Data Source on the grid
        grdAdminSQL.DataSource = dt
        grdAdminSQL.RefreshDataSource()
    Catch ex As Exception
    Finally
        myConn.Close()
        myConn.Dispose()
    End Try
2

There are 2 answers

3
sgmoore On

I think that is because GridView1.Columns.Clear() isn't enough to let DevExpress know it needs to re-create the columns.

You need to also call

grdAdminSQL.MainView.PopulateColumns()

after you have set your dataSource.

If that doesn't work, add

grdAdminSQL.ForceInitialize()
0
Rob  Bergstrom On

I correct the issues by moving the select to an external function that returns a datatable. For some reason if the datatable is created locally within the scope of the form the data will not appear on the grid.

Try
        'Initialize Datatable and Grid
        dt.Reset()
        GridView1.Columns.Clear()
        grdAdminSQL.DataSource = Nothing

        'Get the data and load into data table
        dt = TMSII_ClassLib.Common.ExecuteSQL(MyConnection, tbAdmSQL.Text.Trim)
        'Reset the Data Source on the grid
        grdAdminSQL.DataSource = dt
        grdAdminSQL.RefreshDataSource()
    Catch ex As Exception
    Finally
        myConn.Close()
        myConn.Dispose()
    End Try