• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 272
  • Last Modified:

DataGridTableStyles problem

hi, this is my first attempt as using DataGridTable styles.  I want to make one of my datagrid columns into a combobox. This is the method that Im following

1   create a new DataGridTableStyle
2   create new DataGridColumn styles
3   add the DatagridColumnStyles to the new DataGridTableStyle
4   add the new DataGridTableStyle to the datagrids tableStyles collection.

here is the code I am using,

---------------------------------------

Imports RustemSoft.DataGridColumns

Dim tableStyle As New DataGridTableStyle

'new DataGridTableStyle
 tableStyle.MappingName = "Model_Access"

'new DataGridComboboxClumn
        Dim col1 As New DataGridComboBoxColumn(dsAccessType.Tables.Item("States"), 0, 0, , , False)
        col1.MappingName = "Model_ID"
        col1.HeaderText = "Model"
        col1.Width = 45

        Dim TextCol As New DataGridTextBoxColumn
        TextCol.MappingName = "Accessorie_ID"
        TextCol.HeaderText = "Accessorie"
        TextCol.Width = 250

        'gridStyle.Add(cboGrid)
        tableStyle.GridColumnStyles.Add(TextCol)
        tableStyle.GridColumnStyles.Add(col1)

        dgrModels.TableStyles.Add(tableStyle)
        'Me.dgrModels.TableStyles("Model")
-----------------------------------------------------

Dim col1 As New DataGridComboBoxColumn(dsAccessType.Tables.Item("States"), 0, 0, , , False)

te above line gives me an error
'Object reference not set tan instance of an object"

Whats wrong with this?
Any links to a step by step tutorial on how to make a datagrid field a combbox would be great.

thanks

0
Nerdy_Girl88
Asked:
Nerdy_Girl88
  • 3
  • 3
1 Solution
 
