0

The data sources have the same structure, but different data. One would be used for rows that are saved (view mode), and the other one would be for rows that are being added or edited (edit/new rows). How can that be acomplished?

I have a standard foreign key column that references a standard lookup table which has an ID, Name and Active (bit). The combo box column uses that lookup table to show the list, but only active items. Let's say a lookup item is used and later deactivated (Active = 0). The combo box column now shows errors because the ID is not found in the list. Does anyone have any ideas how to solve it?

4

1 回答 1

1

不知道你是否还在寻找,但我刚刚遇到了同样的问题。以上就是我的处理方式,希望对你有用。

定义一个函数,该函数返回具有适当数据源集的 DataGridViewComboCell(在别处定义的集合对象,请注意,我在此示例中使用 EntitySpaces 来填充 DataSource):

Private Function GetStockComboDataSource(ByVal type As eStockComboType) As DataGridViewComboBoxCell
        Try
            Dim cell As New DataGridViewComboBoxCell
            Select Case type
                Case eStockComboType.StockIn

                    cell.DataSource = Me.StockInCol.Query.LoadDataTable
                    cell.DisplayMember = "FullName"
                    cell.ValueMember = JCStockInMetadata.ColumnNames.StockItemID

                Case eStockComboType.StockItem

                    cell.DataSource = StockItemCol.Query.LoadDataTable
                    cell.ValueMember = JCStockItemMetadata.ColumnNames.StockItemID
                    cell.DisplayMember = "FullName"
            End Select

            Return cell
End Function

现在在设置组合数据源时(例如,我在这里使用 RowEnter 事件):

    Private Sub dgvStock_RowEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgvStock.RowEnter
        Try
            If IsNumeric(Me.dgvStock.Rows(e.RowIndex).Cells("ID").Value) Then
                Dim intValue As Integer = Convert.ToInt32(Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item").Value)
                Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item") = GetStockComboDataSource(eStockComboType.StockItem)
                Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item").Value = intValue
            Else
                Me.dgvStock.Rows(e.RowIndex).Cells("Stock Item") = GetStockComboDataSource(eStockComboType.StockIn)

            End If
        Catch ex As Exception
            JCExceptionLogger.LogException(ex)
        End Try

End Sub

在这里,我根据 ID 列是否为数字来切换组合 DataSource,但您可以实现所需的任何业务逻辑。请注意,我在设置组合(intValue)之前保存了单元格的值。这似乎是必要的,否则组合将显示为空白。

于 2008-11-30T16:45:14.470 回答