add a comboBox in DataGridView

Socarsky

Well-known member
Joined
Dec 27, 2012
Messages
173
Location
Jakarta/Indonesia
Programming Experience
Beginner
I am tracking of below to add a comboBox in DataGridView, I am still searching to find a good one for me. I only fill a comboBox with data from ms sql and type a couple cells then records all cells into ms sql. My laptop's SQL Server does not work on WiFi and I cannot try below but I could only type them... Let me try it tomorrow when I go to company. That's why I am not sure below code whether is suit.


VB.NET:
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        cmbBox = New ComboBox
        cmbBox.Visible = False
        DataGridView1.Controls.Add(cmbBox)


VB.NET:
Private Sub DataGridView1_CellBeginEdit(sender As Object, e As DataGridViewCellCancelEventArgs) Handles DataGridView1.CellBeginEdit
        Try
            If ((DataGridView1.Focused) OrElse (DataGridView1.CurrentCell.ColumnIndex = 4)) Then
                cmbBox.Location = DataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, False).Location
                cmbBox.Visible = True
                If (DataGridView1.CurrentCell.Value IsNot DBNull.Value) Then
                    cmbBox.SelectedValue = DataGridView1.CurrentCell.Value
                Else
                    cmbBox.SelectedValue = DateTime.Today.ToString
                End If
            Else
                cmbBox.Visible = False
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
End Sub

VB.NET:
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
        Try
            If ((DataGridView1.Focused) OrElse (DataGridView1.CurrentCell.ColumnIndex = 4)) Then
                DataGridView1.CurrentCell.Value = cmbBox.SelectedValue
            End If
        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try
    End Sub
 
Do you want a full column of combo boxes or you just want it in one or two cells here and there? Either way, you're doing it wrong. Let us know which you want and we can suggest the correct approach.
 
Do you want a full column of combo boxes or you just want it in one or two cells here and there? Either way, you're doing it wrong. Let us know which you want and we can suggest the correct approach.
I need to do two the captures, below as DataGridView. I know they are not DataGridView and used textbox buttons. I just need to do that one combobox and I fill the other cell by typing and the other form must have two combobox and the other cells must fill out by typing too.

nebimiletisim2.png

nebimiletisim.png
 
Last edited:
Yeah, yeah, whatever. Now answer my question. Do you want a whole column of combo boxes in your grid or do you just want certain individual cells to contain combo boxes and other cells in that column to be text boxes or labels or whatever?
 
Yeah, yeah, whatever. Now answer my question. Do you want a whole column of combo boxes in your grid or do you just want certain individual cells to contain combo boxes and other cells in that column to be text boxes or labels or whatever?
Actually It would be nice to see a combobox arrow means control appearing after click its currentcell location, or textbox button such as on those screen captures. Others might be textboxes.
 
So you're saying that, in the same column, some cells could be combo boxes and some cells could be text boxes, correct? Or are you talking about different columns and all the controls in each individual column will be the same? It would be nice if you could just give a straight answer because asking the same question three times will be my limit.
 
Of course I am talking about one or a couple columns in some rows cell which might a DataGridView have that. Lets say there is a DataGridView which have three columns as one of them DateTimePIcker and another one is a combobox and the last one is a textbox. You can considered that.
 
Obviously English is not your first but you have just managed to contradict yourself again, first saying that it's just some cells and then saying that its all cells in a column. Please just pick which of these statements describes what you want and answer 1 or 2:

1. Every cell in one particular column will contain a combo box.

2. The same column will contain cells with different editing controls, some combo boxes and some text boxes and/or some other type(s).
 
The answer is 1)
Btw, its paralel with your saying in 2) that I saw a dotnetbar demo which is a componet developer company as you know that as DataGridView has contain a few different controls in [a cell] but I never mentioned that in above comments. That one was fantastic one :)
 
Thanks I have already used your tutorial in this morning and I only see something strange that combobox controls alike a button there was not appear a itemlist member when I clicked on the combobox arrow. I hope I was wrong.
 
Back
Top