Home  |  Index  |  Dotnet4all Snippets  |  Submit resources
About  |  Mail us  
Dotnet4all Logo

  Datagrid with a combobox (Thursday, December 23, 2004)




Found the following interesting discussion in the Newsgroups:

datagrid with a combobox
by:Anonymous

OK. So I've been to http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp and learned a lot about what I might do with a datagrid. But I'm STILL not able to do what I want to do. I want to be able be able to enter data into my grid cell using a combobox. I want the combobox to display items based on my selected DisplayMember and update the datasource based on my selected ValueMember. So far, so good. But when the user leaves the combobox cell, I want the value of the DisplayMember to be displayed, not the ValueMember. The closest I've come to this is to Paint all the cells in the column with the same (most recently selected) DisplayMember value.

While I'm at it, I'd like to derive an inherited ComboBox that has a ValueMember, a DisplayMember, and a COLLECTION of DisplayColumns in the DropDown list. Any help?

Thanks,

Pat


 Reply:
by:Cor Ligthert

 Hi Pat,

Did you try this sample that I have made with as base the comboboxsample on
Syncfusion (which I modified). The modifed combobox class is at the end.

I hope this helps?

Cor
'\\\needs a datagrid and 2 buttons on a form
'Used for the comboboxcolumn is a modified sample from
'syncfusion
'To start do the Read/Create ds and cancel direct,
' than a start dataset will be created
Dim ds As New DataSet("Test")
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Me.Button1.Text = "Read/Create ds"
Me.Button2.Text = "Write ds"
End Sub
Private Sub FillGrid()
Dim dv As New DataView(ds.Tables(0))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Names"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "IdName"
textCol.HeaderText = "Id"
textCol.Width = 20
ts.GridColumnStyles.Add(textCol)
textCol = New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)
Dim cmbTxtCol As New DataGridComboBoxColumn
cmbTxtCol.MappingName = "Country"
cmbTxtCol.HeaderText = "Countries"
cmbTxtCol.Width = 100
ts.GridColumnStyles.Add(cmbTxtCol)
ts.PreferredRowHeight = (cmbTxtCol.ColumnComboBox.Height + 3)
cmbTxtCol.ColumnComboBox.DataSource = ds.Tables(1)
cmbTxtCol.ColumnComboBox.DisplayMember = "Country"
cmbTxtCol.ColumnComboBox.ValueMember = "IdCountry"
cmbTxtCol.ColumnComboBox.DropDownStyle = ComboBoxStyle.DropDownList
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button2.Click
Dim of As New SaveFileDialog
If of.ShowDialog = DialogResult.OK Then
ds.WriteXml(of.FileName)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles Button1.Click
Dim fo As New OpenFileDialog
If fo.ShowDialog() = DialogResult.OK Then
ds.ReadXml(fo.FileName)
Else
Dim dtName As New DataTable("Names")
Dim dcIdName As New DataColumn("IdName")
Dim dcName As New DataColumn("Name")
Dim dcCountryN As New DataColumn("Country")
dtName.Columns.Add(dcIdName)
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountryN)
ds.Tables.Add(dtName)
For i As Integer = 1 To 6
Dim dr As DataRow = dtName.NewRow
dr(0) = i.ToString
dtName.Rows.Add(dr)
Next
dtName.Rows(0)(1) = "Herfried K. Wagner"
dtName.Rows(1)(1) = "Armin Zingler"
dtName.Rows(2)(1) = "Ken Tucker"
dtName.Rows(3)(1) = "CJ Taylor"
dtName.Rows(4)(1) = "Jay B Harlow"
dtName.Rows(5)(1) = "Cor Ligthert"
dtName.Rows(0)(2) = "Austria(EU)"
dtName.Rows(1)(2) = "Germany(EU)"
dtName.Rows(2)(2) = "Georgia(US)"
dtName.Rows(3)(2) = "Illinois(US)"
dtName.Rows(4)(2) = "New York(US)"
dtName.Rows(5)(2) = "Holland(EU)"
Dim dtCountry As New DataTable("Countries")
Dim dcIdCountry As New DataColumn("IDCountry")
Dim dcCountry As New DataColumn("Country")
dtCountry.Columns.Add(dcIdCountry)
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 1 To 6
Dim dr As DataRow = dtCountry.NewRow
dr(0) = i.ToString
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(1) = "Austria(EU)"
dtCountry.Rows(1)(1) = "Germany(EU)"
dtCountry.Rows(2)(1) = "Holland(EU)"
dtCountry.Rows(3)(1) = "Georgia(US)"
dtCountry.Rows(4)(1) = "Illinois(US)"
dtCountry.Rows(5)(1) = "New York(US)"
End If
FillGrid()
End Sub
End Class
Public Class DataGridComboBoxColumn
Inherits DataGridTextBoxColumn
Public WithEvents ColumnComboBox As NoKeyUpCombo 'special class
Private WithEvents cmSource As CurrencyManager
Private mRowNum As Integer
Private isEditing As Boolean
Shared Sub New()
End Sub
Public Sub New()
MyBase.New()
ColumnComboBox = New NoKeyUpCombo
AddHandler ColumnComboBox.SelectionChangeCommitted, _
New EventHandler(AddressOf ComboStartEditing)
End Sub
Protected Overloads Overrides Sub Edit(ByVal source As CurrencyManager,
_
ByVal rowNum As Integer, ByVal bounds As Rectangle, ByVal readOnly1 As
Boolean, _
ByVal instantText As String, ByVal cellIsVisible As Boolean)
MyBase.Edit(source, rowNum, bounds, readOnly1, instantText,
cellIsVisible)
mRowNum = rowNum
cmSource = source
ColumnComboBox.Parent = Me.TextBox.Parent
ColumnComboBox.Location = Me.TextBox.Location
ColumnComboBox.Size = New Size(Me.TextBox.Size.Width,
ColumnComboBox.Size.Height)
ColumnComboBox.Text = Me.TextBox.Text
TextBox.Visible = False
ColumnComboBox.Visible = True
ColumnComboBox.BringToFront()
ColumnComboBox.Focus()
End Sub
Protected Overloads Overrides Function Commit(ByVal dataSource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
If isEditing Then
isEditing = False
SetColumnValueAtRow(dataSource, rowNum, ColumnComboBox.Text)
End If
Return True
End Function
Private Sub ComboStartEditing(ByVal sender As Object, ByVal e As
EventArgs)
isEditing = True
MyBase.ColumnStartedEditing(DirectCast(sender, Control))
End Sub
Private Sub LeaveComboBox(ByVal sender As Object, ByVal e As EventArgs)
_
Handles ColumnComboBox.Leave
If isEditing Then
SetColumnValueAtRow(cmSource, mRowNum, ColumnComboBox.Text)
isEditing = False
Invalidate()
End If
ColumnComboBox.Hide()
End Sub
End Class
Public Class NoKeyUpCombo
Inherits ComboBox
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg <> &H101 Then
MyBase.WndProc(m)
End If
End Sub
///


 Reply:
by:Anonymous

 Thanks for the response.

This looks very similar to the syncfusion implementation. I have not actually tried to run yours, but I still don't see how it will work as I want. Specifically, I want to DISPLAY the ComboBox's DisplayMember value in the datagrid, but STORE the ComboBox's ValueMember in the DataGrid's datasource. It appears that your example (and all others that I've looked at), either store AND display the DisplayMember, or store AND display the ValueMember. Here is a listing of what I'm trying to do by intercepting the Paint method. It doesn't actually work, but I THINK I'm on the right track. In particular, have a look at the GetColumnTextAtRow function. Any advice?

Public Class DataGridComboBoxColumn
Inherits DataGridColumnStyle
'
' UI constants
'
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridComboBox
Private WithEvents tb As TextBox
Private _DisplayMember As String
Private _ValueMember As String
Private _rowNum As Integer
Private WithEvents _source As CurrencyManager
'
' Used to track editing state
'
Private OldVal As String = String.Empty
Private InEdit As Boolean = False


Public Property ComboBox() As DataGridComboBox
Get
Return Combo
End Get
Set(ByVal Value As DataGridComboBox)
Combo = Value
End Set
End Property

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal AlignToRight As Boolean)
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum))
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Private Function GetText(ByVal Value As Object) As String
If Not Value Is Nothing Then
Debug.WriteLine("GetText(" & Value.ToString & ")")
Else
Debug.WriteLine("GetText(Value is Nothing)")
End If
If Value Is System.DBNull.Value Then Return NullText

