Combobox in Datagridcell?

L

Lars Netzel

I want a datagrid cell to be a combobox (based on database records) and when
you select a value in the combobox I want to add info to cells on the same
row in the datagrid but with More info from the same record in the database
as the one selecte din the combobox, how do I do this???

I also would like to be able to enter a value in the combobox and a search
will take place and if the value ins NOT in the database.. you will be
prompted to add one (another form)

Is this at all possible?

/Lars
 
C

Cor Ligthert

Hi Lars,

In my first opinion, why not, however you have to do a lot of things
yourself.

Luckily there are a lot of links about this which can help you.

I hope this helps?

Cor

Complete very nice control
<http://dotnet.leadit.be/extendeddatagrid>

Combobox
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q480q

DateTimePicker (gives a description of the methode to add a control in a
datagrid)
http://msdn.microsoft.com/library/d...windowsformsdatagridcolumnstyleclasstopic.asp

Faqs and links with complete samples
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp

http://www.microsoft.com/belux/nl/msdn/community/columns/jtielens/datagrid.mspx

http://msdn.microsoft.com/library/d...ry/en-us/dnwinforms/html/wnf_CustDataGrid.asp

http://msdn.microsoft.com/library/d...ormsdatagridtextboxcolumnclassformattopic.asp
 
C

Carlos Durán Urenda

Hi Cor

any link to unload a extended datagrid example in visual Basic Net???
 
C

Cor Ligthert

Hi Carlos,

That is from Jan Tieleman, he was active a while in these newsgroups.

However here a complete sample with a combobox for what I used that sample
from that Syncfusion board which I changed slightly. (It does not go with
option strict on was one of the reasons and there is in it some not VB.net
style code)

\\\needs a datagrid and 2 buttons on a form and than it goes completly

'Used for the comboboxcolumn is a modified sample from
'syncfusion
'To start create a dataset in this probram by clicking the Read/Create and
cancel then in the openfiledialogbox.
'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 5
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) = "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) = "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 5
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) = "Other(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
End Class
 
C

Carlos Durán Urenda

Sorry

I want to obtain an example in VB.Net that explains the complete
functionality of the extended datagrid

Carlos
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top