programatically click column header of a Datagrid

Holly Max

Member
Joined
Nov 2, 2006
Messages
6
Programming Experience
Beginner
I need to sort a datagrid control by programatically clicking the column header of a Datagrid. I have seen code in C# that uses the datagrid's private method ColumnHeaderClicked, but this is beyond me.
My problem is that if I use the .DefaultView.Sort I loose the binding I need to text boxes for updating records ect.
If I manually click the header of the column everything is fine.
Any help would be appreciated or if you need more info I will do this.

Holly Max
 
have seen code in C# that uses the datagrid's private method ColumnHeaderClicked, but this is beyond me.

I'm a little confused by the above statement. How could anyone directly use a private method? Fact is you can't because it's private. Have you got this c# code to hand and i'll translate it for you. Also why are there a couple of links to the experts exchange in your post???
 
This is the whole article. Hope this helps you.
"This code sorts a Windows.Forms.DataGrid programmatically, "emulating" a header click"
Introduction
This article shows, how to programmatically sort a System.Windows.Forms.DataGrid. In other words, how to emulate a "click" on a column header.
Typically, to sort a DataGrid, we call the DataView.Sort method of an underlying DataView. But what if our DataGrid is bound to a custom datasource? In this case, there's no DataView under our DataGrid!
Background
I was writing an application and the task was to "remember" the way DataGrids are sorted. So I needed to save my sortings to the Windows registry and restore it each time my application starts.
The problem was: my DataGrid was bound to a custom collection, no DataViews or DataTables.
Sorting Basics
If your DataGrid is bound to some IList collection and you want your DataGrid to support sorting - your collection must implement IBindingList interface. This interface contains a method ApplySort() among others. When a user clicks a column header, DataGrid calls the datasource's method ApplySort().
ApplySort() method takes two arguments: property and direction.
void IBindingList.ApplySort(PropertyDescriptor property,
ListSortDirection direction)
Each column of your DataGrid represents some property of an underlying object. And you have to pass this property to ApplySort() method.
Emulate column header click
I've discovered that simply calling ApplySort() doesn't work. Calling DataGrid's OnMouseDown() protected method with MouseEventArgs pointed to a column header also doesn't work!
So, I used System.Reflection to look deep inside the DataGrid class and see what happens when a user clicks a header. In the list of DataGrid's private members, I've found a private method ColumnHeaderClicked(). See what I mean? Bingo.
This method is defined as follows:
private void ColumnHeaderClicked(PropertyDescriptor prop)
So, if we want to sort column number 5, we have to determine the underlying PropertyDescriptor for this column and pass this PropertyDescriptor to ColumnHeaderClicked method. This can be done in two ways.
* Calling for a GridColumnStyle.PropertyDescriptor property of a grid column. But this property sometimes returns null if our grid is bound to a custom collection through MappingNames.
* If our DataGrid is bound to a custom collection MyCollection, we typically create a TableStyle and some GridColumnStyles and assign its MappingNames. TableStyle's MappingName would be "MyCollection" and ColumnStyle's MappingName would contain the name of some property this column displays. So, we can get the PropertyDescriptor object by seeking for the property with the name, equal to the column's MappingName.
After we have a PropertyDescriptor object, we simply invoke a private method using System.Reflection.
Now the code:
Collapse
public class MyDataGrid : DataGrid
{
//sort a column by its index
public void SortColumn(int columnIndex)
{
if(this.DataSource!=null &&
((System.Collections.IList)this.DataSource).Count>0)
{
//discover the TYPE of underlying objects
Type sourceType = ((System.Collections.IList)this.DataSource)[0].GetType();
//get the PropertyDescriptor for a sorted column
//assume TableStyles[0] is used for our datagrid... (change it if necessary)
System.ComponentModel.PropertyDescriptor pd =
this.TableStyles[0].GridColumnStyles[columnIndex].PropertyDescriptor;
//if the above line of code didn't work try to get a propertydescriptor
// via MappingName
if(pd == null)
{
System.ComponentModel.PropertyDescriptorCollection pdc =
System.ComponentModel.TypeDescriptor.GetProperties(sourceType);
pd =
pdc.Find( this.TableStyles[0].GridColumnStyles[columnIndex].MappingName,
false);
}
//now invoke ColumnHeaderClicked method using system.reflection tools
System.Reflection.MethodInfo mi =
typeof(System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked",
System.Reflection.BindingFlags.Instance |
System.Reflection.BindingFlags.NonPublic);
mi.Invoke(this, new object[] { pd });
}
}
}
That's it.
 
