Solved

DataGridComboBoxColumn Problem with NoKeyUpCombo

Posted on 2004-10-06
10
314 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
[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
  • 6
  • 4
10 Comments
 
LVL 25

Accepted Solution

by:
RonaldBiemans earned 500 total points
ID: 12236166
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
ID: 12236179
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
ID: 12236216
Could you specify where the error occurs in your code (set
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Nerdy_Girl88
ID: 12236253
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
ID: 12236280
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
 

Author Comment

by:Nerdy_Girl88
ID: 12236337
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
ID: 12236382
I think it is, because of this line

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

Author Comment

by:Nerdy_Girl88
ID: 12236404
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
ID: 12236473
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
ID: 12246065
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

732 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