Solved

DataGridTableStyles problem

Posted on 2004-09-25
6
212 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Since .Net 2.0, Visual Basic has made it easy to create a splash screen and set it via the "Splash Screen" drop down in the Project Properties.  A splash screen set in this manner is automatically created, displayed and closed by the framework itsel…
Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

760 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now