|
-
Oct 29th, 2010, 12:52 AM
#12
Re: [.NET 2.0+] Adding a ComboBox Column to a DataGridView
 Originally Posted by kotlet
Nice work
But i have one problem and dont know how to solve it. I already have appplication with a lot of combos and textboxes on form (32 rows, in each row 5 combos and 5 texboxes and lot of code behind). Problem is that curent app is very slow on form load. With your sample i could make it a lot faster.
Already have gridview filled with some test data.
And the problem is:
When i click on combobox in use this code to display dropdown (to click only once to display dropdown).
When i click on item in dropdown the cell is filled with selected text. But CellValueChanged event is fired when i go out from cel in clicked on other cell. Now how i set that value is changed when i click on item in dropdown, or to catch this event?
I need this for enable or disable (ready only) next cell in gridview.
An one more thing.
I want to add one column in gridview like row number, starting with 0.
What you think you need, you don't need. First up, you don't need any code to make the combo boxes drop down with a single click. You simply set the EditMode property to EditOnEnter.
As for the cell value changing, whether it changes as soon as the user makes a selection or when they navigate away is irrelevant. To navigate to another cell they must navigate away from the current cell, so the CellValueChanged event will be raised and you can configure the other cells in the row regardless. Here's an update to the code from the first post in this thread:
Code:
Private Sub Form1_Load(ByVal sender As Object, _
ByVal e As EventArgs) Handles MyBase.Load
Me.BindingSource2.DataSource = Me.GetParentTable()
Me.Column1.DisplayMember = "ParentName"
Me.Column1.ValueMember = "ParentID"
Me.Column1.DataSource = Me.BindingSource2
Me.BindingSource1.DataSource = Me.GetChildTable()
Me.DataGridView1.DataSource = Me.BindingSource1
End Sub
Private Function GetParentTable() As DataTable
Dim table As New DataTable
With table.Columns
.Add("ParentID", GetType(Integer))
.Add("ParentName", GetType(String))
End With
With table.Rows
.Add(1, "Parent 1")
.Add(2, "Parent 2")
.Add(3, "Parent 3")
End With
Return table
End Function
Private Function GetChildTable() As DataTable
Dim table As New DataTable
With table.Columns
.Add("ChildID", GetType(Integer))
.Add("ParentID", GetType(Integer))
.Add("ChildName", GetType(String))
End With
With table.Rows
.Add(1, 3, "Child 1")
.Add(2, 2, "Child 2")
.Add(3, 1, "Child 3")
End With
Return table
End Function
Private Sub DataGridView1_CellValueChanged(ByVal sender As Object, _
ByVal e As DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged
Dim row = e.RowIndex
Dim column = e.ColumnIndex
If column = 1 Then
If CInt(Me.DataGridView1(column, row).Value) = 2 Then
Me.DataGridView1(0, row).ReadOnly = True
Me.DataGridView1(2, row).ReadOnly = True
Else
Me.DataGridView1(0, row).ReadOnly = False
Me.DataGridView1(2, row).ReadOnly = False
End If
End If
End Sub
Also, set the EditMode of the grid in the designer. Now, try running that code and editing the values in the first column. You'll find that the combo boxes drop down on the first click, assuming that you click the drop-down arrow and not the text area. Also note that, if you select 'Parent 2', the rest of the cells in the row become read-only while, if you select a different parent, the other cells are editable.
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
|