Home All Groups Group Topic Archive Search About

VS2005 combox question

Author
7 Jan 2006 5:16 PM
Bob
The scenario is that of the employees table wherein there's an EmployeeId
and a ReportsTo field in the same table. The combobox is one in a datagrid
view that lets you select an employee from the employees table to fill in
the reportsto field, but since a person can't report to himself. I'm
wondering how to exclude the EmployeeId of the selected record from the
dropdown list of the combox. Ideally I think it would be to apply a filter
to the table that fills the combobox each time a new row is selected. Does
any one have any code snippets showing how to do that?
Any help would be appreciated.

Bob

Author
8 Jan 2006 12:46 PM
Bart Mermuys
Hi,

"Bob" <bduf***@sgiims.com> wrote in message
news:%23zXth46EGHA.916@TK2MSFTNGP10.phx.gbl...
> The scenario is that of the employees table wherein there's an EmployeeId
> and a ReportsTo field in the same table. The combobox is one in a datagrid
> view that lets you select an employee from the employees table to fill in
> the reportsto field, but since a person can't report to himself. I'm
> wondering how to exclude the EmployeeId of the selected record from the
> dropdown list of the combox. Ideally I think it would be to apply a filter
> to the table that fills the combobox each time a new row is selected. Does
> any one have any code snippets showing how to do that?
> Any help would be appreciated.

Have a look at the DataGridView faq(A.18) at:
http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=152467&SiteID=1

Applying it to your case, since you have circular reference you only need
one DataTable (and TableAdapter) eg. Employee.  But you need 3
BindingSource's.  One BindingSource is for the DataGridView, one for the
ReportsToComboBoxColumn and another only for the ComboBoxCell currently
being edited (and filtered).  It's important that only the currently editing
ComboBoxCell has a filtered BindingSource, the others need the unfiltered
BindingSource so that they can correctly paint the lookup values.

What you  need on the Form is the following:

- DataSet (eg. DataSet1) with a DataTable in it (eg. "Employee" )

- EmployeeBindingSource ( DataSource = DataSet1, DataMember="Employee" )
- ReportsToBindingSource (  DataSource = DataSet1, DataMember="Employee" )
- ReportsToFilteredBindingSource ( DataSource = DataSet1,
DataMember="Employee" )

- EmlpoyeeDataGridView ( DataSource = EmployeeBindingSource )
   - ReportsToDataGridViewColumn
           DataSource = ReportsToBindingSource
           DisplayMember="EmployeeName"
           ValueMember="EmployeeId"
           DataPropertyName="ReportsTo"

Then in code you need:

Private Sub EmployeesDataGridView_CellBeginEdit(...) Handles ...

  If (
EmlpoyeesDataGridView.Columns(e.ColumnIndex).DataPropertyName="ReportsTo" )
Then

    Dim editingCombo As DataGridViewComboBoxCell = _
       EmployeesDataGridView(e.ColumnIndex, e.RowIndex)

    ReportsToFilteredBindingSource.Filter = "EmployeeId<>" + _
       EmployeesDataGridView("EmployeeId", e.RowIndex).Value.ToString()

    editingCombo.DataSource = ReportsToFilteredBindingSource
  End If
End Sub

Private Sub EmployeesDataGridView_CellEndEdit(...) Handles ...

  If (
EmlpoyeesDataGridView.Columns(e.ColumnIndex).DataPropertyName="ReportsTo" )
Then
    Dim editingCombo As DataGridViewComboBoxCell = _
      EmployeesDataGridView(e.ColumnIndex, e.RowIndex)

    editingCombo.DataSource = ReportsToBindingSource
  End If
End Sub


HTH,
Greetings


Show quoteHide quote
>
> Bob
>
>
>
Author
8 Jan 2006 5:48 PM
Bob
Thanks a lot Bart.

Show quoteHide quote
"Bart Mermuys" <bmermuys.nospam@hotmail.com> wrote in message
news:u4MXyEFFGHA.3056@TK2MSFTNGP09.phx.gbl...
> Hi,
>
> "Bob" <bduf***@sgiims.com> wrote in message
> news:%23zXth46EGHA.916@TK2MSFTNGP10.phx.gbl...
>> The scenario is that of the employees table wherein there's an EmployeeId
>> and a ReportsTo field in the same table. The combobox is one in a
>> datagrid view that lets you select an employee from the employees table
>> to fill in the reportsto field, but since a person can't report to
>> himself. I'm wondering how to exclude the EmployeeId of the selected
>> record from the dropdown list of the combox. Ideally I think it would be
>> to apply a filter to the table that fills the combobox each time a new
>> row is selected. Does any one have any code snippets showing how to do
>> that?
>> Any help would be appreciated.
>
> Have a look at the DataGridView faq(A.18) at:
> http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=152467&SiteID=1
>
> Applying it to your case, since you have circular reference you only need
> one DataTable (and TableAdapter) eg. Employee.  But you need 3
> BindingSource's.  One BindingSource is for the DataGridView, one for the
> ReportsToComboBoxColumn and another only for the ComboBoxCell currently
> being edited (and filtered).  It's important that only the currently
> editing ComboBoxCell has a filtered BindingSource, the others need the
> unfiltered BindingSource so that they can correctly paint the lookup
> values.
>
> What you  need on the Form is the following:
>
> - DataSet (eg. DataSet1) with a DataTable in it (eg. "Employee" )
>
> - EmployeeBindingSource ( DataSource = DataSet1, DataMember="Employee" )
> - ReportsToBindingSource (  DataSource = DataSet1, DataMember="Employee" )
> - ReportsToFilteredBindingSource ( DataSource = DataSet1,
> DataMember="Employee" )
>
> - EmlpoyeeDataGridView ( DataSource = EmployeeBindingSource )
>   - ReportsToDataGridViewColumn
>           DataSource = ReportsToBindingSource
>           DisplayMember="EmployeeName"
>           ValueMember="EmployeeId"
>           DataPropertyName="ReportsTo"
>
> Then in code you need:
>
> Private Sub EmployeesDataGridView_CellBeginEdit(...) Handles ...
>
>  If (
> EmlpoyeesDataGridView.Columns(e.ColumnIndex).DataPropertyName="ReportsTo" 
> ) Then
>
>    Dim editingCombo As DataGridViewComboBoxCell = _
>       EmployeesDataGridView(e.ColumnIndex, e.RowIndex)
>
>    ReportsToFilteredBindingSource.Filter = "EmployeeId<>" + _
>       EmployeesDataGridView("EmployeeId", e.RowIndex).Value.ToString()
>
>    editingCombo.DataSource = ReportsToFilteredBindingSource
>  End If
> End Sub
>
> Private Sub EmployeesDataGridView_CellEndEdit(...) Handles ...
>
>  If (
> EmlpoyeesDataGridView.Columns(e.ColumnIndex).DataPropertyName="ReportsTo" 
> ) Then
>    Dim editingCombo As DataGridViewComboBoxCell = _
>      EmployeesDataGridView(e.ColumnIndex, e.RowIndex)
>
>    editingCombo.DataSource = ReportsToBindingSource
>  End If
> End Sub
>
>
> HTH,
> Greetings
>
>
>>
>> Bob
>>
>>
>>
>
>