Solved

DataGridTableStyles problem

Posted on 2004-09-25
6
239 Views
Last Modified: 2010-05-18
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
Comment
Question by:Nerdy_Girl88
  • 3
  • 3
6 Comments
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12152440
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
 

Author Comment

by:Nerdy_Girl88
ID: 12152803
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
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 12152905
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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

 

Author Comment

by:Nerdy_Girl88
ID: 12153287
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
 
LVL 28

Expert Comment

by:iboutchkine
ID: 12153300
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
 

Author Comment

by:Nerdy_Girl88
ID: 12153305
thanks here are your point :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction When many people think of the WebBrowser (http://msdn.microsoft.com/en-us/library/2te2y1x6%28v=VS.85%29.aspx) control, they immediately think of a control which allows the viewing and navigation of web pages. While this is true, it's a…
It’s quite interesting for me as I worked with Excel using vb.net for some time. Here are some topics which I know want to share with others whom this might help. First of all if you are working with Excel then you need to Download the Following …
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question