Turbojohn

Hi,

I have a DataGridView that displays a table which can be dynamically changed by the user selecting from a combobox of tables. I want to provide some consistent column ordering so that the same 2 columns always appear at the start of the table in the DataGridView. How could this be achieved The obvious problem I have is that the DataGridView's data binding changes dynamically at runtime depending on what table is displayed!

Any help would be much appreciated.

Cheers,

Turbojohn



Re: Windows Forms Data Controls and Databinding DataGridView Column Ordering

Derek Smyth

Hi mate,

I've done something similar to what your doing here, one grid showing many tables. The way in which I handled the column ordering was to create a method that builds a collection of datagrid columns used by the grid. When the user changes the table and the application changes the data source, it clears the columns from the datagrid and adds the columns returned by call the method.

Here's an example, this is very specific to my project but it will give you an idea of how I do this.

So here is my method that creates the columns, the columns are displayed in the order they are added. DataPropertyName is the name of the column that us being bound to (i.e binds to the "Reference Number" column of the data source).

Code Snippet

Public Function DefaultLOFColumnStyles() As List(Of Windows.Forms.DataGridViewColumn)

Dim columns As New List(Of Windows.Forms.DataGridViewColumn)

Dim datItemIDCol As New Windows.Forms.DataGridViewTextBoxColumn

datItemIDCol.DataPropertyName = "Reference Number"

datItemIDCol.HeaderText = "Reference Number"

datItemIDCol.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.AllCellsExceptHeader

'datItemIDCol.Frozen = True

columns.Add(datItemIDCol)

Dim datPIDCol As New Windows.Forms.DataGridViewTextBoxColumn

datPIDCol.DataPropertyName = "P+ID Number"

datPIDCol.HeaderText = "P+ID Number"

datPIDCol.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.AllCellsExceptHeader

'datPIDCol.Frozen = True

columns.Add(datPIDCol)

Dim datItemTypeCol As New Windows.Forms.DataGridViewTextBoxColumn

datItemTypeCol.DataPropertyName = "ItemType"

datItemTypeCol.HeaderText = "Type"

datItemTypeCol.AutoSizeMode = Windows.Forms.DataGridViewAutoSizeColumnMode.AllCellsExceptHeader

'datItemTypeCol.Frozen = True

columns.Add(datItemTypeCol)

Return columns

End Function

Then when the user selects from a drop down I call this...

Code Snippet

Me.dgridLOFs.Columns.Clear()

'if selected item is X then

Me.dgridLOFs.Columns.AddRange(Me.DefaultLOFColumnStyles.ToArray) 'load the columns for displaying X

Me.dgridLOFs.DataSource = DataSet.TableX 'set to the required datasource for X

Hope thats helpful






Re: Windows Forms Data Controls and Databinding DataGridView Column Ordering

Turbojohn

Thanks Derek.

It looks like your using Visual Basic( ). I am programming in C#, I'll try to understand your code as best I can!

Thanks again,

Turbojohn