If Not Value Is Nothing Then
Debug.WriteLine(Value.ToString)
Return Value.ToString
Else
Debug.WriteLine("Value is Nothing")
Return String.Empty
End If

End Function

Private Sub PaintText(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Text As String, _
ByVal AlignToRight As Boolean)

Debug.WriteLine("PaintText(1)")
Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub

Private Function GetColumnTextAtRow(ByVal Source As CurrencyManager, ByVal RowNum As Integer) As Object
Dim value As Object = Me.GetColumnValueAtRow(Source, RowNum)
Dim dSource As Object
Dim dr As DataRow

dSource = Combo.DataSource
If value Is System.DBNull.Value Then
Return NullText
End If
If Not dSource Is Nothing Then
If TypeOf dSource Is DataTable Then
dSource = CType(dSource, DataTable)
If Not dSource.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Columns.Contains(Combo.ValueMember) Then
Return NullText
End If
For Each dr In dSource.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
ElseIf TypeOf dSource Is DataView Then
dSource = CType(dSource, DataView)
If Not dSource.Table.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Table.Columns.Contains(Combo.ValueMember) Then
Return value
End If
For Each dr In dSource.Table.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
End If
End If
End Function

Private Sub Combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectedIndexChanged
tb.Text = CType(sender, ComboBox).Text
End Sub

