By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,877 Members | 1,100 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,877 IT Pros & Developers. It's quick & easy.

Combobox in a Datagrid

P: n/a
Hi,

I know there are a lot of postings out there concerning this subject
already, but so far I have not discovered anything that meets my
specific needs. Everything I have found populates the combobox column
with values from a database. I need to be able to manually set these
values in my code. I have tried to make the example code I have found
work for my particular situation but I am having a lot of trouble.
Does anyone have some advice on how to approach coding this?

Basically what I need to do is exactly what Access does - the user
specifies the list of possible values for a particular column in a
table, then Access displays this list as a combobox to the user. The
user can then select an option from the combobox and Access populates
the column/row with the specified value. And every new row contains
this combobox waiting to be selected by the user.

Any guidance would be greatly appreciated! Thanks!!

Joy
Nov 20 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Hi Simchajoy.

I think that this complete sample will help you, you can open a windoform
project and just paste it in, than add 3 buttons and a datagrid on that
form.

I hope this helps?

Cor

'\\\needs a datagrid and 3 buttons on a form
Dim ds As New DataSet("Test")
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Me.Button1.Text = "Read ds"
Me.Button2.Text = "Write ds"
Me.Button3.Text = "New ds"
End Sub
Private Sub FillGrid()
Static done As Boolean
If Not done Then
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Names"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "IdName"
textCol.HeaderText = "Id"
textCol.Width = 20
ts.GridColumnStyles.Add(textCol)
textCol = New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.GridColumnStyles.Add(cmbTxtCol)
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cmbTxtCol.ColumnComboBox.Items.Add("Austria(EU)")
cmbTxtCol.ColumnComboBox.Items.Add("Germany(EU)")
cmbTxtCol.ColumnComboBox.Items.Add("Holland(EU)")
cmbTxtCol.ColumnComboBox.Items.Add("UK(EU)")
cmbTxtCol.ColumnComboBox.Items.Add("Georgia(US)")
cmbTxtCol.ColumnComboBox.Items.Add("NewYork(US)")
cmbTxtCol.ColumnComboBox.DropDownStyle =
ComboBoxStyle.DropDownList
DataGrid1.TableStyles.Add(ts)
done = True
End If

End Sub
'make dataset
Private Sub Button3_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button3.Click
ds.Tables.Clear()
Dim dt As New DataTable("Names")
ds.Tables.Add(dt)
dt.Columns.Add("IdName")
dt.Columns.Add("Name")
dt.Columns.Add("Country")
For i As Integer = 1 To 6
Dim dr As DataRow = dt.NewRow
dr(0) = i.ToString
dt.Rows.Add(dr)
Next
dt.Rows(0)(1) = "Herfried K. Wagner"
dt.Rows(1)(1) = "Armin Zingler"
dt.Rows(2)(1) = "Ken Tucker"
dt.Rows(3)(1) = "Terry Burns"
dt.Rows(4)(1) = "Jay B Harlow"
dt.Rows(5)(1) = "Cor Ligthert"
dt.Rows(0)(2) = "Austria(EU)"
dt.Rows(1)(2) = "Germany(EU)"
dt.Rows(2)(2) = "Georgia(US)"
dt.Rows(3)(2) = "UK(EU)"
dt.Rows(4)(2) = "New York(US)"
dt.Rows(5)(2) = "Holland(EU)"
FillGrid()
End Sub
'Read dataset
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim fo As New OpenFileDialog
If fo.ShowDialog() = DialogResult.OK Then
ds.Clear()
ds.ReadXml(fo.FileName)
FillGrid()
Else
Me.Close()
End If
End Sub
'write dataset
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim of As New SaveFileDialog
If of.ShowDialog = DialogResult.OK Then
ds.WriteXml(of.FileName)
End If
End Sub
End Class
' The simple DatagridCombobox
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
Private WithEvents cmSource As CurrencyManager
Private mRowNum As Integer
Private isEditing As Boolean
Shared Sub New()
End Sub
Public Sub New()
MyBase.New()
ColumnComboBox = New NoKeyUpCombo
AddHandler ColumnComboBox.SelectionChangeCommitted, _
New EventHandler(AddressOf ComboStartEditing)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager,
_
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As
Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText,
cellIsVisible)
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
If isEditing Then
isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As
EventArgs)
isEditing = True
MyBase.ColumnStartedEditing(DirectCast(sender, Control))
End Sub
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
_
Handles ColumnComboBox.Leave
If isEditing Then
SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
isEditing = False
Invalidate()
End If
ColumnComboBox.Hide()
End Sub
End Class
Public Class NoKeyUpCombo
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg <> &H101 Then
MyBase.WndProc(m)
End If
End Sub
///
Nov 20 '05 #2

P: n/a

Thank you! That helps a lot!

Joy
*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #3

P: n/a

Hi,

This code you posted has worked great for me. However, now I have
another question that maybe you can help me with.

I have many DataGridComboBoxColumns in my datagrid and I want to execute
different code in each column when the user commits a change to the
combobox. Right now the "ComboStartEditing" sub handles the
"SelectionChangeCommitted" event for any combobox from any
DataGridComboBoxColumn.

I don't want to change this but I want to place my extra code for
handling the combobox.SelectionChangeCommitted event in my class that
builds the DataGridTableStyle.GridColumnStyles. Is this possible?? Is
it even possible to access the combobox.SelectionChangeCommitted event
from outside the DataGridComboBoxColumn instance?

Thanks for all the help!!

Joy

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 20 '05 #4

P: n/a
Hi Joy,

I think that this will fits what you need

\\\
AddHandler CType(cmbTextColumn.ColumnComboBox, _
ComboBox).SelectionChangeCommitted, AddressOf mychange
///
\\\
Private Sub mychange(ByVal sender As Object, ByVal e As System.EventArgs)
MessageBox.Show("I was changed")
End Sub
///

I hope this helps?

Cor
Nov 20 '05 #5

P: n/a

i need a datagridcombobox component
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 21 '05 #6

P: n/a
Saeed,

When you have no datarelations than this sample will maybe help (the class
is at the bottom)
http://groups.google.com/groups?selm...tngp13.phx.gbl
When you have datarelations than this sample
http://groups.google.com/groups?selm...TNGP10.phx.gbl

You can try both, however check yourself of course.

I hope this helps?

Cor
Nov 21 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.