-
Apr 13th, 2008, 10:19 AM
#1
Thread Starter
Hyperactive Member
DataGridView column type question
hello all,
I want to ask I have dropped the dataset and create a DataGridView, but how to change one of the specific column to combobox?
it is because something I just want user to select in combobox and update it to mysql database.
-
Apr 13th, 2008, 11:25 AM
#2
Fanatic Member
Re: DataGridView column type question
dear friend,
please explain your need clearly, understandable. so that we can help you
-
Apr 13th, 2008, 12:48 PM
#3
Thread Starter
Hyperactive Member
Re: DataGridView column type question
As you can see the red pointer on pic, I want the user can select the position, but not type by himself
-
Apr 13th, 2008, 01:13 PM
#4
Fanatic Member
Re: DataGridView column type question
dear friend,
use this code:
Code:
Private Sub DataGridView1_CellBeginEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
If e.ColumnIndex = 3 AndAlso e.RowIndex = DataGridView1.Rows.Count - 1 Then
e.Cancel = True
End If
End Sub
this works for me
-
Apr 13th, 2008, 01:17 PM
#5
Fanatic Member
Re: DataGridView column type question
hey man,
follow this simple method:
Code:
DataGridView1.DataSource = dt
DataGridView1.Columns(3).ReadOnly = True
hope this helps
-
Apr 13th, 2008, 01:34 PM
#6
Thread Starter
Hyperactive Member
Re: DataGridView column type question
what are the different between them?
-
Apr 14th, 2008, 01:39 AM
#7
Re: DataGridView column type question
Neither of those code snippets are going to help you put a combo box in a DataGridview.
The thing is, you can't change the type of a column in a DataGridView. Once the column is created it is what it is. What you have to do is add a combo box column yourself. If you do it properly the grid will not create a text box column for that column from the data source.
In the designer you need to add a column to your grid and set its type appropriately. You then set its DataPropertyName property to the name of the data source column you want bound to it. In your case that's likely "Position". When you bind the data to the grid, instead of the grid creating a new column for that data it will bind it to the existing column.
Now, to populate the drop down list for each cell you need to bind a second data source directly to the column. You do that just as you do for a ComboBox control. You can either use its DataSource, DisplayMember and ValueMember properties or add directly to its Items collection.
-
Apr 14th, 2008, 02:00 PM
#8
Thread Starter
Hyperactive Member
Re: DataGridView column type question
I think the best way to solve the problem is that I can create a combobox outside the datagird view, when the user click on one of the "position" rows, the combobox which contain the hand-code item will automatically show out. Then the user select one of the item, the selecting rows will be changed to the selected item value.Would it be possible on VB code?
-
Apr 14th, 2008, 02:38 PM
#9
Re: DataGridView column type question
All you need to do is add a datagridviewcomboboxcolumn to your datagridview in design view. You then set those properties for that column:
DataSource = the datasource you want to use (i.e a bindingsource or a datatable)
DataPropertyName = the name of the column/field in your datasource you want for this column
ValueMember = the name of the column/field in your datasource that you want for selected values of the combobox.
Note that if you don't have a datasource until runtime, you'll have to set these properties in code.
-
Apr 15th, 2008, 12:57 PM
#10
Thread Starter
Hyperactive Member
Re: DataGridView column type question
ha, I have changed the column property from textbox to combobox, but it seem that doesn't allow me to do so. Do anyone thinks my method in #8 working?
-
Apr 15th, 2008, 03:41 PM
#11
Re: DataGridView column type question
Did you read post#7 and post#9?
You simply can't change the type of a datagridview column once it is created. So you have to create the column of your chosen type first, then tell it where to get the data from.
-
Apr 16th, 2008, 11:01 AM
#12
Thread Starter
Hyperactive Member
Re: DataGridView column type question
Originally Posted by stanav
Did you read post#7 and post#9?
You simply can't change the type of a datagridview column once it is created. So you have to create the column of your chosen type first, then tell it where to get the data from.
I know
But I mean that there are external combobox which containing the hand code item, when the row is selected, then u select the item text in combobox, the text will auto-input into the selected row, I think this way can ignore the datatype of column because it is just a string.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|