Editable Datagrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Faith

    Editable Datagrid

    I found code on www.codeproject.com. Here is a sample it
    is called DataGridColumnD ropDown. I modified it to use
    the text box when I wanted it to depended on a certain
    search criteria.

    Public Class ExtendedSearchF ieldValueColumn Style
    Inherits DataGridTextBox Column
    Public WithEvents ColumnComboBox As ComboBox
    Public WithEvents ColumnTextBox As TextBox

    Private _source As System.Windows. Forms.CurrencyM anager
    Private _rowNum As Integer
    Private _isEditing As Boolean
    Public Shared _RowCount As Integer


    Public Sub New()
    _source = Nothing
    _isEditing = False
    _RowCount = -1

    ColumnComboBox = New NoKeyUpCombo
    ColumnComboBox. DropDownStyle =
    ComboBoxStyle.D ropDown

    AddHandler ColumnComboBox. Leave, AddressOf
    LeaveComboBox
    AddHandler
    ColumnComboBox. SelectionChange Committed, AddressOf
    ComboStartEditi ng
    AddHandler Me.TextBox.Ente r, AddressOf
    ComboStartEditi ng
    AddHandler Me.TextBox.Leav e, AddressOf LeaveTextBox
    End Sub 'New

    Private Sub HandleScroll(By Val sender As Object, ByVal
    e As EventArgs)
    If ColumnComboBox. Visible Then
    ColumnComboBox. Hide()
    End If
    End Sub 'HandleScroll

    Private Sub ComboStartEditi ng(ByVal sender As Object,
    ByVal e As EventArgs)
    MyBase.ColumnSt artedEditing(se nder)
    End Sub 'ComboMadeCurre nt


    Private Sub LeaveComboBox(B yVal sender As Object,
    ByVal e As EventArgs)
    ColumnComboBox. Hide()
    AddHandler Me.DataGridTabl eStyle.DataGrid .Scroll,
    New EventHandler(Ad dressOf HandleScroll)
    End Sub 'LeaveComboBox

    Private Sub LeaveTextBox(By Val sender As Object, ByVal
    e As EventArgs)
    Me.TextBox.Hide ()
    End Sub

    Protected Overloads Overrides Sub Edit(ByVal
    currencySource As System.Windows. Forms.CurrencyM anager,
    ByVal rowNum As Integer, ByVal bounds As
    System.Drawing. Rectangle, ByVal isReadOnly As Boolean,
    ByVal instantText As String, ByVal cellIsVisible As
    Boolean)
    Try
    MyBase.Edit(cur rencySource, rowNum, bounds,
    isReadOnly, instantText, cellIsVisible)

    _rowNum = rowNum
    _source = currencySource

    ' Get to get the
    ExtendedSearchD ata.ExtendedSea rch.FieldName value for the
    current row
    Dim currentRow As DataRowView = CType
    (currencySource .Current, DataRowView)
    Dim currentValue As Object = currentRow
    ("RefTrendSearc hFieldID")
    ' If the current value is null then exit
    function
    If currentValue Is DBNull.Value Then Exit Sub
    ' otherwise
    ' Lookup the ExtendedSearchI nfo row based on
    the value
    Dim refTrendSearchF ieldID As Integer = CType
    (currentValue, Integer)
    Dim refTrendSearchF ield As
    ExtendedSearchI nfoDataset.RefT rendSearchField DataTable
    refTrendSearchF ield =
    ExtendedSearchS ystem.GetRefTre ndSearchFieldBy TrendSearchFie
    ldID(refTrendSe archFieldID)
    ' Just in case
    If refTrendSearchF ield.Rows.Count = 0 Then
    Exit Sub

    Dim refTrendSearchF ieldRow As
    ExtendedSearchI nfoDataset.RefT rendSearchField Row
    refTrendSearchF ieldRow =
    refTrendSearchF ield.Rows(0)

    ' Show the text box or combobox based on the
    ExtendedSearchI nfo.DataGridCel lType field
    Select Case
    refTrendSearchF ieldRow.sDataGr idCellType.Trim ()
    Case "D"
    currentRow("Col umnType") = "D"
    ColumnComboBox. Parent =
    Me.TextBox.Pare nt
    ColumnComboBox. Location =
    Me.TextBox.Loca tion
    ColumnComboBox. Size = New Size
    (Me.TextBox.Siz e.Width, ColumnComboBox. Size.Height)
    ColumnComboBox. ValueMember
    = "lookupValu e"
    ColumnComboBox. DisplayMember
    = "Display"
    Dim data As DataTable =
    GetCurrentRowFi eldValue(curren cySource)
    ColumnComboBox. DataSource = data
    If Not currentRow("Dis playValue") Is
    DBNull.Value Then
    ColumnComboBox. SelectedIndex =
    ColumnComboBox. FindStringExact (CType(currentR ow
    ("DisplayValue" ), String))
    End If
    'ColumnComboBox .Text = Me.TextBox.Text
    Me.TextBox.Visi ble = False
    ColumnComboBox. Visible = True
    AddHandler
    Me.DataGridTabl eStyle.DataGrid .Scroll, AddressOf
    HandleScroll
    ColumnComboBox. BringToFront()
    ColumnComboBox. Focus()
    Case "T"
    currentRow("Col umnType") = "T"
    Me.ColumnComboB ox.Visible = False
    Me.TextBox.Visi ble = True
    Me.TextBox.Brin gToFront()
    Me.TextBox.Focu s()
    End Select
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    End Try
    End Sub 'Edit

    Private Function GetCurrentRowFi eldValue(ByVal
    currencySource As System.Windows. Forms.CurrencyM anager) As
    ExtendedSearchI nfoDataset.Comb oResultsDataTab le
    ' Method returns the value of the FieldName column
    in the current row
    Dim currentRow As DataRowView = CType
    (currencySource .Current, DataRowView)
    Dim currentValue As Object = currentRow
    ("RefTrendSearc hFieldID")
    ' If the current value is null then exit function
    If currentValue Is DBNull.Value Then Exit Function
    ' otherwise
    ' Lookup the ExtendedSearchI nfo row based on the
    value
    Dim refTrendSearchF ieldID As Integer = CType
    (currentValue, Integer)
    Dim refTrendSearchF ield As
    ExtendedSearchI nfoDataset.RefT rendSearchField DataTable
    Dim refTrendSearchF ieldRow As
    ExtendedSearchI nfoDataset.RefT rendSearchField Row
    refTrendSearchF ield =
    ExtendedSearchS ystem.GetRefTre ndSearchFieldBy TrendSearchFie
    ldID(refTrendSe archFieldID)
    refTrendSearchF ieldRow = refTrendSearchF ield.Rows
    (0)
    Dim valueInfo As
    ExtendedSearchI nfoDataset.Comb oResultsDataTab le =
    ExtendedSearchS ystem.GetComboB oxTypes
    (refTrendSearch FieldRow.RefTre ndSearchFieldID ,
    refTrendSearchF ieldRow.sDropDo wnSource)
    Dim valueInfoRow As
    ExtendedSearchI nfoDataset.Comb oResultsRow

    Return valueInfo
    End Function


    Protected Overrides Function Commit(ByVal dataSource
    As System.Windows. Forms.CurrencyM anager, ByVal rowNum As
    Integer) As Boolean
    Try
    Dim row As DataRowView = CType
    (dataSource.Cur rent, DataRowView)
    Dim value As String
    If row("ColumnType ") Is DBNull.Value Then
    Return False

    If CType(row("Colu mnType"), String) = "D" Then
    value = ColumnComboBox. Text
    Else
    value = TextBox.Text
    End If
    If value.Length = 0 Then
    Return False
    Else
    SetColumnValueA tRow(dataSource , rowNum,
    value)
    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    End Try
    Return True
    End Function 'Commit

    Protected Overrides Function GetColumnValueA tRow(ByVal
    currencySource As System.Windows. Forms.CurrencyM anager,
    ByVal rowNum As Integer) As Object
    Try
    Dim row As DataRowView = CType
    (currencySource .List, DataView)(rowNu m)
    Return row("DisplayVal ue")
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    End Try
    End Function 'GetColumnValue AtRow


    Protected Overrides Sub SetColumnValueA tRow(ByVal
    currencySource As System.Windows. Forms.CurrencyM anager,
    ByVal rowNum As Integer, ByVal value As Object)
    Try
    Dim s As Object = value

    Dim row As DataRowView = CType
    (currencySource .Current, DataRowView)

    row("DisplayVal ue") = s
    If CType(row("Colu mnType"), String) = "D"
    Then ' Combobox
    ' Lookup the combobox value by the display
    value
    Dim dt As DataTable = CType
    (Me.ColumnCombo Box.DataSource, DataTable)
    Dim rows() As DataRow = dt.Select("")
    Dim rowCount As Integer = rows.Length
    Dim i As Integer = 0
    Dim s1 As Object

    'if things are slow, you could order your
    dataview
    '& use binary search instead of this
    linear one
    While i < rowCount
    s1 = rows(i)
    (Me.ColumnCombo Box.DisplayMemb er)
    If (Not s1 Is DBNull.Value) AndAlso _
    s = s1 Then
    Exit While
    End If
    i = i + 1
    End While
    If i < rowCount Then
    s = rows(i)
    (Me.ColumnCombo Box.ValueMember )
    Else
    s = DBNull.Value
    End If
    End If
    row("Value") = s
    row.EndEdit()

    ' Call the base method to set the value
    If currencySource. Position = rowNum Then
    MyBase.SetColum nValueAtRow(cur rencySource,
    rowNum, s)
    End If
    Catch ex As Exception
    MessageBox.Show (ex.Message)
    End Try
    End Sub 'SetColumnValue AtRow
    End Class


    This is an addition to it to help with some errors...

    Public Class NoKeyUpCombo
    Inherits ComboBox
    Private WM_KEYUP As Integer = &H101


    Protected Overrides Sub WndProc(ByRef m As
    System.Windows. Forms.Message)
    If m.Msg = WM_KEYUP Then
    'ignore keyup to avoid problem with tabbing &
    dropdownlist;
    Return
    End If
    MyBase.WndProc( m)
    End Sub 'WndProc
    End Class 'NoKeyUpCombo

    Maybe this will help you. Look at the web site for more
    info on it.
    If you drop this code into a .Net class, you can see that
    it has Edit functions and Commit functions etc. Just
    modify it to meet your needs & standards.

    Hope this helps...
    -Faith[color=blue]
    >-----Original Message-----
    >I have a datagrid that is editable. I have an
    ><Edititemtempl ate> column that is a dropdownlist when in
    >edit mode. Is there a way to render the a particular
    >control based on a value within that row. Currently, I
    >have a datagrid that renders a dropdownlist for a
    >selected item I want to edit. But there are cases where a
    >particular row, I just want to display a "label" instead
    >of a dropdownlist. Is this possible?
    >
    >Thanks
    >.
    >[/color]
Working...