Solved

DataGridTableStyles problem

Posted on 2004-09-25
6
252 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

 

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

734 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