Running that code through Developer Fusions converter give this VB.Net code (in box). I haven't tested it, but it 'looks' right :) It's an inherited control from DataGrid, to be used instead of the regular DataGrid control. This 'MyDataGrid' control then expose the public SortColumn method that should work as if the column header was clicked (according to the description).
VB.NET:
[FONT=Courier New]Public Class MyDataGrid [/FONT]
[FONT=Courier New]Inherits DataGrid [/FONT]
 
[FONT=Courier New]Public Sub SortColumn(ByVal columnIndex As Integer) [/FONT]
[FONT=Courier New]If Not (Me.DataSource Is Nothing) AndAlso CType(Me.DataSource, System.Collections.IList).Count > 0 Then [/FONT]
[FONT=Courier New] Dim sourceType As Type = CType(Me.DataSource, System.Collections.IList)(0).GetType [/FONT]
[FONT=Courier New] Dim pd As System.ComponentModel.PropertyDescriptor = _[/FONT]
[FONT=Courier New]   Me.TableStyles(0).GridColumnStyles(columnIndex).PropertyDescriptor [/FONT]
[FONT=Courier New] If pd Is Nothing Then [/FONT]
[FONT=Courier New]   Dim pdc As System.ComponentModel.PropertyDescriptorCollection = _[/FONT]
[FONT=Courier New]     System.ComponentModel.TypeDescriptor.GetProperties(sourceType) [/FONT]
[FONT=Courier New]   pd = pdc.Find(Me.TableStyles(0).GridColumnStyles(columnIndex).MappingName, False) [/FONT]
[FONT=Courier New] End If [/FONT]
[FONT=Courier New] Dim mi As System.Reflection.MethodInfo = _[/FONT]
[FONT=Courier New]   GetType(System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked", _[/FONT]
[FONT=Courier New]   System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic) [/FONT]
[FONT=Courier New] mi.Invoke(Me, New Object() {pd}) [/FONT]
[FONT=Courier New]End If [/FONT]
[FONT=Courier New]End Sub [/FONT]
[FONT=Courier New]End Class[/FONT]
 
