Trouble with combobox in DataGrid

Fiaolle

Member
Joined
Jun 14, 2006
Messages
8
Programming Experience
1-3
Hi
The first set of source code is the class for a combobox in a grid,
hopefully. In the second set of code we try to use the combobox class, but
the grid is empty.
I don't understand how this works. The first set of code I downloaded from
internet and when i tried it, it worked fine. But when I changed the second
part of the code it started to give me trouble. Before I changed the code
they used Datacolumns,Datarows and Datatables and added them to the Dataset
which they used to the Datagrid and it worked fine. I don't understand what
is wrong know that I'm using a table I haven't "made" myself but a table I
got from a connection. As the third set of code showing, when using
comboboxes you can set the Datasource to a table you have connected to with
a connection and it works fine, so I think the second part of code should
work, but the grid is empty when the program starts.

If there is anyone who understand my problem and have a solution, please
give me an answer.

Thanks

Fia

VB.NET:
'The MyComboColumn class
Imports System.Data
Imports System.Drawing
Imports System.Collections
Imports System.Windows.Forms
Namespace ComboBoxTest
Public Class MyComboColumn
Inherits System.Windows.Forms.DataGridTextBoxColumn
Private _cboColumn As ComboBox
Private _objSource As Object
Private _strMember As String
Private _strValue As String
Private _bIsComboBound As Boolean = False
Private _backBrush As Brush = Nothing
Private _foreBrush As Brush = Nothing
Private _iRowNum As Integer
Private _cmSource As CurrencyManager
Public Sub New(ByVal objSource As DataSet, ByVal strMember As String, ByVal
strValue As String, ByVal bUseDropDownList As Boolean)
'_objSource = objSource
_strMember = strMember
_strValue = strValue
_cboColumn = New ComboBox
_cboColumn.DataSource = objSource.Tables(0)
_cboColumn.DisplayMember = _strMember
_cboColumn.ValueMember = _strValue
If bUseDropDownList = True Then
_cboColumn.DropDownStyle = ComboBoxStyle.DropDownList
Me.ReadOnly = True
Else
'AddHandler _cboColumn.KeyPress, AddressOf _cboColumn_KeyPress
AddHandler _cboColumn.KeyUp, AddressOf _cboColumn_KeyUp
End If
AddHandler _cboColumn.Leave, AddressOf cboColumn_Leave
_cboColumn.Visible = False
End Sub
Private Sub _cboColumn_KeyPress(ByVal sender As Object, ByVal e As
KeyPressEventArgs)
e.Handled = True
End Sub
Private Shadows Sub _cboColumn_KeyUp(ByVal sender As Object, _
ByVal e As System.Windows.Forms.KeyEventArgs)
Dim index As Integer
Dim actual As String
Dim found As String
' Do nothing for certain keys such as navigation keys
If ((e.KeyCode = Keys.Back) Or _
(e.KeyCode = Keys.Left) Or _
(e.KeyCode = Keys.Right) Or _
(e.KeyCode = Keys.Up) Or _
(e.KeyCode = Keys.Delete) Or _
(e.KeyCode = Keys.Down) Or _
(e.KeyCode = Keys.PageUp) Or _
(e.KeyCode = Keys.PageDown) Or _
(e.KeyCode = Keys.Home) Or _
(e.KeyCode = Keys.ShiftKey) Or _
(e.KeyCode = Keys.End)) Then
Return
End If
' Store the actual text that has been typed
actual = _cboColumn.Text
' Find the first match for the typed value
index = _cboColumn.FindString(actual)
' Get the text of the first match
If (index > -1) Then
found = _cboColumn.Items(index).ToString()
' Select this item from the list
_cboColumn.SelectedIndex = index
' Select the portion of the text that was automatically
' added so further typing will replace it
_cboColumn.SelectionStart = actual.Length
_cboColumn.SelectionLength = found.Length
End If
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager,
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal [ReadOnly] As
Boolean, ByVal instantText As String, ByVal cellIsVisible As Boolean)
If _bIsComboBound = False Then
_bIsComboBound = True
Me.DataGridTableStyle.DataGrid.Controls.Add(_cboColumn)
End If
_iRowNum = rowNum
_cmSource = source
_cboColumn.Font = Me.TextBox.Font
Dim anObj As Object = Me.GetColumnValueAtRow(source, rowNum)
_cboColumn.Bounds = bounds
_cboColumn.BeginUpdate()
_cboColumn.Visible = True
If Not (anObj.GetType Is System.DBNull.Value) Then
'TextObject Is System.DBNull.Value
_cboColumn.SelectedValue = anObj
Else
_cboColumn.SelectedIndex = 0
End If
_cboColumn.EndUpdate()
_cboColumn.Focus()
End Sub
Public Sub cboColumn_Leave(ByVal sender As Object, ByVal e As EventArgs)
Dim objValue As Object = _cboColumn.SelectedValue
If objValue Is Nothing Then
objValue = DBNull.Value
Else
Me.SetColumnValueAtRow(_cmSource, _iRowNum, objValue)
End If
_cboColumn.Visible = False
End Sub
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As
Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer, ByVal
backBrush As Brush, ByVal foreBrush As Brush, ByVal alignToRight As Boolean)
Dim strCountry As String = "Huh?"
Dim aRowA As DataRow()
Dim anObj As Object = Me.GetColumnValueAtRow(source, rowNum)
Dim aType As Type = anObj.GetType
If Not (aType.GetType Is System.DBNull.Value) Then
aRowA = CType(_objSource, DataTable).Select(_strValue + " = " +
anObj.ToString)
Else
aRowA = CType(_objSource, DataTable).Select
End If
If aRowA.Length > 0 Then
strCountry = aRowA(0)(_strMember).ToString
End If
Dim rect As Rectangle = bounds
Dim rectF As RectangleF = rectF.op_Implicit(rect) ' Convert to
If Me._backBrush Is Nothing Then
g.FillRectangle(backBrush, rect)
Else
g.FillRectangle(_backBrush, rect)
End If
rect.Y += 2
If Me._foreBrush Is Nothing Then
'g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font, ForeBrush, RectF,
Format)
g.DrawString(strCountry, Me.TextBox.Font, foreBrush, rectF)
Else
g.DrawString(strCountry, Me.TextBox.Font, _foreBrush, rectF)
End If
End Sub
Public WriteOnly Property backgroundColour() As System.Drawing.Color
Set(ByVal Value As System.Drawing.Color)
If Value.Equals(System.Drawing.Color.Transparent) Then
Me._backBrush = Nothing
Else
Me._backBrush = New SolidBrush(Value)
End If
End Set
End Property
Public WriteOnly Property foregroundColour() As System.Drawing.Color
Set(ByVal Value As System.Drawing.Color)
If Value.Equals(System.Drawing.Color.Transparent) Then
Me._foreBrush = Nothing
Else
Me._foreBrush = New SolidBrush(Value)
End If
End Set
End Property
End Class
End Namespace
 
