Solved

DataGridComboBoxColumn Problem with NoKeyUpCombo

Posted on 2004-10-06
10
273 Views
Last Modified: 2012-05-05
Hi

=====================

Option Strict Off
Option Explicit On

Imports System
Imports System.Data
Imports System.Drawing
Imports System.Windows.Forms

Namespace DataGridCombobox

    '
    ' Derive a custom column style from DataGridTextBoxColumn that:
    '  - adds a ComboBox member
    '  - tracks when the combobox has focus in the Enter and Leave events
    '  - overrides Edit event so the ComboBox replaces the TextBox
    '  - overrides Commit event to save the changed data
    '

    Public Class DataGridComboBoxColumn

#Region " Declarations "

        Inherits DataGridTextBoxColumn

        Public ColumnComboBox As NoKeyUpCombo 'error here

        Private mcmSource As System.Windows.Forms.CurrencyManager
        Private mintRowNum As Integer
        Private mblnEditing As Boolean

#End Region

#Region " Constructor "

        Public Sub New()

            mcmSource = Nothing
            mblnEditing = False

            ColumnComboBox = New NoKeyUpCombo 'error here

            ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList

            AddHandler ColumnComboBox.Leave, AddressOf LeaveComboBox
            AddHandler ColumnComboBox.SelectionChangeCommitted, AddressOf ComboStartEditing

        End Sub

#End Region

#Region " Private Methods "

        Private Sub HandleScroll(ByVal sender As Object, ByVal e As EventArgs)

            If ColumnComboBox.Visible Then ColumnComboBox.Hide()

        End Sub

        Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As EventArgs)

            mblnEditing = True
            MyBase.ColumnStartedEditing(sender)

        End Sub

        Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)

            If mblnEditing Then
                SetColumnValueAtRow(mcmSource, mintRowNum, ColumnComboBox.Text)
                mblnEditing = False
                Invalidate()
            End If

            ColumnComboBox.Hide()
            AddHandler Me.DataGridTableStyle.DataGrid.Scroll, New EventHandler(AddressOf HandleScroll)

        End Sub

#End Region

#Region " Overridden Methods "

        Protected Overloads Overrides Sub Edit( _
                    ByVal [source] As System.Windows.Forms.CurrencyManager, _
                    ByVal rowNum As Integer, _
                    ByVal bounds As System.Drawing.Rectangle, _
                    ByVal [readOnly] As Boolean, _
                    ByVal instantText As String, _
                    ByVal cellIsVisible As Boolean)

            MyBase.Edit([source], rowNum, bounds, [readOnly], instantText, cellIsVisible)

            mintRowNum = rowNum
            mcmSource = [source]

            With ColumnComboBox
                .Parent = Me.TextBox.Parent
                .Location = Me.TextBox.Location
                .Size = New Size(Me.TextBox.Size.Width, .Size.Height)
                .SelectedIndex = .FindStringExact(Me.TextBox.Text)
                .Text = Me.TextBox.Text

                Me.TextBox.Visible = False
                .Visible = True

                AddHandler Me.DataGridTableStyle.DataGrid.Scroll, AddressOf HandleScroll

                .BringToFront()
                .Focus()
            End With

        End Sub

        Protected Overrides Function Commit( _
                    ByVal dataSource As System.Windows.Forms.CurrencyManager, _
                    ByVal rowNum As Integer) As Boolean

            If mblnEditing Then
                mblnEditing = False
                SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
            End If

            Return True

        End Function

        Protected Overrides Sub ConcedeFocus()

            Console.WriteLine("ConcedeFocus")
            MyBase.ConcedeFocus()

        End Sub

        Protected Overrides Function GetColumnValueAtRow( _
                    ByVal [source] As System.Windows.Forms.CurrencyManager, _
                    ByVal rowNum As Integer) As Object

            Dim s As Object = MyBase.GetColumnValueAtRow([source], rowNum)
            Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)
            Dim rowCount As Integer = dv.Count
            Dim i As Integer = 0
            Dim obj As Object

            While i < rowCount
                obj = dv(i)(Me.ColumnComboBox.ValueMember)
                If (Not obj Is DBNull.Value) AndAlso _
                   (Not s Is DBNull.Value) AndAlso _
                   (s = obj) Then

                    Exit While
                End If

                i += 1
            End While

            If i < rowCount Then
                Return dv(i)(Me.ColumnComboBox.DisplayMember)
            End If

            Return DBNull.Value

        End Function

        Protected Overrides Sub SetColumnValueAtRow( _
                    ByVal [source] As System.Windows.Forms.CurrencyManager, _
                    ByVal rowNum As Integer, ByVal value As Object)

            Dim s As Object = value
            Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)
            Dim rowCount As Integer = dv.Count
            Dim i As Integer = 0
            Dim obj As Object

            While i < rowCount
                obj = dv(i)(Me.ColumnComboBox.DisplayMember)

                If (Not obj Is DBNull.Value) AndAlso _
                    (s = obj) Then

                    Exit While
                End If

                i += 1
            End While

            If i < rowCount Then
                s = dv(i)(Me.ColumnComboBox.ValueMember)
            Else
                s = DBNull.Value
            End If

            MyBase.SetColumnValueAtRow([source], rowNum, s)
        End Sub

