Click here to Skip to main content
16,005,149 members
Home / Discussions / Visual Basic
   

Visual Basic

 
GeneralDHTML Edit Control Pin
Sumit Domyan23-Feb-05 19:27
Sumit Domyan23-Feb-05 19:27 
Generalexporting crystal report to text format Pin
trishalhen0823-Feb-05 12:39
trishalhen0823-Feb-05 12:39 
Generalchanging combo box text Pin
i hate combo boxes23-Feb-05 10:01
sussi hate combo boxes23-Feb-05 10:01 
GeneralRe: changing combo box text Pin
Acheto23-Feb-05 23:40
Acheto23-Feb-05 23:40 
GeneralRe: changing combo box text Pin
Tom John24-Feb-05 2:50
Tom John24-Feb-05 2:50 
GeneralRe: changing combo box text Pin
i hate combo boxes24-Feb-05 4:00
sussi hate combo boxes24-Feb-05 4:00 
Generalvb.net datagrid combobox Pin
jake07223-Feb-05 9:05
jake07223-Feb-05 9:05 
GeneralRe: vb.net datagrid combobox Pin
j45mw12-Mar-05 17:35
j45mw12-Mar-05 17:35 
Create a class module and stick this code in it:

Imports System
Imports System.Data
Imports System.Windows.Forms
Imports System.Drawing

Namespace ComboBoxCode
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Private _comboBox As DataGridComboBox
Private _sorce As CurrencyManager
Private _rowNumber As Integer
Private _editing As Boolean

Public Sub New(ByVal dataSource As DataView, _
ByVal displayMember As String, _
ByVal valueMember As String)
_sorce = Nothing
_editing = False

_comboBox = New DataGridComboBox
_comboBox.DropDownStyle = ComboBoxStyle.DropDownList
_comboBox.Visible = False
_comboBox.DataSource = dataSource
_comboBox.DisplayMember = displayMember
_comboBox.ValueMember = valueMember

AddHandler _comboBox.Leave, AddressOf _comboBox_Leave
AddHandler _comboBox.SelectionChangeCommitted, _
AddressOf _comboBox_SelectionChangeCommitted
End Sub

Public ReadOnly Property ComboBox() As DataGridComboBox
Get
Return _comboBox
End Get
End Property

Private Sub _comboBox_Leave(ByVal sender As Object, _
ByVal e As EventArgs)
If _editing Then
_editing = False
SetColumnValueAtRow(_sorce, _rowNumber, _
_comboBox.Text)
Invalidate()
End If
_comboBox.Visible = False
AddHandler DataGridTableStyle.DataGrid.Scroll, _
AddressOf DataGrid_Scroll
End Sub

Private Sub DataGrid_Scroll(ByVal sender As Object, _
ByVal e As EventArgs)
If _comboBox.Visible Then
_comboBox.Visible = False
End If
End Sub

Private Sub _comboBox_SelectionChangeCommitted( _
ByVal sender As Object, ByVal e As EventArgs)
_editing = True
MyBase.ColumnStartedEditing(CType(sender, Control))
End Sub

Protected Overrides Function GetMinimumHeight() As Integer
Return _comboBox.PreferredHeight
End Function

Protected Overrides Sub SetDataGridInColumn( _
ByVal value As DataGrid)
MyBase.SetDataGridInColumn(value)
_comboBox.Parent = CType(value, Control)
End Sub

Protected Overrides Sub ConcedeFocus()
MyBase.ConcedeFocus()
_comboBox.Visible = False
End Sub

Protected Overloads Overrides Sub Edit( _
ByVal source As CurrencyManager, _
ByVal rowNum As Integer, _
ByVal bounds As Rectangle, _
ByVal [readOnly] As Boolean, _
ByVal instantText As String, _
ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, [readOnly], _
instantText, cellIsVisible)
Me.TextBox.Visible = False

_rowNumber = rowNum
_sorce = source

_comboBox.Bounds = bounds
_comboBox.RightToLeft = _
Me.DataGridTableStyle.DataGrid.RightToLeft

If cellIsVisible AndAlso Not [readOnly] Then
_comboBox.Visible = True
_comboBox.BringToFront()
_comboBox.Focus()
End If

_comboBox.SelectedIndex = _
_comboBox.FindStringExact(Me.TextBox.Text)

AddHandler DataGridTableStyle.DataGrid.Scroll, _
AddressOf DataGrid_Scroll
End Sub

Protected Overrides Function Commit( _
ByVal dataSource As CurrencyManager, _
ByVal rowNum As Integer) As Boolean
If _editing Then
_editing = False
SetColumnValueAtRow(dataSource, rowNum, _
_comboBox.Text)
End If

Return True
End Function

