Question DatagridView ComboBox remove item

renju79

New member
Joined
Dec 13, 2009
Messages
4
Programming Experience
3-5
Hello Developers..

So am back again with dattagridview and comboboxes...sorry for the troubless.. :D

Now let me come to my scenario aka problem..

I have 2 dataviewgrids in my WINDOWS APPLICATION FORM (VB.net).

The first grid populates some data from the SQLSERVER 2005 DB.

When I click on any cell of the first grid, my second grid shows up and populates with the data corresponding to the cell which I have clicked in the grid 1.This is working perfect and so far so good.

In the second grid I have 3 combobox columns and some textbox columns..The three combobox columns in this grid are also populated from the DB only usng datasets and datasource.In the first Combobox Column if I select one value, then the corresponding values are filled into the other textboxcolumns.This is also working good.The other 2 comboboxcolumns are also populated from database using different datasets..

So these were the functionalities I've implemented in my form so far.

Now here comes my required stuff..

let me detail the process here.

Am about to add a master data ( i.e data into the first grid).it got added and after that a msgbox pops up sayn u must add data into the second grid as well.so the user is adding the data in the second grid.(all works except the new requirement).he added something into the first row by selecting the comboboxcolumns and also editing the other columns in the first row.(he havent saved this row.This will happen when he clicks the save button in the form not in the grid)

he goes into the second row .Now here he should not see the item from the combobox1 which he has selected in row1.

to be more clear in row1 he selected "Item 1" in combobox1.In row2 when he comes to combobox1, there should not be "Item1" in that dropdown.
similarly in row 3 the combobox should populate with a dropdown without the values he/she has selected in row1 and row 2 and so on.....

This can be saved only after he has finished entering into all (desired no: of) rows in grid2.
How to accomplish this???this is eating my brains.... :eek:

I've added a handler to catch the combobox selected index changed event in the gridview...

I can provide the code also if required.

Thanks in advance

Renju
 
When you bind data to a DataGridViewComboBoxColumn you are binding data for every cell in that column. If you don't want the same data in every cell then you can't bind the column. You need to either set the DataSource of each cell separately, or else populate each ComboBox control as it's displayed. For the latter you would have to handle the EditingControlShowing event of the grid.
 
Hi JM,

Thanks for your reply.
I tried with your answer and I cant get through because first of all I cant avoid binding.
I have to bind the combobox with the same data from the SQL table in such a way that the first selected item is removed in the second combobox.

Am using the EditingControlShowing for the grid and in this event I have written the handler for ComboBox_SelectedIndexChanged event.

Now i have done an alternative.When I select a value in the combo, am setting a hidden field in the gridview with the same value of combo. In the sub ComboBox_SelectedIndexChanged,am checking this hidden values with the value selected in the current combo(s) in the previous row(s) .This is working fine for the newly adding rows.But for the ist row (i.e Row 0) am not able to check.

Please find the sub as below.
VB.NET:
Private Sub ComboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
        Try
            Dim comboBox1 As ComboBox = CType(sender, ComboBox)
            If dgvwCustomsItems.CurrentCell.ColumnIndex = 2 Then
                Dim rowcnt As Integer = dgvwCustomsItems.CurrentCell.RowIndex
                IIf(IsDBNull(dgvwCustomsItems.Rows(rowcnt).Cells(2).Value), 1, (dgvwCustomsItems.Rows(rowcnt).Cells(2).Value))
                If Int32.Parse(comboBox1.SelectedValue) < 0 Or TypeOf comboBox1.SelectedValue Is DataRowView Then
                    Exit Sub
                Else
                    Dim objParams As Object() = {comboBox1.SelectedValue}
                    Dim ds As New DataSet
                    ds = Master.SearchItemName(objParams)
                    dgvwCustomsItems.CurrentRow.Cells(10).Value = ds.Tables(0).Rows(0).Item(0)
                    'dgvwCustomsItems.CurrentRow.Cells(2).Value = ds.Tables(0).Rows(0).Item(1)
                    dgvwCustomsItems.CurrentRow.Cells(3).Value = ds.Tables(0).Rows(0).Item(2)
                    dgvwCustomsItems.CurrentRow.Cells(5).Value = ds.Tables(0).Rows(0).Item(3)
                    dgvwCustomsItems.CurrentRow.Cells(6).Value = ds.Tables(0).Rows(0).Item(4)
                    dgvwCustomsItems.CurrentRow.Cells(7).Value = ds.Tables(0).Rows(0).Item(5)
                    dgvwCustomsItems.CurrentRow.Cells(8).Value = ds.Tables(0).Rows(0).Item(6)
                    dgvwCustomsItems.CurrentRow.Cells(9).Value = ds.Tables(0).Rows(0).Item(7)
                End If
               [COLOR="Red"] If rowcnt > 0 Then
                    For i As Integer = 0 To rowcnt - 1
                        If comboBox1.SelectedValue = dgvwCustomsItems.Rows(i).Cells(10).Value Then
                                                            MessageBox.Show("This Custom Item is Already Selected.Please Select Another Custom Item.", grpCustomItems.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                                comboBox1.SelectedValue = dgvwCustomsItems.Rows(i).Cells(10).Value + 1
                           
                        End If
                    Next
                End If
                If newCD = False And dgvwCustomsItems.CurrentRow.Index = 0 Then
                    For j As Integer = dgvwCustomsItems.Rows.Count - 1 To 0 Step -1
                        If j - 1 > dgvwCustomsItems.Rows.Count - 1 Then
                            Exit For
                        Else
                            If comboBox1.SelectedValue = dgvwCustomsItems.Rows(j - 1).Cells(10).Value Then
                                
                                    MessageBox.Show("This Custom Item is Already Selected.Please Select Another Custom Item.", grpCustomItems.Text, MessageBoxButtons.OK, MessageBoxIcon.Information)
                                    Exit For
                                
                            End If
                        End If

                    Next j
                End If
            End If[/COLOR]
        Catch ex As Exception
            'MsgBox("Combobox Handler")
            LogError(ex.Message, Me.Name, userName)
        End Try
    End Sub

Many Thanks

Renju
 
Last edited:
Back
Top