----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------
'The Form's class
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Imports System.ComponentModel
Imports System.Windows.Forms
Imports ReceptNet.ComboBoxTest
Public Class DataGridSample
Inherits Form
Private ds As DataSet
Private myGrid As DataGrid
Shared Sub Main()
Application.Run(New DataGridSample)
End Sub
 
Private Sub ConnectToData()
Dim AppPath As String = Replace(Replace(Application.ExecutablePath,
Application.ProductName + ".exe", ""), Application.ProductName + ".exe", "")
& "Recept.mdb"
Dim cString As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" &
AppPath
Dim cn As System.Data.OleDb.OleDbConnection = New
System.Data.OleDb.OleDbConnection(cString)
Dim adpTables As System.Data.OleDb.OleDbDataAdapter = New
System.Data.OleDb.OleDbDataAdapter
adpTables.TableMappings.Add("Table", "Kött")
cn.Open()
Dim cmdTable As OleDb.OleDbCommand = New OleDb.OleDbCommand("SELECT * FROM
Kryddor", cn)
cmdTable.CommandType = CommandType.Text
adpTables.SelectCommand = cmdTable
ds = New DataSet("Customers")
adpTables.Fill(ds)
cn.Close()
Dim GridTableStyle As DataGridTableStyle = New DataGridTableStyle
GridTableStyle.MappingName = "Kött"
Me.myGrid.TableStyles.Add(GridTableStyle)
AddStyle(myGrid,ds)
End Sub
Private Sub AddStyle(ByVal grdSrc As DataGrid, ByVal dataS As DataSet)
Dim aCboCol As MyComboColumn = New MyComboColumn(dataS, "KöttID", "", False)
aCboCol.Width = 129
aCboCol.MappingName = "KöttID"
aCboCol.HeaderText = "Kött"
myGrid.TableStyles("Kött").GridColumnStyles.Add(aCboCol)
End Sub
Private Sub DataGridSample_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
ConnectToData()
End Sub
End Class
 
----------------------------------------------------------------------------
----------------------------------------------------------------------------
------------------------------------
 
Me.cboDatum.DataSource = ds.Tables(1)
cboDatum.DisplayMember = "KöttID"
cboDatum.ValueMember = ""
 
Last edited by a moderator:
Back
Top