is called DataGridColumnDropDown. I modified it to use
the text box when I wanted it to depended on a certain
search criteria.
Public Class ExtendedSearchFieldValueColumnStyle
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As ComboBox
Public WithEvents ColumnTextBox As TextBox
Private _source As System.Windows.Forms.CurrencyManager
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.DropDown
AddHandler ColumnComboBox.Leave, AddressOf
LeaveComboBox
AddHandler
ColumnComboBox.SelectionChangeCommitted, AddressOf
ComboStartEditing
AddHandler Me.TextBox.Enter, AddressOf
ComboStartEditing
AddHandler Me.TextBox.Leave, AddressOf LeaveTextBox
End Sub 'New
Private Sub HandleScroll(ByVal sender As Object, ByVal
e As EventArgs)
If ColumnComboBox.Visible Then
ColumnComboBox.Hide()
End If
End Sub 'HandleScroll
Private Sub ComboStartEditing(ByVal sender As Object,
ByVal e As EventArgs)
MyBase.ColumnStartedEditing(sender)
End Sub 'ComboMadeCurrent
Private Sub LeaveComboBox(ByVal sender As Object,
ByVal e As EventArgs)
ColumnComboBox.Hide()
AddHandler Me.DataGridTableStyle.DataGrid.Scroll,
New EventHandler(AddressOf HandleScroll)
End Sub 'LeaveComboBox
Private Sub LeaveTextBox(ByVal sender As Object, ByVal
e As EventArgs)
Me.TextBox.Hide()
End Sub
Protected Overloads Overrides Sub Edit(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
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(currencySource, rowNum, bounds,
isReadOnly, instantText, cellIsVisible)
_rowNum = rowNum
_source = currencySource
' Get to get the
ExtendedSearchData.ExtendedSearch.FieldName value for the
current row
Dim currentRow As DataRowView = CType
(currencySource.Current, DataRowView)
Dim currentValue As Object = currentRow
("RefTrendSearchFieldID")
' If the current value is null then exit
function
If currentValue Is DBNull.Value Then Exit Sub
' otherwise
' Lookup the ExtendedSearchInfo row based on
the value
Dim refTrendSearchFieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchField As
ExtendedSearchInfoDataset.RefTrendSearchFieldDataT able
refTrendSearchField =
ExtendedSearchSystem.GetRefTrendSearchFieldByTrend SearchFie
ldID(refTrendSearchFieldID)
' Just in case
If refTrendSearchField.Rows.Count = 0 Then
Exit Sub
Dim refTrendSearchFieldRow As
ExtendedSearchInfoDataset.RefTrendSearchFieldRow
refTrendSearchFieldRow =
refTrendSearchField.Rows(0)
' Show the text box or combobox based on the
ExtendedSearchInfo.DataGridCellType field
Select Case
refTrendSearchFieldRow.sDataGridCellType.Trim()
Case "D"
currentRow("ColumnType") = "D"
ColumnComboBox.Parent =
Me.TextBox.Parent
ColumnComboBox.Location =
Me.TextBox.Location
ColumnComboBox.Size = New Size
(Me.TextBox.Size.Width, ColumnComboBox.Size.Height)
ColumnComboBox.ValueMember
= "lookupValue"
ColumnComboBox.DisplayMember
= "Display"
Dim data As DataTable =
GetCurrentRowFieldValue(currencySource)
ColumnComboBox.DataSource = data
If Not currentRow("DisplayValue") Is
DBNull.Value Then
ColumnComboBox.SelectedIndex =
ColumnComboBox.FindStringExact(CType(currentRow
("DisplayValue"), String))
End If
'ColumnComboBox.Text = Me.TextBox.Text
Me.TextBox.Visible = False
ColumnComboBox.Visible = True
AddHandler
Me.DataGridTableStyle.DataGrid.Scroll, AddressOf
HandleScroll
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
Case "T"
currentRow("ColumnType") = "T"
Me.ColumnComboBox.Visible = False
Me.TextBox.Visible = True
Me.TextBox.BringToFront()
Me.TextBox.Focus()
End Select
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'Edit
Private Function GetCurrentRowFieldValue(ByVal
currencySource As System.Windows.Forms.CurrencyManager) As
ExtendedSearchInfoDataset.ComboResultsDataTable
' 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
("RefTrendSearchFieldID")
' If the current value is null then exit function
If currentValue Is DBNull.Value Then Exit Function
' otherwise
' Lookup the ExtendedSearchInfo row based on the
value
Dim refTrendSearchFieldID As Integer = CType
(currentValue, Integer)
Dim refTrendSearchField As
ExtendedSearchInfoDataset.RefTrendSearchFieldDataT able
Dim refTrendSearchFieldRow As
ExtendedSearchInfoDataset.RefTrendSearchFieldRow
refTrendSearchField =
ExtendedSearchSystem.GetRefTrendSearchFieldByTrend SearchFie
ldID(refTrendSearchFieldID)
refTrendSearchFieldRow = refTrendSearchField.Rows
(0)
Dim valueInfo As
ExtendedSearchInfoDataset.ComboResultsDataTable =
ExtendedSearchSystem.GetComboBoxTypes
(refTrendSearchFieldRow.RefTrendSearchFieldID,
refTrendSearchFieldRow.sDropDownSource)
Dim valueInfoRow As
ExtendedSearchInfoDataset.ComboResultsRow
Return valueInfo
End Function
Protected Overrides Function Commit(ByVal dataSource
As System.Windows.Forms.CurrencyManager, ByVal rowNum As
Integer) As Boolean
Try
Dim row As DataRowView = CType
(dataSource.Current, DataRowView)
Dim value As String
If row("ColumnType") Is DBNull.Value Then
Return False
If CType(row("ColumnType"), String) = "D" Then
value = ColumnComboBox.Text
Else
value = TextBox.Text
End If
If value.Length = 0 Then
Return False
Else
SetColumnValueAtRow(dataSource, rowNum,
value)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
Return True
End Function 'Commit
Protected Overrides Function GetColumnValueAtRow(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer) As Object
Try
Dim row As DataRowView = CType
(currencySource.List, DataView)(rowNum)
Return row("DisplayValue")
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Function 'GetColumnValueAtRow
Protected Overrides Sub SetColumnValueAtRow(ByVal
currencySource As System.Windows.Forms.CurrencyManager,
ByVal rowNum As Integer, ByVal value As Object)
Try
Dim s As Object = value
Dim row As DataRowView = CType
(currencySource.Current, DataRowView)
row("DisplayValue") = s
If CType(row("ColumnType"), String) = "D"
Then ' Combobox
' Lookup the combobox value by the display
value
Dim dt As DataTable = CType
(Me.ColumnComboBox.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.ColumnComboBox.DisplayMember)
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.ColumnComboBox.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.SetColumnValueAtRow(currencySource,
rowNum, s)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub 'SetColumnValueAtRow
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
-----Original Message-----
I have a datagrid that is editable. I have an
<Edititemtemplate> 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
.