Public Class DataGridComboBox
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub

Private _modified As Boolean = False

Public isInEditOrNavigateMode As Boolean = True

Public Property Modified() As Boolean
Get
Return _modified
End Get
Set(ByVal Value As Boolean)
_modified = Value
End Set
End Property

End Class

End Class


 Reply:
by:Anonymous

 Update. The following code does what I want it to do, but moves slower than molasses in January. Any advice on speeding it up. I believe that it has to do with passing the ComboBox's datasource around.

Option Strict Off
Option Explicit On
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Windows.Forms
Imports System.Data

Namespace DataGridComboBoxColumnStyle
Public Class DataGridComboBox
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub

Private _modified As Boolean = False

Public isInEditOrNavigateMode As Boolean = True

Public Property Modified() As Boolean
Get
Return _modified
End Get
Set(ByVal Value As Boolean)
_modified = Value
End Set
End Property

End Class
Public Class DataGridComboBoxColumn
Inherits DataGridColumnStyle
'
' UI constants
'
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridComboBox
Private WithEvents tb As TextBox
Private _DisplayMember As String
Private _ValueMember As String
Private _rowNum As Integer
Private WithEvents _source As CurrencyManager
'
' Used to track editing state
'
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
'
' Create a new column - DisplayMember, ValueMember
' Passed by ordinal
'
Public Sub New()
Combo = New DataGridComboBox
Combo.Visible = False
tb = New TextBox
tb.Visible = False
End Sub

Public Sub New(ByRef DataSource As DataTable, _
ByVal DisplayMember As Integer, _
ByVal ValueMember As Integer)

Debug.WriteLine("New(1)")
Combo = New DataGridComboBox
_DisplayMember = DataSource.Columns.Item(index:=DisplayMember).ToString
_ValueMember = DataSource.Columns.Item(index:=ValueMember).ToString

