By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,490 Members | 1,417 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,490 IT Pros & Developers. It's quick & easy.

Many-to-many relation in a datagrid...

P: n/a
Hi all

I'm a former Access developer who would like to implement a many-to-many
relation in about the same way you do in Access: With a subform and a combo
box.

Is it possible to use a combobox in a datagrid?

Any other suggestions/articles on how to implement many-many relations in
the frontend (which of course are 2 one-many relations)?

Best regards

Jan Nielsen
Nov 21 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
CMA
hi,

yes, you can add combo box in a datagrid. set the column as a template
column and edit the template and add the combo box.

hope this helps,
CMA
"Jan Nielsen" <Re**************@tiscali.dk> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hi all

I'm a former Access developer who would like to implement a many-to-many
relation in about the same way you do in Access: With a subform and a combo box.

Is it possible to use a combobox in a datagrid?

Any other suggestions/articles on how to implement many-many relations in
the frontend (which of course are 2 one-many relations)?

Best regards

Jan Nielsen

Nov 21 '05 #2

P: n/a
Sorry, I wasn't precise: I meant in a winform, not a webform.

Jan
"CMA" <cm**************@textcentric.lk> skrev i en meddelelse
news:eC**************@tk2msftngp13.phx.gbl...
hi,

yes, you can add combo box in a datagrid. set the column as a template
column and edit the template and add the combo box.

hope this helps,
CMA
"Jan Nielsen" <Re**************@tiscali.dk> wrote in message
news:Oj**************@TK2MSFTNGP12.phx.gbl...
Hi all

I'm a former Access developer who would like to implement a many-to-many
relation in about the same way you do in Access: With a subform and a

combo
box.

Is it possible to use a combobox in a datagrid?

Any other suggestions/articles on how to implement many-many relations in
the frontend (which of course are 2 one-many relations)?

Best regards

Jan Nielsen


Nov 21 '05 #3

P: n/a
Jan,

The question of the many to many relation I don't not really understand.
This about the combobox and datagrid.

I made this yesterday when there was a question, however than the question
was resolved, so the sample works however can probably be nicer.

Can you try it, this is an very extended combobox, there are a lot more
simple.

This combobox is one that is on syncfussion as well however only in the
link. In my opinion this exists in all kinds of ways on Internet, it seems
that everybody has everytime improved something on it, I did in my opinion
only some small improvements to get it with early binding and resolved an
error around the display of the fields and the right setting with
membernames.

The sample database you know already, I have placed the combobox classes at
the end, so those are easy to cut from the sample.

My chalenge is now in this sample to get the states in a relation to the
countries, however I told you earlier that that binding was still a problem
for me. (With a seperate datagrid it is not a problem, that is I thought
conform an earlier solution I showed you)

