trouble migrating from datagrid to datagridview

atilak

Member
Joined
Jul 23, 2006
Messages
8
Location
india
Programming Experience
3-5
Hi... I used datagrid in my application to display my custom class data. I used to create DataGridTableStyle to format the grid columns ... Now I am moving to DataGridView, because .net 2005 says so, and now I have no idea how to do what I used to do with TableStyle. I cannot bind the DataGridView to my custom class... Can anybody help?? Here is a sample code with DataGrid... Can anyone convert it to use it with DataGridView?

VB.NET:
    Sub BindToArray()
        ' Creates an array of Widget objects (defined below).
        Dim Widgets(2) As Widget
        Dim tempWidget As Widget
        tempWidget = New Widget()
        tempWidget.Model = "AAA"
        tempWidget.Id = "A100"
        tempWidget.Price = Convert.ToDecimal(3.8)
        Widgets(0) = tempWidget
        ' The first Widget includes an array of Part objects.
        Dim p1 As New Part()
        p1.PartId = "PartX"
        Dim p2 As New Part()
        p2.PartId = "PartY"
        ' Note that the Widgets.Parts property returns an ArrayList.
        ' Add the parts to the ArrayList using the AddRange method.
        tempWidget.Parts.AddRange(New Part() {p1, p2})
        tempWidget = New Widget()
        tempWidget.Model = "BBB"
        tempWidget.Id = "B100"
        tempWidget.Price = Convert.ToDecimal(1.52)
        Widgets(1) = tempWidget
        tempWidget = New Widget()
        tempWidget.Id = "CCC"
        tempWidget.Model = "B100"
        tempWidget.Price = Convert.ToDecimal(2.14)
        Widgets(2) = tempWidget
        DataGrid1.SetDataBinding(Widgets, "")
        CreateTableStyle()
    End Sub
 
    Private Sub CreateTableStyle()
        ' Creates two DataGridTableStyle objects, one for the Widget
        ' array, and one for the Parts ArrayList.
        Dim widgetTable As New DataGridTableStyle()
        ' Sets the MappingName to the class name plus brackets.    
        widgetTable.MappingName = "Widget[]"
        ' Sets the AlternatingBackColor so you can see the difference.
        widgetTable.AlternatingBackColor = System.Drawing.Color.LightBlue
        ' Creates three column styles.
        Dim modelColumn As New DataGridTextBoxColumn()
        modelColumn.MappingName = "Model"
        modelColumn.HeaderText = "Model"
        Dim IdColumn As New DataGridTextBoxColumn()
        IdColumn.MappingName = "Id"
        IdColumn.HeaderText = "Id"
        Dim priceColumn As New DataGridTextBoxColumn()
        priceColumn.MappingName = "Price"
        priceColumn.HeaderText = "Price"
        priceColumn.Format = "c"
        ' Adds the column styles to the grid table style.
        widgetTable.GridColumnStyles.Add(modelColumn)
        widgetTable.GridColumnStyles.Add(IdColumn)
        widgetTable.GridColumnStyles.Add(priceColumn)
        ' Add the table style to the collection, but clear the 
        ' collection first.
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(widgetTable)
        ' Create another table style, one for the related data.
        Dim partsTable As New DataGridTableStyle()
        ' Set the MappingName to an ArrayList. Note that you need not 
        ' include brackets.
        partsTable.MappingName = "ArrayList"
        Dim partIdColumn As New DataGridTextBoxColumn()
        partIdColumn.MappingName = "PartID"
        partIdColumn.HeaderText = "Part ID"
        partsTable.GridColumnStyles.Add(partIdColumn)
        DataGrid1.TableStyles.Add(partsTable)
    End Sub
 
    Public Class Widget
        Private myModel As String
        Private myId As String
        Private myPrice As Decimal
        ' Use an ArrayList to create a related collection.
        Private myParts As New ArrayList()
        Public Property Model() As String
            Get
                Return myModel
            End Get
            Set(ByVal Value As String)
                myModel = Value
            End Set
        End Property
        Public Property Id() As String
            Get
                Return myId
            End Get
            Set(ByVal Value As String)
                myId = Value
            End Set
        End Property
        Public Property Parts() As ArrayList
            Get
                Return myParts
            End Get
            Set(ByVal Value As ArrayList)
                myParts = Value
            End Set
        End Property
        Public Property Price() As Decimal
            Get
                Return myPrice
            End Get
            Set(ByVal Value As Decimal)
                myPrice = Value
            End Set
        End Property
    End Class
 
    Public Class Part
        Private myPartId As String
 
        Public Property PartId() As String
            Get
                Return myPartId
            End Get
            Set(ByVal Value As String)
                myPartId = Value
            End Set
        End Property
    End Class
 
Hi

Although the DataGridView is the new Grid control and in most cases should be used in .NET 2.0 applications, you can still use the DataGrid control. A valid reason for sticking to the DataGrid control is that the DataGridView does not support the display of hierarchical data in a single Grid. To achieve the same using the DataGridView controls you will have to make use of two DataGridView controls (something that I haven't done as I don't do much Windows programming).

See http://msdn2.microsoft.com/en-us/library/ms171628.aspx for more information.

You may also find this link of interest:
http://blogs.msdn.com/markrideout/archive/2006/01/08/510700.aspx


HTH
 
Back
Top