DataGridView and ComboBox

G

Guest

Hi,

Forgive me for posting this message in 2 groups -- it appears that the Data
Access group isn't very active, so I'm posting again here:

I have a DataTable that I want to fill using a DataGridView. I've set the
DataSource of the DataGridView to my DataTable. My problem is that I want
some of the columns to be drop downs. For example, the user will need to
enter a "product" into one of the columns. There is a list of "products" in
another DataTable. I would like the user to be able to click a drop down in
my DataGridView to select the "product' that they want.

I'm not using the the designer to set this stuff up. The DataTables are
instantiated in the code. The DataGridView is now being handled in the code,
but this isn't necessary.

I've tried to make sense of the help screens and also some outside
references, but without any success. I'd really appreciate either some
guidance as to how to do this, or a reference that would help me.

Thanks very much,

Art
 
R

ronchese

Are you not using VS 2005? The new DataGrid have this feature natively. The
new DataGrid is awesome.

If you are using VS 2003, you need do a very hard job to insert a
well-working combobox in the DataGrid. So, look for articles on Internet to
help (a bit) you do that.
Personally, I think is better you consider upgrading your VS, if is the
case.

I told the new DataGrid is awesome, already???

:^P
Cesar
 
G

Guest

Actually I am using VS2005. I see that I can tell a column to be a ComboBox
-- that works. Unfortunately I can't seem to figure out how to tell it where
to get the data for the list. The GridView itself has a different DataTable
as its source.

Art
 
R

ronchese

You need to set some properties of the combobox column. Check the full sample below:


Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim dtr As DataRow

'create main table
Dim dtbMain As New DataTable
dtbMain.Columns.Add("people_name", GetType(String))
dtbMain.Columns.Add("gender_id", GetType(Integer))

'fill main table
'1
dtr = dtbMain.NewRow
dtr.Item("people_name") = "Cinthia"
dtr.Item("gender_id") = 2
dtbMain.Rows.Add(dtr)
'2
dtr = dtbMain.NewRow
dtr.Item("people_name") = "Peter"
dtr.Item("gender_id") = 1
dtbMain.Rows.Add(dtr)
'3
dtr = dtbMain.NewRow
dtr.Item("people_name") = "Mary"
dtr.Item("gender_id") = 2
dtbMain.Rows.Add(dtr)

'create gender table
Dim dtbGender As New DataTable
dtbGender.Columns.Add("gender_id", GetType(Integer))
dtbGender.Columns.Add("gender_name", GetType(String))

'fill genders
'1
dtr = dtbGender.NewRow
dtr.Item("gender_id") = 1
dtr.Item("gender_name") = "Male"
dtbGender.Rows.Add(dtr)
'2
dtr = dtbGender.NewRow
dtr.Item("gender_id") = 2
dtr.Item("gender_name") = "Female"
dtbGender.Rows.Add(dtr)

With DataGridView1
.Columns.Clear()
.AutoGenerateColumns = False

Dim colName As DataGridViewTextBoxColumn
colName = New DataGridViewTextBoxColumn
colName.Name = "name"
colName.HeaderText = "Name"
colName.DataPropertyName = "people_name"

Dim colGender As DataGridViewComboBoxColumn
colGender = New DataGridViewComboBoxColumn
colGender.Name = "gender"
colGender.HeaderText = "Gender"
colGender.DataPropertyName = "gender_id"
colGender.DataSource = dtbGender
colGender.DisplayMember = "gender_name"
colGender.ValueMember = "gender_id"

.Columns.Add(colName)
.Columns.Add(colGender)

.DataSource = dtbMain
End With
End Sub


[]s
Cesar
 

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