473,224 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 software developers and data experts.

What's wrong with the combobox in grid

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
'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(_cboCo lumn)
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(aC boCol)
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 = ""
Jul 14 '06 #1
1 2060
Fiaolle,

There are a lot of combobox samples on Internet for VBNet. Some are just
changed C# one with even a look at it. We have as well to at our website.
Have a look at those.

http://www.vb-tips.com/dbpages.aspx?...6-7320e41e6d05

http://www.vb-tips.com/dbPages.aspx?...9-3866d4936de0

I don't think anybody would check and check over and over again the wrong
given combobox code for datagrids every time.

I hope this helps

Cor
"fiaolle" <fi*****@telia.comschreef in bericht
news:Zb*****************@newsb.telia.net...
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
'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(_cboCo lumn)
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(aC boCol)
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 = ""


Jul 16 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Supa Hoopsa | last post by:
I have a databound combobox within a datagrid. I have set the DisplayMember and ValueMember. BUT, how do I capture the Value (SelectedValue) when the user changes the selection?
3
by: Idelso Quintero | last post by:
Hi everyone: Could someone look into what could possibly be wrong in these lines of code? It looks all good to me but for some reason the combo box that is created, is not shown with all the...
0
by: Jesper Denmark | last post by:
Well I'm new to C# and I do not know of all of the different controls yet. The grid you are referring to is that the datagrid control, and if yes, would it be possible to use it without a database...
1
by: anonymous | last post by:
I've been trying to put a them, please help me out. Here's the major parts of my code: public Form1() { DataSet myDataSet = new DataSet("myDataSet"); DataTable testTable = new...
3
by: TT (Tom Tempelaere) | last post by:
Hay there, I'm writing my own DataGridComboBoxColumn because .NET 1.1 does not have one (I hope .NET 2.0 supplies one). I based it on this article:...
5
by: jaYPee | last post by:
i have successfully added a combobox to my datagrid by setting their datasource from one of my table. here's my code... Dim grdColStyle6 As New DataGridComboBoxColumn() With grdColStyle6...
0
by: ER | last post by:
Hi Guys Started having a play with WPF and need some direction with a grid. I am trying to build a list of products in a grid view with combox boxes as column headers that allow filtering and...
8
by: =?Utf-8?B?RyBIdXN0aXM=?= | last post by:
This is the 2nd time posting so sorry for duplications. I am using VB.NT 2005 & a standard Combobox. I've been wracking my brain over this problem for a over a month & cannot seem to find a way to...
1
by: SnakesRule | last post by:
I'm trying to pass a name from a ComboBox to a MySQL search function, but no matter how I twist it, the get() always returns the first value in the combobox list, regardless of which name the user...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.