473,385 Members | 1,661 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,385 software developers and data experts.

datagrid with a combobox

OK. So I've been to http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp and learned a lot about what I might do with a datagrid. But I'm STILL not able to do what I want to do. I want to be able be able to enter data into my grid cell using a combobox. I want the combobox to display items based on my selected DisplayMember and update the datasource based on my selected ValueMember. So far, so good. But when the user leaves the combobox cell, I want the value of the DisplayMember to be displayed, not the ValueMember. The closest I've come to this is to Paint all the cells in the column with the same (most recently selected) DisplayMember value

While I'm at it, I'd like to derive an inherited ComboBox that has a ValueMember, a DisplayMember, and a COLLECTION of DisplayColumns in the DropDown list. Any help

Thanks

Pat
Nov 20 '05 #1
2 1551
Hi Pat,

Did you try this sample that I have made with as base the comboboxsample on
Syncfusion (which I modified). The modifed combobox class is at the end.

I hope this helps?

Cor
'\\\needs a datagrid and 2 buttons on a form
'Used for the comboboxcolumn is a modified sample from
'syncfusion
'To start do the Read/Create ds and cancel direct,
' than a start dataset will be created
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/Create ds"
Me.Button2.Text = "Write ds"
End Sub
Private Sub FillGrid()
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.DataSource = ds.Tables(1)
cmbTxtCol.ColumnComboBox.DisplayMember = "Country"
cmbTxtCol.ColumnComboBox.ValueMember = "IdCountry"
cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
DataGrid1.TableStyles.Add(ts)
End Sub
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
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.ReadXml(fo.FileName)
Else
Dim dtName As New DataTable("Names")
Dim dcIdName As New DataColumn("IdName")
Dim dcName As New DataColumn("Name")
Dim dcCountryN As New DataColumn("Country")
dtName.Columns.Add(dcIdName)
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountryN)
ds.Tables.Add(dtName)
For i As Integer = 1 To 6
Dim dr As DataRow = dtName.NewRow
dr(0) = i.ToString
dtName.Rows.Add(dr)
Next
dtName.Rows(0)(1) = "Herfried K. Wagner"
dtName.Rows(1)(1) = "Armin Zingler"
dtName.Rows(2)(1) = "Ken Tucker"
dtName.Rows(3)(1) = "CJ Taylor"
dtName.Rows(4)(1) = "Jay B Harlow"
dtName.Rows(5)(1) = "Cor Ligthert"
dtName.Rows(0)(2) = "Austria(EU)"
dtName.Rows(1)(2) = "Germany(EU)"
dtName.Rows(2)(2) = "Georgia(US)"
dtName.Rows(3)(2) = "Illinois(US)"
dtName.Rows(4)(2) = "New York(US)"
dtName.Rows(5)(2) = "Holland(EU)"
Dim dtCountry As New DataTable("Countries")
Dim dcIdCountry As New DataColumn("IDCountry")
Dim dcCountry As New DataColumn("Country")
dtCountry.Columns.Add(dcIdCountry)
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 1 To 6
Dim dr As DataRow = dtCountry.NewRow
dr(0) = i.ToString
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(1) = "Austria(EU)"
dtCountry.Rows(1)(1) = "Germany(EU)"
dtCountry.Rows(2)(1) = "Holland(EU)"
dtCountry.Rows(3)(1) = "Georgia(US)"
dtCountry.Rows(4)(1) = "Illinois(US)"
dtCountry.Rows(5)(1) = "New York(US)"
End If
FillGrid()
End Sub
End Class
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
Thanks for the response