With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = _DisplayMember
.ValueMember = _ValueMember
End With
tb = New TextBox
With tb
.Visible = False
End With
End Sub
'
' Create a new column - DisplayMember, ValueMember
' passed by string
'
Public Sub New(ByRef DataSource As DataTable, _
ByVal DisplayMember As String, _
ByVal ValueMember As String)

Debug.WriteLine("New(2)")
Combo = New DataGridComboBox
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = DisplayMember
.ValueMember = ValueMember
End With
tb = New TextBox
With tb
.Visible = False
End With
End Sub
Public Sub New(ByRef DataSource As DataView, _
ByVal DisplayMember As Integer, _
ByVal ValueMember As Integer)

Debug.WriteLine("New(3)")
Combo = New DataGridComboBox
_DisplayMember = DataSource.Table.Columns.Item(index:=DisplayMember).ToString
_ValueMember = DataSource.Table.Columns.Item(index:=ValueMember).ToString

With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = _DisplayMember
.ValueMember = _ValueMember
End With
tb = New TextBox
With tb
.Visible = False
End With
End Sub
'
' Create a new column - DisplayMember, ValueMember
' passed by string
'
Public Sub New(ByRef DataSource As DataView, _
ByVal DisplayMember As String, _
ByVal ValueMember As String)

Debug.WriteLine("New(4)")
Combo = New DataGridComboBox
With Combo
.Visible = False
.DataSource = DataSource
.DisplayMember = DisplayMember
.ValueMember = ValueMember
End With
tb = New TextBox
With tb
.Visible = False
End With
End Sub

'------------------------------------------------------
' Methods overridden from DataGridColumnStyle
'------------------------------------------------------
'
' Abort Changes
'
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideComboBox()
EndEdit()
End Sub
'
' Commit Changes
'
Protected Overloads Overrides Function Commit(ByVal DataSource As CurrencyManager, _
ByVal RowNum As Integer) As Boolean
Debug.WriteLine("Commit()")
HideComboBox()
_rowNum = RowNum
_source = DataSource
If Not InEdit Then
Return True
End If

Try
Dim Value As Object = Combo.SelectedValue
If NullText.Equals(Value) Then
Value = Convert.DBNull
End If
tb.Text = Combo.Text
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch e As Exception
RollBack()
Return False
End Try
EndEdit()
Return True
End Function
'
' Remove focus
'
Protected Overloads Overrides Sub ConcedeFocus()
Debug.WriteLine("ConcedeFocus()")
Combo.Visible = False
End Sub
'
' Edit Grid
'
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)

Debug.WriteLine("Edit()")
Combo.Text = String.Empty

Dim OriginalBounds As Rectangle = Bounds
Dim txt As String

OldVal = Combo.Text

If CellIsVisible Then
Bounds.Offset(xMargin, yMargin)
Bounds.Width -= xMargin * 2
Bounds.Height -= yMargin
Combo.Bounds = Bounds
Combo.Visible = True
Else
Combo.Bounds = OriginalBounds
Combo.Visible = False
End If

txt = tb.Text
If Not txt = InstantText Then
Combo.Modified = True
End If
If Not txt = NullText Then
Combo.SelectedValue = GetText(GetColumnValueAtRow(Source, Rownum))
End If

If Not InstantText Is Nothing Then
Combo.SelectedValue = InstantText
End If

Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
Combo.Focus()

If InstantText Is Nothing Then
Combo.SelectAll()
Else
Dim [End] As Integer = Combo.Text.Length
Combo.Select([End], 0)
End If

If Combo.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds)
End If

InEdit = True

End Sub

Protected Overloads Overrides Function GetMinimumHeight() As Integer
'
' Set the minimum height to the height of the combobox
'
Debug.WriteLine("GetMinimumHeight()")
Return Combo.PreferredHeight + yMargin
End Function