iboutchkineCommented:
Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "
#End Region

    Private DGTextBoxCol As New DataGridTextBoxColumn()
    Private WithEvents Combo As New ComboBox()

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("Control")
        Dim arr(2) As String

        arr(0) = "Name"
        arr(1) = "ControlCol"
        arr(2) = "Description"
        Dim i As Integer

        For i = i To 2
            Dim col As New DataColumn(arr(i))
            col.DefaultValue = ""
            col.DataType = System.Type.GetType("System.String")

            dt.Columns.Add(col)

        Next
        dt.TableName = "Control"

        DataGrid1.DataSource = dt


        i = 0
        For i = i To 4
            dt.LoadDataRow(arr, True)
            DataGrid1(i, 0) = "Iouri"
            DataGrid1(i, 1) = "This is a Combo"
            DataGrid1(i, 2) = "Description"
        Next

        Dim dgtstyle As New DataGridTableStyle()

        dgtstyle.MappingName = "Control"
        dgtstyle.RowHeadersVisible = False
        dgtstyle.AllowSorting = False
        dgtstyle.HeaderBackColor = Color.Navy
        dgtstyle.HeaderForeColor = Color.White
        dgtstyle.HeaderFont = New System.Drawing.Font("Microsoft Sans Serif", 9.0F, FontStyle.Bold, GraphicsUnit.Point, 0)
        dgtstyle.GridLineColor = Color.DarkGray
        dgtstyle.PreferredRowHeight = Combo.Height

        DataGrid1.BackgroundColor = Color.White

        'first col
        Dim Data1 As New DataGridTextBoxColumn()
        Data1.MappingName = "Name"
        Data1.HeaderText = "Name"
        dgtstyle.GridColumnStyles.Add(Data1)

        'second col (combo)
        DGTextBoxCol.MappingName = "ControlCol"
        DGTextBoxCol.HeaderText = "Control"
        AddHandler DGTextBoxCol.TextBox.Enter, AddressOf TestThis
        dgtstyle.GridColumnStyles.Add(DGTextBoxCol)

        'third col
        Dim Data2 As New DataGridTextBoxColumn()
        Data2.MappingName = "description"
        Data2.HeaderText = "Description"
        dgtstyle.GridColumnStyles.Add(Data2)

        'add new style
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(dgtstyle)

    End Sub

    Private Sub TestThis(ByVal Sender As Object, ByVal e As EventArgs)
        Dim List As New ArrayList()

        'prepare list for combo
        List.Add("HI man1")
        List.Add("HI man2")
        List.Add("HI man3")
        List.Add("HI man4")
        List.Add("HI man5")


        'populate combo from list
        Combo.DataSource = List
        Combo.DisplayMember = "LongName"
        Combo.ValueMember = "ShortName"
        DGTextBoxCol.Width = Combo.Width
        DGTextBoxCol.TextBox.Controls.Add(Combo)
        Combo.BringToFront()
    End Sub

    Private Sub Combo_select(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectionChangeCommitted
        DataGrid1(DataGrid1.CurrentCell) = Combo.SelectedValue
    End Sub
End Class

0
 
Nerdy_Girl88Author Commented:
im not sure what to do with this?

I tried just running the code to see how it worked, i ran it on its own and get a errormessage saying the DataGrid1 not decleared, this made sense., since4 the form had no daatgrid on it. So i add a data grid called DataGrid1 and then I get this error "Object reference not set to an object instance"

Now im stuck,

thanks
0
 
iboutchkineCommented:
Here is the full code for you

1.Create a new project
2.Replace Form1 code with the code below

I just ran it. It works fine
------------------------------

Public Class Form1
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.DataGrid1 = New System.Windows.Forms.DataGrid
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
        Me.SuspendLayout()
        '
        'DataGrid1
        '
        Me.DataGrid1.DataMember = ""
        Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
        Me.DataGrid1.Location = New System.Drawing.Point(0, 0)
        Me.DataGrid1.Name = "DataGrid1"
        Me.DataGrid1.Size = New System.Drawing.Size(436, 212)
        Me.DataGrid1.TabIndex = 0
        '
        'Form1
        '
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(440, 266)
        Me.Controls.Add(Me.DataGrid1)
        Me.Name = "Form1"
        Me.Text = "Form1"
        CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
        Me.ResumeLayout(False)

    End Sub

#End Region
    Private DGTextBoxCol As New DataGridTextBoxColumn
    Private WithEvents Combo As New ComboBox

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim dt As New DataTable("Control")
        Dim arr(2) As String

        arr(0) = "Name"
        arr(1) = "ControlCol"
        arr(2) = "Description"
        Dim i As Integer

        For i = i To 2
            Dim col As New DataColumn(arr(i))
            col.DefaultValue = ""
            col.DataType = System.Type.GetType("System.String")

            dt.Columns.Add(col)

        Next
        dt.TableName = "Control"

        DataGrid1.DataSource = dt


        i = 0
        For i = i To 4
            dt.LoadDataRow(arr, True)
            DataGrid1(i, 0) = "Iouri"
            DataGrid1(i, 1) = "This is a Combo"
            DataGrid1(i, 2) = "Description"
        Next

        Dim dgtstyle As New DataGridTableStyle

        dgtstyle.MappingName = "Control"
        dgtstyle.RowHeadersVisible = False
        dgtstyle.AllowSorting = False
        dgtstyle.HeaderBackColor = Color.Navy
        dgtstyle.HeaderForeColor = Color.White
        dgtstyle.HeaderFont = New System.Drawing.Font("Microsoft Sans Serif", 9.0F, FontStyle.Bold, GraphicsUnit.Point, 0)
        dgtstyle.GridLineColor = Color.DarkGray
        dgtstyle.PreferredRowHeight = Combo.Height

        DataGrid1.BackgroundColor = Color.White

        'first col
        Dim Data1 As New DataGridTextBoxColumn
        Data1.MappingName = "Name"
        Data1.HeaderText = "Name"
        dgtstyle.GridColumnStyles.Add(Data1)

        'second col (combo)
        DGTextBoxCol.MappingName = "ControlCol"
        DGTextBoxCol.HeaderText = "Control"
        AddHandler DGTextBoxCol.TextBox.Enter, AddressOf TestThis
        dgtstyle.GridColumnStyles.Add(DGTextBoxCol)

        'third col
        Dim Data2 As New DataGridTextBoxColumn
        Data2.MappingName = "description"
        Data2.HeaderText = "Description"
        dgtstyle.GridColumnStyles.Add(Data2)

        'add new style
        DataGrid1.TableStyles.Clear()
        DataGrid1.TableStyles.Add(dgtstyle)

    End Sub

    Private Sub TestThis(ByVal Sender As Object, ByVal e As EventArgs)
        Dim List As New ArrayList

        'prepare list for combo
        List.Add("HI man1")
        List.Add("HI man2")
        List.Add("HI man3")
        List.Add("HI man4")
        List.Add("HI man5")


        'populate combo from list
        Combo.DataSource = List
        Combo.DisplayMember = "LongName"
        Combo.ValueMember = "ShortName"
        DGTextBoxCol.Width = Combo.Width
        DGTextBoxCol.TextBox.Controls.Add(Combo)
        Combo.BringToFront()
    End Sub

    Private Sub Combo_select(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectionChangeCommitted
        DataGrid1(DataGrid1.CurrentCell) = Combo.SelectedValue
    End Sub
End Class

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
Nerdy_Girl88Author Commented:
thanks for that it runs fine now, can you just explain this peice of the code.

'second col (combo)
        DGTextBoxCol.MappingName = "ControlCol"
        DGTextBoxCol.HeaderText = "Control"
        AddHandler DGTextBoxCol.TextBox.Enter, AddressOf TestThis
        dgtstyle.GridColumnStyles.Add(DGTextBoxCol)

Where does the DGTextBoxCol comefrom?  I probably just missed it in the code?

thanks
0
 
iboutchkineCommented:
Look at the top of the code (after the Region). That is where it is defined
 Private DGTextBoxCol As New DataGridTextBoxColumn

The rest is pretty much obvious

 DGTextBoxCol.MappingName = "ControlCol" --> give it a mappng name
 DGTextBoxCol.HeaderText = "Control" --> give the Header name
 AddHandler DGTextBoxCol.TextBox.Enter, AddressOf TestThis ---> add handler
 dgtstyle.GridColumnStyles.Add(DGTextBoxCol) ---> and attach tablestyle to datagrid


Good luck



0
 
Nerdy_Girl88Author Commented:
thanks here are your point :)
0

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now