\\\the sample needs a datagrid
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dv As New DataView(ds.Tables("Persons"))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Persons"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)
ts.GridColumnStyles.Add _
(New DataGridComboColumnStyle(ds.Tables("Countries"), "Country",
"Country"))
Dim countrystyle As DataGridColumnStyle =
ts.GridColumnStyles.Item(1)
countrystyle.MappingName = "Country"
countrystyle.HeaderText = "Countries"
ts.GridColumnStyles.Add _
(New DataGridComboColumnStyle(ds.Tables("States"), "State",
"State"))
Dim statestyle As DataGridColumnStyle = ts.GridColumnStyles.Item(2)
statestyle.MappingName = "State"
statestyle.HeaderText = "States"
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub CreateDs(ByVal ds As DataSet)
Dim dtName As New DataTable("Persons")
Dim dcName As New DataColumn("Name")
Dim dcCountry As New DataColumn("Country")
Dim dcState As New DataColumn("State")
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountry)
dtName.Columns.Add(dcState)
ds.Tables.Add(dtName)
For i As Integer = 0 To 5
Dim dr As DataRow = dtName.NewRow
dtName.Rows.Add(dr)
Next
dtName.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU", "Austria"}
dtName.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US", "Florida"}
dtName.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US", "Illinois"}
dtName.Rows(3).ItemArray = New Object() _
{"Terry Burns", "EU", "UK"}
dtName.Rows(4).ItemArray = New Object() _
{"Jay B Harlow", "US", "New York"}
dtName.Rows(5).ItemArray = New Object() _
{"Cor Ligthert", "EU", "Holland"}
Dim dtCountry As New DataTable("Countries")
dcCountry = New DataColumn("Country")
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountry.NewRow
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(0) = "EU"
dtCountry.Rows(1)(0) = "US"
Dim dtStates As New DataTable("States")
dtStates.Columns.Add("State")
ds.Tables.Add(dtStates)
For i As Integer = 0 To 5
Dim dr As DataRow = dtStates.NewRow
dtStates.Rows.Add(dr)
Next
dtStates.Rows(0)(0) = "Austria"
dtStates.Rows(1)(0) = "Florida"
dtStates.Rows(2)(0) = "Illinois"
dtStates.Rows(3)(0) = "UK"
dtStates.Rows(4)(0) = "New York"
dtStates.Rows(5)(0) = "Holland"
End Sub
'Combobox
End Class
Public Class DataGridCombo
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub
End Class
Public Class DataGridComboColumnStyle
Inherits DataGridColumnStyle
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridCombo
Private mDisplayMember As String
Private mValueMember As String
Private mSource As CurrencyManager
Private mRowNum As Integer
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
Integer, ByVal ValueMember As Integer)
Combo = New DataGridCombo
mDisplayMember = DataSource.Columns.Item(DisplayMember).ToString
mValueMember = DataSource.Columns.Item(ValueMember).ToString
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
String, ByVal ValueMember As String)
Combo = New DataGridCombo
mDisplayMember = DisplayMember
mValueMember = ValueMember
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideComboBox()
EndEdit()
End Sub
Protected Overloads Overrides Function Commit(ByVal DataSource As
CurrencyManager, ByVal RowNum As Integer) As Boolean
HideComboBox()
If InEdit Then
Try
Dim Value As Object = Combo.SelectedValue
If NullText.Equals(Value) Then
Value = DBNull.Value
End If
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch
RollBack()
Return False
End Try
EndEdit()
End If
Return True
End Function
Protected Overloads Overrides Sub ConcedeFocus()
Combo.Visible = False
InEdit = 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)
mSource = Source
mRowNum = Rownum
Combo.Text = ""
Dim OriginalBounds As Rectangle = Bounds
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
Dim TextObject As Object = GetColumnValueAtRow(Source, Rownum)
If Not TextObject Is System.DBNull.Value Then Combo.SelectedValue =
TextObject.ToString()
If Not InstantText Is Nothing Then
Combo.SelectedValue = InstantText
End If
Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll()
Else
Combo.Select(Combo.Text.Length, 0)
End If
If Combo.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBou nds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeight() As Integer
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
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Value, String).IndexOf("r\n",
NewLineIndex + 1)
NewLines += 1
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))
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)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Dg As
DataGrid)
MyBase.SetDataGridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Controls.Remove(Combo)
End If
End If
If Not Dg Is Nothing Then Dg.Controls.Add(Combo)
End Sub
Protected Overloads Overrides Sub UpdateUI(ByVal Source As
CurrencyManager, ByVal RowNum As Integer, ByVal InstantText As String)
Combo.Text = GetText(GetColumnValueAtRow(Source, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
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()
InEdit = False
Invalidate()
End Sub
Private Function GetText(ByVal Value As Object) As String
If Value Is System.DBNull.Value Then Return NullText
If Not Value Is Nothing Then
Return Value.ToString
Else
Return String.Empty
End If
End Function
Private Sub HideComboBox()
If Combo.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
Combo.Visible = False
End Sub
Private Sub RollBack()
Combo.Text = OldVal
End Sub
Private Sub PaintText(ByVal g As Graphics, ByVal Bounds As Rectangle,
ByVal Text As String, ByVal AlignToRight As Boolean)
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)
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 Sub ComboChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Combo.SelectionChangeCommitted
SetColumnValueAtRow(mSource, mRowNum, Combo.SelectedValue)
ColumnStartedEditing(Combo)
End Sub
Private Function LookupDisplayValue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.Value Then Return NullText
Dim LookupText As String = LookupObject.ToString
Dim I As Integer
Dim IMax As Integer = Combo.Items.Count - 1
Dim DT As DataTable = CType(Combo.DataSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mValueMember).ToString = LookupText Then
Return DT.Rows(I)(mDisplayMember).ToString
End If
Next
Return String.Empty
End Function
End Class
////

I hope this helps a little bit?

Cor



Nov 21 '05 #4

P: n/a
Hi Cor
Thanks again for your time.
The datagrid/combobox is the solution to my many-to-many problem.

Your solution seems a bit complicated. But I'll look into it.
I stumpled over the MS free vb.net kit.
It seems to include a combo in a datagrid as well

Thanks for your help

Jan

"Cor Ligthert" <no************@planet.nl> skrev i en meddelelse
news:ul*************@TK2MSFTNGP14.phx.gbl...
Jan,

The question of the many to many relation I don't not really understand.
This about the combobox and datagrid.

I made this yesterday when there was a question, however than the question
was resolved, so the sample works however can probably be nicer.

Can you try it, this is an very extended combobox, there are a lot more
simple.

This combobox is one that is on syncfussion as well however only in the
link. In my opinion this exists in all kinds of ways on Internet, it seems
that everybody has everytime improved something on it, I did in my opinion
only some small improvements to get it with early binding and resolved an
error around the display of the fields and the right setting with
membernames.

The sample database you know already, I have placed the combobox classes
at the end, so those are easy to cut from the sample.

My chalenge is now in this sample to get the states in a relation to the
countries, however I told you earlier that that binding was still a
problem for me. (With a seperate datagrid it is not a problem, that is I
thought conform an earlier solution I showed you)

\\\the sample needs a datagrid
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dv As New DataView(ds.Tables("Persons"))
dv.AllowNew = False
DataGrid1.DataSource = dv
Dim ts As New DataGridTableStyle
ts.MappingName = "Persons"
Dim textCol As New DataGridTextBoxColumn
textCol.MappingName = "Name"
textCol.HeaderText = "Name"
textCol.Width = 120
ts.GridColumnStyles.Add(textCol)
ts.GridColumnStyles.Add _
(New DataGridComboColumnStyle(ds.Tables("Countries"), "Country",
"Country"))
Dim countrystyle As DataGridColumnStyle =
ts.GridColumnStyles.Item(1)
countrystyle.MappingName = "Country"
countrystyle.HeaderText = "Countries"
ts.GridColumnStyles.Add _
(New DataGridComboColumnStyle(ds.Tables("States"), "State",
"State"))
Dim statestyle As DataGridColumnStyle = ts.GridColumnStyles.Item(2)
statestyle.MappingName = "State"
statestyle.HeaderText = "States"
DataGrid1.TableStyles.Add(ts)
End Sub
Private Sub CreateDs(ByVal ds As DataSet)
Dim dtName As New DataTable("Persons")
Dim dcName As New DataColumn("Name")
Dim dcCountry As New DataColumn("Country")
Dim dcState As New DataColumn("State")
dtName.Columns.Add(dcName)
dtName.Columns.Add(dcCountry)
dtName.Columns.Add(dcState)
ds.Tables.Add(dtName)
For i As Integer = 0 To 5
Dim dr As DataRow = dtName.NewRow
dtName.Rows.Add(dr)
Next
dtName.Rows(0).ItemArray = New Object() _
{"Herfried K. Wagner", "EU", "Austria"}
dtName.Rows(1).ItemArray = New Object() _
{"Ken Tucker", "US", "Florida"}
dtName.Rows(2).ItemArray = New Object() _
{"CJ Taylor", "US", "Illinois"}
dtName.Rows(3).ItemArray = New Object() _
{"Terry Burns", "EU", "UK"}
dtName.Rows(4).ItemArray = New Object() _
{"Jay B Harlow", "US", "New York"}
dtName.Rows(5).ItemArray = New Object() _
{"Cor Ligthert", "EU", "Holland"}
Dim dtCountry As New DataTable("Countries")
dcCountry = New DataColumn("Country")
dtCountry.Columns.Add(dcCountry)
ds.Tables.Add(dtCountry)
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountry.NewRow
dtCountry.Rows.Add(dr)
Next
dtCountry.Rows(0)(0) = "EU"
dtCountry.Rows(1)(0) = "US"
Dim dtStates As New DataTable("States")
dtStates.Columns.Add("State")
ds.Tables.Add(dtStates)
For i As Integer = 0 To 5
Dim dr As DataRow = dtStates.NewRow
dtStates.Rows.Add(dr)
Next
dtStates.Rows(0)(0) = "Austria"
dtStates.Rows(1)(0) = "Florida"
dtStates.Rows(2)(0) = "Illinois"
dtStates.Rows(3)(0) = "UK"
dtStates.Rows(4)(0) = "New York"
dtStates.Rows(5)(0) = "Holland"
End Sub
'Combobox
End Class
Public Class DataGridCombo
Inherits ComboBox
Public Sub New()
MyBase.New()
End Sub
End Class
Public Class DataGridComboColumnStyle
Inherits DataGridColumnStyle
Private xMargin As Integer = 2
Private yMargin As Integer = 1
Private WithEvents Combo As DataGridCombo
Private mDisplayMember As String
Private mValueMember As String
Private mSource As CurrencyManager
Private mRowNum As Integer
Private OldVal As String = String.Empty
Private InEdit As Boolean = False
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
Integer, ByVal ValueMember As Integer)
Combo = New DataGridCombo
mDisplayMember = DataSource.Columns.Item(DisplayMember).ToString
mValueMember = DataSource.Columns.Item(ValueMember).ToString
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Public Sub New(ByRef DataSource As DataTable, ByVal DisplayMember As
String, ByVal ValueMember As String)
Combo = New DataGridCombo
mDisplayMember = DisplayMember
mValueMember = ValueMember
Combo.Visible = True
Combo.DataSource = DataSource
Combo.DisplayMember = mDisplayMember
Combo.ValueMember = mValueMember
Combo.DropDownStyle = ComboBoxStyle.DropDownList
Combo.Bounds = Rectangle.Empty
End Sub
Protected Overloads Overrides Sub Abort(ByVal RowNum As Integer)
Debug.WriteLine("Abort()")
RollBack()
HideComboBox()
EndEdit()
End Sub
Protected Overloads Overrides Function Commit(ByVal DataSource As
CurrencyManager, ByVal RowNum As Integer) As Boolean
HideComboBox()
If InEdit Then
Try
Dim Value As Object = Combo.SelectedValue
If NullText.Equals(Value) Then
Value = DBNull.Value
End If
SetColumnValueAtRow(DataSource, RowNum, Value)
Catch
RollBack()
Return False
End Try
EndEdit()
End If
Return True
End Function
Protected Overloads Overrides Sub ConcedeFocus()
Combo.Visible = False
InEdit = 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)
mSource = Source
mRowNum = Rownum
Combo.Text = ""
Dim OriginalBounds As Rectangle = Bounds
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
Dim TextObject As Object = GetColumnValueAtRow(Source, Rownum)
If Not TextObject Is System.DBNull.Value Then Combo.SelectedValue =
TextObject.ToString()
If Not InstantText Is Nothing Then
Combo.SelectedValue = InstantText
End If
Combo.RightToLeft = Me.DataGridTableStyle.DataGrid.RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll()
Else
Combo.Select(Combo.Text.Length, 0)
End If
If Combo.Visible Then
DataGridTableStyle.DataGrid.Invalidate(OriginalBou nds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeight() As Integer
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
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Value, String).IndexOf("r\n",
NewLineIndex + 1)
NewLines += 1
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))
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)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
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)
Dim objText As Object = GetColumnValueAtRow(Source, RowNum)
Dim Text As String = LookupDisplayValue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInColumn(ByVal Dg As
DataGrid)
MyBase.SetDataGridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Controls.Remove(Combo)
End If
End If
If Not Dg Is Nothing Then Dg.Controls.Add(Combo)
End Sub
Protected Overloads Overrides Sub UpdateUI(ByVal Source As
CurrencyManager, ByVal RowNum As Integer, ByVal InstantText As String)
Combo.Text = GetText(GetColumnValueAtRow(Source, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
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()
InEdit = False
Invalidate()
End Sub
Private Function GetText(ByVal Value As Object) As String
If Value Is System.DBNull.Value Then Return NullText
If Not Value Is Nothing Then
Return Value.ToString
Else
Return String.Empty
End If
End Function
Private Sub HideComboBox()
If Combo.Focused Then
Me.DataGridTableStyle.DataGrid.Focus()
End If
Combo.Visible = False
End Sub
Private Sub RollBack()
Combo.Text = OldVal
End Sub
Private Sub PaintText(ByVal g As Graphics, ByVal Bounds As Rectangle,
ByVal Text As String, ByVal AlignToRight As Boolean)
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)
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 Sub ComboChanged(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Combo.SelectionChangeCommitted
SetColumnValueAtRow(mSource, mRowNum, Combo.SelectedValue)
ColumnStartedEditing(Combo)
End Sub
Private Function LookupDisplayValue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.Value Then Return NullText
Dim LookupText As String = LookupObject.ToString
Dim I As Integer
Dim IMax As Integer = Combo.Items.Count - 1
Dim DT As DataTable = CType(Combo.DataSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mValueMember).ToString = LookupText Then
Return DT.Rows(I)(mDisplayMember).ToString
End If
Next
Return String.Empty
End Function
End Class
////

I hope this helps a little bit?

Cor



Nov 21 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.