VB.NET:
Public Class MyDataGrid : Inherits DataGrid
'sort a column by its index
Public Sub SortColumn(ByVal columnIndex As Integer)
If Not Me.DataSource Is Nothing AndAlso (CType(Me.DataSource, System.Collections.IList)).Count> 0 Then
'discover the TYPE of underlying objects
Dim sourceType As Type = (CType(Me.DataSource, System.Collections.IList))(0).GetType()
'get the PropertyDescriptor for a sorted column
'assume TableStyles[0] is used for our datagrid... (change it if necessary)
Dim pd As System.ComponentModel.PropertyDescriptor = Me.TableStyles(0).GridColumnStyles(columnIndex).PropertyDescriptor
'if the above line of code didn't work try to get a propertydescriptor
' via MappingName
If pd Is Nothing Then
Dim pdc As System.ComponentModel.PropertyDescriptorCollection = System.ComponentModel.TypeDescriptor.GetProperties (sourceType)
pd = pdc.Find(Me.TableStyles(0).GridColumnStyles(columnIndex).MappingName, False)
End If
'now invoke ColumnHeaderClicked method using system.reflection tools
Dim mi As System.Reflection.MethodInfo = GetType(System.Windows.Forms.DataGrid).GetMethod("C olumnHeaderClicked", System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
mi.Invoke(Me, New Object() { pd })
End If
End Sub
End Class

Ok, here's that in vb.net. So basically it's just a class that inherits from the datagrid. So rather than using the standard datagrid in your app you would use this one instead which has the new method that you need. If you are using 2005 then you may be able to accomplish all this with a partial class. but it will work just fine as it is.
 
Thanks for the help so far. Now is this code used as a User Control, or how do I replace the standard datagrid with MyDataGrid (or make the reference to this new control)?

I know you haven't tested it but can you see any problems using this type of method to acheive what I am trying to do.
I basically need for the datagrid columns to be able to be sorted without using the .DefaultView.Sort method which looses the binding to the text boxes I need. Having the Columns clicked (or clicking the columns) maintains the binding.

Holly Max
 
Also I didn't post the C# code laid out in case I missed part of the code in amongst the article. That's why I posted the whole thing as it was. I've never done any C# coding. I have read some and can sometimes follow what the vb.net equivalent is or should be.
 
OK this is the Custom Control I created.
Using the line
MyDataGrid.SortColumn(1)

I get the following error

An unhandled exception of type 'System.InvalidCastException' occurred in mydatagrid.dll
Additional information: Specified cast is not valid.

Custom Control code
VB.NET:
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE][SIZE=2] MyDataGrid[/SIZE]
[SIZE=2][COLOR=#0000ff]Inherits[/COLOR][/SIZE][SIZE=2] DataGrid[/SIZE]
[SIZE=2][COLOR=#008000]'Inherits System.Windows.Forms.DataGrid[/COLOR][/SIZE]
[SIZE=2]#[/SIZE][SIZE=2][COLOR=#0000ff]Region[/COLOR][/SIZE][SIZE=2] " Windows Form Designer generated code "[/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2]()[/SIZE]
[SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].New()[/SIZE]
[SIZE=2][COLOR=#008000]'This call is required by the Windows Form Designer.[/COLOR][/SIZE]
[SIZE=2]InitializeComponent()[/SIZE]
[SIZE=2][COLOR=#008000]'Add any initialization after the InitializeComponent() call[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'UserControl1 overrides dispose to clean up the component list.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Protected[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overloads[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Overrides[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] Dispose([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] disposing [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Boolean[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] disposing [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] (components [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2]) [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2]components.Dispose()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]MyBase[/COLOR][/SIZE][SIZE=2].Dispose(disposing)[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Required by the Windows Form Designer[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] components [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.IContainer[/SIZE]
[SIZE=2][COLOR=#008000]'NOTE: The following procedure is required by the Windows Form Designer[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'It can be modified using the Windows Form Designer. [/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'Do not modify it using the code editor.[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Friend[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]WithEvents[/COLOR][/SIZE][SIZE=2] DataGrid [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DataGrid[/SIZE]
[SIZE=2]<System.Diagnostics.DebuggerStepThrough()> [/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] InitializeComponent()[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Windows.Forms.DataGrid[/SIZE]
[SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid, System.ComponentModel.ISupportInitialize).BeginInit()[/SIZE]
[SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], System.ComponentModel.ISupportInitialize).BeginInit()[/SIZE]
[SIZE=2][COLOR=#008000]'[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'DataGrid[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid.DataMember = ""[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid.HeaderForeColor = System.Drawing.SystemColors.ControlText[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid.Location = [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] System.Drawing.Point(17, 17)[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid.Name = "DataGrid"[/SIZE]
[SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid.TabIndex = 0[/SIZE]
[SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataGrid, System.ComponentModel.ISupportInitialize).EndInit()[/SIZE]
[SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], System.ComponentModel.ISupportInitialize).EndInit()[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2]#[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Region[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'sort a column by its index[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Public[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE][SIZE=2] SortColumn([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] columnIndex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Integer[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSource [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]AndAlso[/COLOR][/SIZE][SIZE=2] ([/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSource, System.Collections.IList)).Count > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'discover the TYPE of underlying objects[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] sourceType [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Type = ([/SIZE][SIZE=2][COLOR=#0000ff]CType[/COLOR][/SIZE][SIZE=2]([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].DataSource, System.Collections.IList))(0).GetType()[/SIZE]
[SIZE=2][COLOR=#008000]'get the PropertyDescriptor for a sorted column[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'assume TableStyles[0] is used for our datagrid... (change it if necessary)[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pd [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.PropertyDescriptor = [/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TableStyles(0).GridColumnStyles(columnIndex).PropertyDescriptor[/SIZE]
[SIZE=2][COLOR=#008000]'if the above line of code didn't work try to get a propertydescriptor[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]' via MappingName[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] pd [/SIZE][SIZE=2][COLOR=#0000ff]Is[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Nothing[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] pdc [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.ComponentModel.PropertyDescriptorCollection = System.ComponentModel.TypeDescriptor.GetProperties(sourceType)[/SIZE]
[SIZE=2]pd = pdc.Find([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2].TableStyles(0).GridColumnStyles(columnIndex).MappingName, [/SIZE][SIZE=2][COLOR=#0000ff]False[/COLOR][/SIZE][SIZE=2])[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#008000]'now invoke ColumnHeaderClicked method using system.reflection tools[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] mi [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] System.Reflection.MethodInfo = [/SIZE][SIZE=2][COLOR=#0000ff]GetType[/COLOR][/SIZE][SIZE=2](System.Windows.Forms.DataGrid).GetMethod("ColumnHeaderClicked", _[/SIZE]
[SIZE=2]System.Reflection.BindingFlags.Instance [/SIZE][SIZE=2][COLOR=#0000ff]Or[/COLOR][/SIZE][SIZE=2] System.Reflection.BindingFlags.NonPublic)[/SIZE]
[SIZE=2]mi.Invoke([/SIZE][SIZE=2][COLOR=#0000ff]Me[/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Object[/COLOR][/SIZE][SIZE=2]() {pd})[/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Sub[/COLOR][/SIZE]
[SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2][COLOR=#0000ff]Class[/COLOR][/SIZE]
 
The code you posted in the #Region doesn't belong there, but it don't affect this request.

Here is a modification of the inherited Datagrid class, I have tested it and it works for sorting programmatically.
VB.NET:
Public Class MyDataGrid
    Inherits DataGrid
 
    Public Sub SortColumn(ByVal columnIndex As Integer)
        If Not Me.DataSource Is Nothing Then 
            Dim pd As System.ComponentModel.PropertyDescriptor
            pd = Me.TableStyles(0).GridColumnStyles(columnIndex).PropertyDescriptor
            Dim mi As System.Reflection.MethodInfo
            mi = GetType(DataGrid).GetMethod("ColumnHeaderClicked", _
                System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.NonPublic)
            mi.Invoke(Me, New Object() {pd})
        End If
    End Sub
End Class
Here is example of usage, do take note that the databound Datagrid doesn't have a TableStyles by default so you must create one and add it if you don't already have one - the sort method use this to get the PropertyDescriptor which is passed to the private ColumnHeaderClicked method.
VB.NET:
Private WithEvents x As New MyDataGrid
 
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MyBase.Load
    Me.Controls.Add(x)
    Dim ds As New DataSet
    ds.ReadXml("somedata.xml")
    Dim ts As New DataGridTableStyle
    ts.MappingName = ds.Tables(2).TableName
    x.TableStyles.Add(ts)
    x.DataSource = ds.Tables(2)
    x.Dock = DockStyle.Fill
End Sub
 
Private Sub MenuItem1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles MenuItem1.Click
    x.SortColumn(2)
End Sub
 
Thank you JohnH. Finally got it working along with the rest of my code.
The last code I posted was for a Custom User Control, which I realise I don't need now you have given me an example to explain it.

Thank you again for the help and also vis781

Holly Max
 
Also I didn't post the C# code laid out in case I missed part of the code in amongst the article. That's why I posted the whole thing as it was. I've never done any C# coding. I have read some and can sometimes follow what the vb.net equivalent is or should be.


Just compile the c# project and add a reference to the compiled result, to your project - your VB code can use C# components because when compiled they all use the same low level language
 
Back
Top