This looks very similar to the syncfusion implementation. I have not actually tried to run yours, but I still don't see how it will work as I want. Specifically, I want to DISPLAY the ComboBox's DisplayMember value in the datagrid, but STORE the ComboBox's ValueMember in the DataGrid's datasource. It appears that your example (and all others that I've looked at), either store AND display the DisplayMember, or store AND display the ValueMember. Here is a listing of what I'm trying to do by intercepting the Paint method. It doesn't actually work, but I THINK I'm on the right track. In particular, have a look at the GetColumnTextAtRow function. Any advice

Public Class DataGridComboBoxColum
Inherits DataGridColumnStyl

' UI constants

Private xMargin As Integer =
Private yMargin As Integer =
Private WithEvents Combo As DataGridComboBo
Private WithEvents tb As TextBo
Private _DisplayMember As Strin
Private _ValueMember As Strin
Private _rowNum As Intege
Private WithEvents _source As CurrencyManage

' Used to track editing stat

Private OldVal As String = String.Empt
Private InEdit As Boolean = Fals
Public Property ComboBox() As DataGridComboBo
Ge
Return Comb
End Ge
Set(ByVal Value As DataGridComboBox
Combo = Valu
End Se
End Propert

Protected Overloads Overrides Sub Paint(ByVal g As Graphics,
ByVal Bounds As Rectangle,
ByVal Source As CurrencyManager,
ByVal RowNum As Integer,
ByVal AlignToRight As Boolean
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum)
PaintText(g, Bounds, Text, AlignToRight
End Su

Private Function GetText(ByVal Value As Object) As Strin
If Not Value Is Nothing The
Debug.WriteLine("GetText(" & Value.ToString & ")"
Els
Debug.WriteLine("GetText(Value is Nothing)"
End I
If Value Is System.DBNull.Value Then Return NullTex

If Not Value Is Nothing The
Debug.WriteLine(Value.ToString
Return Value.ToStrin
Els
Debug.WriteLine("Value is Nothing"
Return String.Empt
End I

End Functio

Private Sub PaintText(ByVal g As Graphics,
ByVal Bounds As Rectangle,
ByVal Text As String,
ByVal AlignToRight As Boolean

Debug.WriteLine("PaintText(1)"
Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor
Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight
End Su

Private Function GetColumnTextAtRow(ByVal Source As CurrencyManager, ByVal RowNum As Integer) As Objec
Dim value As Object = Me.GetColumnValueAtRow(Source, RowNum
Dim dSource As Objec
Dim dr As DataRo

dSource = Combo.DataSourc
If value Is System.DBNull.Value The
Return NullTex
End I
If Not dSource Is Nothing The
If TypeOf dSource Is DataTable The
dSource = CType(dSource, DataTable
If Not dSource.Columns.Contains(Combo.DisplayMember)
OrElse Not dSource.Columns.Contains(Combo.ValueMember) The
Return NullTex
End I
For Each dr In dSource.Row
If value = dr(Combo.ValueMember) The
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
ElseIf TypeOf dSource Is DataView Then
dSource = CType(dSource, DataView)
If Not dSource.Table.Columns.Contains(Combo.DisplayMember ) _
OrElse Not dSource.Table.Columns.Contains(Combo.ValueMember) Then
Return value
End If
For Each dr In dSource.Table.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
End If
End If
End Function

Private Sub Combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectedIndexChanged
tb.Text = CType(sender, ComboBox).Text
End Sub

Public Class DataGridComboBox
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub

Private _modified As Boolean = False

Public isInEditOrNavigateMode As Boolean = True

Public Property Modified() As Boolean
Get
Return _modified
End Get
Set(ByVal Value As Boolean)
_modified = Value
End Set
End Property

End Class

End Class
Nov 20 '05 #3

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

Similar topics

0
by: Gamze | last post by:
Hi, How can i get values from datagrid to combobox and should select the same name as in datagrid row on the combobox control In my vb.net windows application ,i have combobox which is...
3
by: Bill C. | last post by:
Hello, I know this has been discussed a lot already because I've been searching around for information the last few weeks. I'm trying to implement a DataGridComboBoxColumn class. I've found...
2
by: Bill C. | last post by:
Hi, I'm trying to implement a ComboBox drop-down column for a DataGrid. When a cell is selected in the ComboBox column I overlay a ComboBox over the cell and call: this.comboBox.Show();...
3
by: Kevin | last post by:
Hi Al I want to add two combobox columns in my datagrid. the one combobox column must be bound to the same datasource that the datagrid is, and the other combobox I just want to populate with a...
3
by: PeterZ | last post by:
G'day, After doing much searching and pinching bits of ideas from here there and everywhere I came up with a fairly 'clean' solution of including a comboBox into a dataGrid column. You can...
2
by: pei_world | last post by:
I want to implement a key hit with enter to dropdown a combobox that is in the datagrid. in this case I need to override its original behaviours. I found some codes from the web. Does anyone know...
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:...
3
by: Doug | last post by:
Hi I have the following code (not mine) that populates a datagrid with some file names. But I want to replace the datagrid with a combo box. private void OnCurrentDataCellChanged(object sender,...
1
by: jimb | last post by:
I can get the dropdownlist into the datagrid, and I can populate it, but I can't read it. Anybody have a working example of a dropdownlist in an editable grid? Thanks. -- .....
4
by: Jan Nielsen | last post by:
Hi all I'm a former Access developer who would like to implement a many-to-many relation in about the same way you do in Access: With a subform and a combo box. Is it possible to use a...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.