Protected Overrides Sub SetColumnValueAtRow( _
ByVal source As CurrencyManager, _
ByVal rowNum As Integer, ByVal value As Object)
MyBase.SetColumnValueAtRow(source, rowNum, _
_comboBox.FindValueMember(value))
End Sub

Protected Overrides Function GetColumnValueAtRow( _
ByVal source As CurrencyManager, _
ByVal rowNum As Integer) As Object
Dim val As Object = _
MyBase.GetColumnValueAtRow(source, rowNum)
Return _comboBox.FindDisplayMember(val)
End Function
End Class

Public Class DataGridComboBox
Inherits System.Windows.Forms.ComboBox
Private WM_KEYUP As Integer = &H101

Protected Overrides Sub WndProc( _
ByRef theMessage As System.Windows.Forms.Message)
If theMessage.Msg = WM_KEYUP Then
Return
End If
MyBase.WndProc(theMessage)
End Sub

Public Function FindValueMember( _
ByVal display As Object) As Object
Dim dv As DataView = CType(DataSource, DataView)
Dim rowCount As Integer = dv.Count

Dim disp As Object
Dim i As Integer
For i = 0 To rowCount - 1
disp = dv(i)(DisplayMember)
If display.Equals(disp) Then
Return dv(i)(ValueMember)
End If
Next i
Return display
End Function

Public Function FindDisplayMember( _
ByVal value As Object) As Object
Dim dv As DataView = CType(DataSource, DataView)
Dim rowCount As Integer = dv.Count

Dim val As Object
Dim i As Integer
For i = 0 To rowCount - 1
val = dv(i)(ValueMember)
If value.Equals(val) Then
Return dv(i)(DisplayMember)
End If
Next i
Return DBNull.Value
End Function
End Class
End Namespace

Then go to where you format your datagrid and use this code for the column you need a datagrid combo box for:

Dim grdColStyle4 As New ComboBoxCode.DataGridComboBoxColumn( _
dvgProducts, "ProductCode", "ProductCode")
''''DataSource, DisplayMember, ValueMember
With grdColStyle4
.MappingName = "ProductCode"
.HeaderText = "Product ID"
.NullText = ""
.Width = 80
End With

You will have to create either a dataview or dataset for your datasource, but this should get you where you want to be.

Let me know how this works out for you.



GeneralVB.NET: Diagramm but no Excel! Pin
Acheto23-Feb-05 7:31
Acheto23-Feb-05 7:31 
GeneralSessions lost Pin
partt23-Feb-05 7:25
partt23-Feb-05 7:25 
GeneralRe: Sessions lost Pin
Colin Angus Mackay24-Feb-05 0:17
Colin Angus Mackay24-Feb-05 0:17 
GeneralDouble problem: dynamic adding with autoscroll and dynamic removing Pin
Nanaki2k23-Feb-05 7:14
Nanaki2k23-Feb-05 7:14 
GeneralRe: Double problem: dynamic adding with autoscroll and dynamic removing Pin
Nanaki2k23-Feb-05 8:55
Nanaki2k23-Feb-05 8:55 
Generaltext box value to double Pin
dyerstein23-Feb-05 7:00
dyerstein23-Feb-05 7:00 
GeneralRe: text box value to double Pin
Jim Matthews23-Feb-05 7:29
Jim Matthews23-Feb-05 7:29 
GeneralRe: text box value to double Pin
dyerstein23-Feb-05 7:46
dyerstein23-Feb-05 7:46 
GeneralRe: text box value to double Pin
Jim Matthews23-Feb-05 9:14
Jim Matthews23-Feb-05 9:14 
GeneralRe: text box value to double Pin
dyerstein23-Feb-05 9:45
dyerstein23-Feb-05 9:45 
GeneralRe: text box value to double Pin
Christian Graus23-Feb-05 9:39
protectorChristian Graus23-Feb-05 9:39 
Generalvb.net and sql 2000 Pin
Member 107391923-Feb-05 6:16
Member 107391923-Feb-05 6:16 
GeneralRe: vb.net and sql 2000 Pin
Colin Angus Mackay24-Feb-05 0:24
Colin Angus Mackay24-Feb-05 0:24 
GeneralRe: vb.net and sql 2000 Pin
Member 107391924-Feb-05 8:32
Member 107391924-Feb-05 8:32 
Generalowner drawn combo box focus rectangle Pin
Anonymous23-Feb-05 6:06
Anonymous23-Feb-05 6:06 
QuestionUSB Card Reader. How to read data from usb into string? Pin
Member 116828123-Feb-05 5:02
Member 116828123-Feb-05 5:02 
AnswerRe: USB Card Reader. How to read data from usb into string? Pin
Dave Kreskowiak23-Feb-05 6:33
mveDave Kreskowiak23-Feb-05 6:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.