Protected Overloads Overrides Function GetPreferredHeight(ByVal g As Graphics, _
ByVal Value As Object) As Integer
Debug.WriteLine("GetPreferredHeight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Dim ValueString As String = Me.GetText(Value)
Do
While NewLineIndex <> -1
NewLineIndex = ValueString.IndexOf("r\n", NewLineIndex + 1)
NewLines += 1
End While
Loop

Return FontHeight * NewLines + yMargin
End Function

Protected Overloads Overrides Function GetPreferredSize(ByVal g As Graphics, _
ByVal Value As Object) As Size
Dim Extents As Size = Size.Ceiling(g.MeasureString(GetText(Value), _
Me.DataGridTableStyle.DataGrid.Font))
Debug.WriteLine("GetPreferredSize()")
Extents.Width += xMargin * 2 + DataGridTableGridLineWidth
Extents.Height += yMargin
Return Extents
End Function
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer)
Debug.WriteLine("Paint(1)")
Paint(g, Bounds, Source, RowNum, False)
End Sub

Protected Overloads Overrides Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal AlignToRight As Boolean)
Debug.WriteLine("Paint(2)" & GetText(GetColumnValueAtRow(Source, RowNum)))
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum))
PaintText(g, Bounds, Text, AlignToRight)
End Sub

Protected Overloads Sub Paint(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)

Debug.WriteLine("Paint(3)")
Dim Text As String = GetText(GetColumnTextAtRow(Source, RowNum))
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub

Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Value As DataGrid)
Debug.WriteLine("SetDataGridInColumn()")
MyBase.SetDataGridInColumn(Value)
If Not (Combo.Parent Is Value) Then
If Not (Combo.Parent Is Nothing) Then
Combo.Parent.Controls.Remove(Combo)
End If
End If

If Not (Value Is Nothing) Then Value.Controls.Add(Combo)
If Not (tb.Parent Is Value) Then
If Not (tb.Parent Is Nothing) Then
tb.Parent.Controls.Remove(tb)
End If
End If

If Not (Value Is Nothing) Then Value.Controls.Add(tb)
End Sub

Protected Overloads Overrides Sub UpdateUI(ByVal Source As CurrencyManager, _
ByVal RowNum As Integer, ByVal InstantText As String)
Debug.WriteLine("UpdateUI()")
Combo.Text = tb.Text
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub

'----------------------------------------------------------------------
' Helper Methods
'----------------------------------------------------------------------

Public Property ComboBox() As DataGridComboBox
Get
Return Combo
End Get
Set(ByVal Value As DataGridComboBox)
Combo = Value
End Set
End Property

Private ReadOnly Property DataGridTableGridLineWidth() As Integer
Get
If Me.DataGridTableStyle.GridLineStyle = DataGridLineStyle.Solid Then
Return 1
Else
Return 0
End If
End Get
End Property

Private Sub EndEdit()
Debug.WriteLine("EndEdit()")
InEdit = False
Combo.Modified = False
Invalidate()
End Sub

Private Function GetText(ByVal Value As Object) As String
If Not Value Is Nothing Then
Debug.WriteLine("GetText(" & Value.ToString & ")")
Else
Debug.WriteLine("GetText(Value is Nothing)")
End If
If Value Is System.DBNull.Value Then Return NullText

If Not Value Is Nothing Then
Debug.WriteLine(Value.ToString)
Return Value.ToString
Else
Debug.WriteLine("Value is Nothing")
Return String.Empty
End If

End Function

Private Sub HideComboBox()
Debug.WriteLine("HideComboBox()")
If Combo.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
Combo.Visible = False
End Sub

Private Sub RollBack()
Debug.WriteLine("RollBack()")
Combo.Text = OldVal
tb.Text = OldVal
Combo.Modified = False
End Sub

Private Sub PaintText(ByVal g As Graphics, _
ByVal Bounds As Rectangle, _
ByVal Text As String, _
ByVal AlignToRight As Boolean)

Debug.WriteLine("PaintText(1)")
Dim BackBrush As Brush = New SolidBrush(Me.DataGridTableStyle.BackColor)
Dim ForeBrush As Brush = New SolidBrush(Me.DataGridTableStyle.ForeColor)
PaintText(g, Bounds, Text, BackBrush, ForeBrush, AlignToRight)
End Sub

