Results 1 to 12 of 12

Thread: DataGridView column type question

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    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.

  2. #2
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: DataGridView column type question

    dear friend,
    please explain your need clearly, understandable. so that we can help you
    Loving dotnet

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    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

  4. #4
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    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
    Loving dotnet

  5. #5
    Fanatic Member karthikeyan's Avatar
    Join Date
    Oct 2005
    Location
    inside .net
    Posts
    919

    Re: DataGridView column type question

    hey man,

    follow this simple method:

    Code:
     DataGridView1.DataSource = dt
            DataGridView1.Columns(3).ReadOnly = True
    hope this helps
    Loving dotnet

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: DataGridView column type question

    what are the different between them?

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,348

    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.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    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?

  9. #9
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    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.

  10. #10

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    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?

  11. #11
    PowerPoster stanav's Avatar
    Join Date
    Jul 2006
    Location
    Providence, RI - USA
    Posts
    9,289

    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.

  12. #12

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2007
    Location
    Hong Kong
    Posts
    384

    Re: DataGridView column type question

    Quote 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
  •  



Click Here to Expand Forum to Full Width