Combobox and datagrid

E

Echo

Hi all. How are you? If you don't mind, I need some help pleeze.
Firstly, I have a datagrid in which the values I want are added(works)
Now I want to add a combobox next to each value that I need to change.
Problem is I'm new to C# and don't Know how to add the combobox into
the datagrid. Any code in C# would be helpfull. I looked through the
groups and found something but its in vb and don't understand it>>>>

\\\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(textCo­l)
textCol = New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCo­l)
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.GridColumnStyles.Add(cmbTxt­Col)
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Heig­ht + 3)

cmbTxtCol.ColumnComboBox.DataS­ource = ds.Tables(1)
cmbTxtCol.ColumnComboBox.Displ­ayMember = "Country"
cmbTxtCol.ColumnComboBox.Value­Member = "IdCountry"
cmbTxtCol.ColumnComboBox.DropD­ownStyle =
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(dcIdCoun­try)
dtCountry.Columns.Add(dcCountr­y)
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.SelectionChange­Committed, _
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(Di­rectCast(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
///

If somebody out there can help me in translating it to c# I will
appreciate it to the ends of time.

thanxs in advance.
 
E

Echo

Thanx for the link Will look at it shortly.

BTW Thanx for replying. Do you know that you are the first to reply to
me of all the messages I have posted on the groups? Hope to use your
help again later
 
G

Guest

That's a funny coincidence :)

I don't usually pay attention to the name of a poster.
 

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