Private Sub PaintText(ByVal g As Graphics, _
ByVal TextBounds As Rectangle, _
ByVal Text As String, _
ByVal BackBrush As Brush, _
ByVal ForeBrush As Brush, _
ByVal AlignToRight As Boolean)

Debug.WriteLine("PaintText(2)" & Text)
Dim Rect As Rectangle = TextBounds
Dim RectF As RectangleF = RectF.op_Implicit(Rect) ' Convert to RectangleF
Dim Format As StringFormat = New StringFormat

If AlignToRight Then
Format.FormatFlags = StringFormatFlags.DirectionRightToLeft
End If

Select Case Me.Alignment
Case Is = HorizontalAlignment.Left
Format.Alignment = StringAlignment.Near
Case Is = HorizontalAlignment.Right
Format.Alignment = StringAlignment.Far
Case Is = HorizontalAlignment.Center
Format.Alignment = StringAlignment.Center
End Select

Format.FormatFlags = Format.FormatFlags Or StringFormatFlags.NoWrap
g.FillRectangle(Brush:=BackBrush, Rect:=Rect)

Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Text, Me.DataGridTableStyle.DataGrid.Font, ForeBrush, RectF, Format)
Format.Dispose()

End Sub

Private Function GetColumnTextAtRow(ByVal Source As CurrencyManager, ByVal RowNum As Integer) As Object
Dim value As Object = Me.GetColumnValueAtRow(Source, RowNum)
Dim dSource As Object

dSource = Combo.DataSource
If value Is System.DBNull.Value Then
Return NullText
End If
If Not dSource Is Nothing Then
If TypeOf dSource Is DataTable Then
Dim dr As DataRow
dSource = CType(dSource, DataTable)
If Not dSource.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Columns.Contains(Combo.ValueMember) Then
Return NullText
End If
For Each dr In dSource.Rows
If value = dr(Combo.ValueMember) Then
Return dr(Combo.DisplayMember)
End If
Next
Return NullText
ElseIf TypeOf dSource Is DataView Then
Dim drv As DataRowView
dSource = CType(dSource, DataView)
If Not dSource.Table.Columns.Contains(Combo.DisplayMember) _
OrElse Not dSource.Table.Columns.Contains(Combo.ValueMember) Then
Return NullText
End If
For Each drv In dSource
If value = drv(Combo.ValueMember) Then
Return drv(Combo.DisplayMember)
End If
Next
Return NullText
End If
End If
End Function

Private Sub Combo_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles Combo.SelectedIndexChanged
tb.Text = CType(sender, ComboBox).Text
End Sub

End Class

End Namespace








0 Comments:

Post a Comment

 
Previous Posts
    - Error: Argument 'Prompt' cannot be converted to ty...
    - Drawing XY coords on a form
    - webBrowser control
    - remoteing setup
    - Does file exist
    - Why TextAlign in ListView doesn't work!
    - Having trouble with stdregprov and uintvalue.
    - Problem with sending hex in UDP packet
    - Word library in server side?
    - vbFTPClient - kb article 812404

Archives
    - 10/03/2004 - 10/10/2004
    - 10/10/2004 - 10/17/2004
    - 10/17/2004 - 10/24/2004
    - 10/24/2004 - 10/31/2004
    - 10/31/2004 - 11/07/2004
    - 11/21/2004 - 11/28/2004
    - 11/28/2004 - 12/05/2004
    - 12/05/2004 - 12/12/2004
    - 12/12/2004 - 12/19/2004
    - 12/19/2004 - 12/26/2004
    - 12/26/2004 - 01/02/2005
    - 01/23/2005 - 01/30/2005
    - 01/01/2006 - 01/08/2006
    - 09/24/2006 - 10/01/2006

Disclaimer & Terms of Use  | DotNet4All.Com concept & © 2004 - 2007 by  Zelders²  - Holland