#End Region

    End Class

End Namespace
==============================================


I am using the above class to add a combobox to a datagrid.  I have used this class before and worked fine.   My problem is that it now tells me i have this error!

Type 'NoKeyUpCombo' is not defind. on these lines

Public ColumnComboBox As NoKeyUpCombo 'error here
ColumnComboBox = New NoKeyUpCombo 'error here


Can anyone see what my class is missing or post me a new DataGridComboBoxColumn  to perform this function.

Thanks
0
Comment
Question by:Nerdy_Girl88
  • 6
  • 4
10 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
Comment Utility
Hi nerdy_girl88,

The first and obvious question, is the nokeyupcombo class in your project

Option Strict Off
Option Explicit On

Imports Microsoft.VisualBasic
Imports System
Imports System.ComponentModel
Imports System.Data
Imports System.Data.Common
Imports System.Data.OleDb
Imports System.Drawing
Imports System.Windows.Forms

Namespace DataGridTextBoxCombo
    Public Class NoKeyUpCombo
        Inherits ComboBox
        Private WM_KEYUP As Integer = &H101


        Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
            If m.Msg = WM_KEYUP Then
                Return
            End If
            MyBase.WndProc(m)
        End Sub
    End Class
End Namespace
0
 

Author Comment

by:Nerdy_Girl88
Comment Utility
Your right it wasn;t, silly, just addeed it now i get this error

Specific cast is not valid!

The program runs but when i Click on any control i get this error,  

thanks.
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Could you specify where the error occurs in your code (set
0
 

Author Comment

by:Nerdy_Girl88
Comment Utility
it appears at the being of my frmAccessory class.  This is the class that has all the controls on it, including the datagrid with the new combobox column.  The error is highlighted green, I don't know how to deal with these error messages.

I have a from called frmAccessory.

Imports Quote_a_matic2000.DataGridCombobox
Public Class frmAccessory   //this is the line i get the error on.
    Inherits System.Windows.Forms.Form
'code in here
  End Sub
End Class

thanks
0
 

Author Comment

by:Nerdy_Girl88
Comment Utility
I just stepped through me code and noticed it happened here

Private Sub cboSearch_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles cboSearch.SelectedIndexChanged
        Dim DsAccessory As DataSet
        Dim Parameter As String

        If mFinished Then
            'fill the dataset with data
            Parameter = cboSearch.SelectedValue
            DsAccessory = mDataobj.getAccessory(Parameter)
        End If
    End Sub    //error here
End Class

it jumps to here......Public Class frmAccessory   and highlights frmAccessory, and displays this error.    //this is the line i get the error on.

An unhandled exception of type 'System.InvalidCastException' occurred in system.windows.forms.dll
0
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Nerdy_Girl88
Comment Utility
is it because the cast type expected in the DataGridComboBoxColumn class is expecting that datagrid.datasource to be a dataview?
0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
I think it is, because of this line

Dim dv As DataView = CType(Me.ColumnComboBox.DataSource, DataView)
0
 

Author Comment

by:Nerdy_Girl88
Comment Utility
The datagrid on my form is  part of a master detail form, i want to be able to update,Add and delete from the dataset!  

Is there cose i can use to modify this function to use a dataset.

0
 
LVL 25

Expert Comment

by:RonaldBiemans
Comment Utility
Hi Nerdy_Girl88,

What has updating,adding and deleting from the dataset have to do with how you fill your combobox ?

how do you fill the combobox now ?
0
 

Author Comment

by:Nerdy_Girl88
Comment Utility
Your right it doesn't,  I was thinking the dataview would need to be updated.  I Now realise that the combobox has a daview. And the datagrid has its own dataset that is bound to it.  I get very confused using all these different objects.  Thanks for you help.

The combobox works fine now, i just changed the datasource to a dataview.

Thanks.
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
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.
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

772 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

12 Experts available now in Live!

Get 1:1 Help Now