473,837 Members | 1,545 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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
4 2289
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******** ******@TK2MSFTN GP12.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
Sorry, I wasn't precise: I meant in a winform, not a webform.

Jan
"CMA" <cm************ **@textcentric. lk> skrev i en meddelelse
news:eC******** ******@tk2msftn gp13.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******** ******@TK2MSFTN GP12.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
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(ByVa l sender As Object, ByVal e _
As System.EventArg s) Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dv As New DataView(ds.Tab les("Persons"))
dv.AllowNew = False
DataGrid1.DataS ource = dv
Dim ts As New DataGridTableSt yle
ts.MappingName = "Persons"
Dim textCol As New DataGridTextBox Column
textCol.Mapping Name = "Name"
textCol.HeaderT ext = "Name"
textCol.Width = 120
ts.GridColumnSt yles.Add(textCo l)
ts.GridColumnSt yles.Add _
(New DataGridComboCo lumnStyle(ds.Ta bles("Countries "), "Country",
"Country"))
Dim countrystyle As DataGridColumnS tyle =
ts.GridColumnSt yles.Item(1)
countrystyle.Ma ppingName = "Country"
countrystyle.He aderText = "Countries"
ts.GridColumnSt yles.Add _
(New DataGridComboCo lumnStyle(ds.Ta bles("States"), "State",
"State"))
Dim statestyle As DataGridColumnS tyle = ts.GridColumnSt yles.Item(2)
statestyle.Mapp ingName = "State"
statestyle.Head erText = "States"
DataGrid1.Table Styles.Add(ts)
End Sub
Private Sub CreateDs(ByVal ds As DataSet)
Dim dtName As New DataTable("Pers ons")
Dim dcName As New DataColumn("Nam e")
Dim dcCountry As New DataColumn("Cou ntry")
Dim dcState As New DataColumn("Sta te")
dtName.Columns. Add(dcName)
dtName.Columns. Add(dcCountry)
dtName.Columns. Add(dcState)
ds.Tables.Add(d tName)
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("Coun tries")
dcCountry = New DataColumn("Cou ntry")
dtCountry.Colum ns.Add(dcCountr y)
ds.Tables.Add(d tCountry)
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountry.NewRo w
dtCountry.Rows. Add(dr)
Next
dtCountry.Rows( 0)(0) = "EU"
dtCountry.Rows( 1)(0) = "US"
Dim dtStates As New DataTable("Stat es")
dtStates.Column s.Add("State")
ds.Tables.Add(d tStates)
For i As Integer = 0 To 5
Dim dr As DataRow = dtStates.NewRow
dtStates.Rows.A dd(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 DataGridComboCo lumnStyle
Inherits DataGridColumnS tyle
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.Colu mns.Item(Displa yMember).ToStri ng
mValueMember = DataSource.Colu mns.Item(ValueM ember).ToString
Combo.Visible = True
Combo.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.SelectedV alue
If NullText.Equals (Value) Then
Value = DBNull.Value
End If
SetColumnValueA tRow(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(x Margin, 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 = GetColumnValueA tRow(Source, Rownum)
If Not TextObject Is System.DBNull.V alue Then Combo.SelectedV alue =
TextObject.ToSt ring()
If Not InstantText Is Nothing Then
Combo.SelectedV alue = InstantText
End If
Combo.RightToLe ft = Me.DataGridTabl eStyle.DataGrid .RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll ()
Else
Combo.Select(Co mbo.Text.Length , 0)
End If
If Combo.Visible Then
DataGridTableSt yle.DataGrid.In validate(Origin alBounds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeigh t() As Integer
Return Combo.Preferred Height + yMargin
End Function
Protected Overloads Overrides Function GetPreferredHei ght(ByVal g As
Graphics, ByVal Value As Object) As Integer
Debug.WriteLine ("GetPreferredH eight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Valu e, String).IndexOf ("r\n",
NewLineIndex + 1)
NewLines += 1
Loop
Return FontHeight * NewLines + yMargin
End Function
Protected Overloads Overrides Function GetPreferredSiz e(ByVal g As
Graphics, ByVal Value As Object) As Size
Dim Extents As Size = Size.Ceiling(g. MeasureString(G etText(Value), _
Me.DataGridTabl eStyle.DataGrid .Font))
Extents.Width += xMargin * 2 + DataGridTableGr idLineWidth
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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInCo lumn(ByVal Dg As
DataGrid)
MyBase.SetDataG ridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Co ntrols.Remove(C ombo)
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(GetColu mnValueAtRow(So urce, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
Private ReadOnly Property DataGridTableGr idLineWidth() As Integer
Get
If Me.DataGridTabl eStyle.GridLine Style = DataGridLineSty le.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.V alue 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.DataGridTabl eStyle.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.D ataGridTableSty le.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(Me.D ataGridTableSty le.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_Implic it(Rect) ' Convert to
RectangleF
Dim Format As StringFormat = New StringFormat
If AlignToRight Then
Format.FormatFl ags = StringFormatFla gs.DirectionRig htToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlign ment.Left
Format.Alignmen t = StringAlignment .Near
Case Is = HorizontalAlign ment.Right
Format.Alignmen t = StringAlignment .Far
Case Is = HorizontalAlign ment.Center
Format.Alignmen t = StringAlignment .Center
End Select
Format.FormatFl ags = Format.FormatFl ags Or StringFormatFla gs.NoWrap
g.FillRectangle (Brush:=BackBru sh, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Te xt, Me.DataGridTabl eStyle.DataGrid .Font, ForeBrush,
RectF, Format)
Format.Dispose( )
End Sub
Private Sub ComboChanged(By Val sender As Object, ByVal e As
System.EventArg s) Handles Combo.Selection ChangeCommitted
SetColumnValueA tRow(mSource, mRowNum, Combo.SelectedV alue)
ColumnStartedEd iting(Combo)
End Sub
Private Function LookupDisplayVa lue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.V alue Then Return NullText
Dim LookupText As String = LookupObject.To String
Dim I As Integer
Dim IMax As Integer = Combo.Items.Cou nt - 1
Dim DT As DataTable = CType(Combo.Dat aSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mVal ueMember).ToStr ing = LookupText Then
Return DT.Rows(I)(mDis playMember).ToS tring
End If
Next
Return String.Empty
End Function
End Class
////

I hope this helps a little bit?

Cor



Nov 21 '05 #4
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******** *****@TK2MSFTNG P14.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(ByVa l sender As Object, ByVal e _
As System.EventArg s) Handles MyBase.Load
Dim ds As New DataSet
CreateDs(ds)
Dim dv As New DataView(ds.Tab les("Persons"))
dv.AllowNew = False
DataGrid1.DataS ource = dv
Dim ts As New DataGridTableSt yle
ts.MappingName = "Persons"
Dim textCol As New DataGridTextBox Column
textCol.Mapping Name = "Name"
textCol.HeaderT ext = "Name"
textCol.Width = 120
ts.GridColumnSt yles.Add(textCo l)
ts.GridColumnSt yles.Add _
(New DataGridComboCo lumnStyle(ds.Ta bles("Countries "), "Country",
"Country"))
Dim countrystyle As DataGridColumnS tyle =
ts.GridColumnSt yles.Item(1)
countrystyle.Ma ppingName = "Country"
countrystyle.He aderText = "Countries"
ts.GridColumnSt yles.Add _
(New DataGridComboCo lumnStyle(ds.Ta bles("States"), "State",
"State"))
Dim statestyle As DataGridColumnS tyle = ts.GridColumnSt yles.Item(2)
statestyle.Mapp ingName = "State"
statestyle.Head erText = "States"
DataGrid1.Table Styles.Add(ts)
End Sub
Private Sub CreateDs(ByVal ds As DataSet)
Dim dtName As New DataTable("Pers ons")
Dim dcName As New DataColumn("Nam e")
Dim dcCountry As New DataColumn("Cou ntry")
Dim dcState As New DataColumn("Sta te")
dtName.Columns. Add(dcName)
dtName.Columns. Add(dcCountry)
dtName.Columns. Add(dcState)
ds.Tables.Add(d tName)
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("Coun tries")
dcCountry = New DataColumn("Cou ntry")
dtCountry.Colum ns.Add(dcCountr y)
ds.Tables.Add(d tCountry)
For i As Integer = 0 To 1
Dim dr As DataRow = dtCountry.NewRo w
dtCountry.Rows. Add(dr)
Next
dtCountry.Rows( 0)(0) = "EU"
dtCountry.Rows( 1)(0) = "US"
Dim dtStates As New DataTable("Stat es")
dtStates.Column s.Add("State")
ds.Tables.Add(d tStates)
For i As Integer = 0 To 5
Dim dr As DataRow = dtStates.NewRow
dtStates.Rows.A dd(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 DataGridComboCo lumnStyle
Inherits DataGridColumnS tyle
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.Colu mns.Item(Displa yMember).ToStri ng
mValueMember = DataSource.Colu mns.Item(ValueM ember).ToString
Combo.Visible = True
Combo.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.DataSourc e = DataSource
Combo.DisplayMe mber = mDisplayMember
Combo.ValueMemb er = mValueMember
Combo.DropDownS tyle = ComboBoxStyle.D ropDownList
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.SelectedV alue
If NullText.Equals (Value) Then
Value = DBNull.Value
End If
SetColumnValueA tRow(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(x Margin, 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 = GetColumnValueA tRow(Source, Rownum)
If Not TextObject Is System.DBNull.V alue Then Combo.SelectedV alue =
TextObject.ToSt ring()
If Not InstantText Is Nothing Then
Combo.SelectedV alue = InstantText
End If
Combo.RightToLe ft = Me.DataGridTabl eStyle.DataGrid .RightToLeft
If InstantText Is Nothing Then
Combo.SelectAll ()
Else
Combo.Select(Co mbo.Text.Length , 0)
End If
If Combo.Visible Then
DataGridTableSt yle.DataGrid.In validate(Origin alBounds)
End If
InEdit = True
End Sub
Protected Overloads Overrides Function GetMinimumHeigh t() As Integer
Return Combo.Preferred Height + yMargin
End Function
Protected Overloads Overrides Function GetPreferredHei ght(ByVal g As
Graphics, ByVal Value As Object) As Integer
Debug.WriteLine ("GetPreferredH eight()")
Dim NewLineIndex As Integer = 0
Dim NewLines As Integer = 0
Do Until NewLineIndex = -1
NewLineIndex = DirectCast(Valu e, String).IndexOf ("r\n",
NewLineIndex + 1)
NewLines += 1
Loop
Return FontHeight * NewLines + yMargin
End Function
Protected Overloads Overrides Function GetPreferredSiz e(ByVal g As
Graphics, ByVal Value As Object) As Size
Dim Extents As Size = Size.Ceiling(g. MeasureString(G etText(Value),
_
Me.DataGridTabl eStyle.DataGrid .Font))
Extents.Width += xMargin * 2 + DataGridTableGr idLineWidth
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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(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 = GetColumnValueA tRow(Source, RowNum)
Dim Text As String = LookupDisplayVa lue(objText)
PaintText(g, Bounds, Text, AlignToRight)
End Sub
Protected Overloads Overrides Sub SetDataGridInCo lumn(ByVal Dg As
DataGrid)
MyBase.SetDataG ridInColumn(Dg)
If Not Combo.Parent Is Dg Then
If Not Combo.Parent Is Nothing Then
Combo.Parent.Co ntrols.Remove(C ombo)
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(GetColu mnValueAtRow(So urce, RowNum))
If Not (InstantText Is Nothing) Then
Combo.Text = InstantText
End If
End Sub
Private ReadOnly Property DataGridTableGr idLineWidth() As Integer
Get
If Me.DataGridTabl eStyle.GridLine Style =
DataGridLineSty le.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.V alue 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.DataGridTabl eStyle.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.D ataGridTableSty le.BackColor)
Dim ForeBrush As Brush = New
SolidBrush(Me.D ataGridTableSty le.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_Implic it(Rect) ' Convert to
RectangleF
Dim Format As StringFormat = New StringFormat
If AlignToRight Then
Format.FormatFl ags = StringFormatFla gs.DirectionRig htToLeft
End If
Select Case Me.Alignment
Case Is = HorizontalAlign ment.Left
Format.Alignmen t = StringAlignment .Near
Case Is = HorizontalAlign ment.Right
Format.Alignmen t = StringAlignment .Far
Case Is = HorizontalAlign ment.Center
Format.Alignmen t = StringAlignment .Center
End Select
Format.FormatFl ags = Format.FormatFl ags Or StringFormatFla gs.NoWrap
g.FillRectangle (Brush:=BackBru sh, Rect:=Rect)
Rect.Offset(0, yMargin)
Rect.Height -= yMargin
g.DrawString(Te xt, Me.DataGridTabl eStyle.DataGrid .Font, ForeBrush,
RectF, Format)
Format.Dispose( )
End Sub
Private Sub ComboChanged(By Val sender As Object, ByVal e As
System.EventArg s) Handles Combo.Selection ChangeCommitted
SetColumnValueA tRow(mSource, mRowNum, Combo.SelectedV alue)
ColumnStartedEd iting(Combo)
End Sub
Private Function LookupDisplayVa lue(ByVal LookupObject As Object) As
String
If LookupObject Is System.DBNull.V alue Then Return NullText
Dim LookupText As String = LookupObject.To String
Dim I As Integer
Dim IMax As Integer = Combo.Items.Cou nt - 1
Dim DT As DataTable = CType(Combo.Dat aSource, DataTable)
For I = 0 To IMax
If DT.Rows(I)(mVal ueMember).ToStr ing = LookupText Then
Return DT.Rows(I)(mDis playMember).ToS tring
End If
Next
Return String.Empty
End Function
End Class
////

I hope this helps a little bit?

Cor



Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
459
by: Juan | last post by:
I built a form that displays a master-detail relation ship, now i need to add a column to the datagrid displaying the master data, this column corresponds to the text name of a column stored in the master table as an integer (the foreign key, i need to display the text name of the foreign key stored in the third table). Do i have to create another datatable in the dataset im using that includes all the information I need? Change the Sql...
1
3053
by: TusharP | last post by:
Hi Friends, My requirement are as follow..... 1) First DataGrid display all row of one table, with this one additional column having +/- control like tree. 2) When user click on +/- control DataGrid row must expand & show rows of other table (filtered row based on clicked row values). I thing we need embedded datagrid to show expanded row. Is am I right? Is it possible to embedd datagrid as row in other datagrid?
5
2352
by: Mikael Syska | last post by:
Hi, I have 2 talbes in a DataSet, and made a DataRelation between the 2 tables in the DataSet. One of the Tables are displayed in a datagrid, and when one of them is selected in the datagrid I want its childcolumns to be displayed in a second datagrid. How can I filter the dataset or get only the related data into the
0
1210
by: stefanjohansson_2005 | last post by:
I can databind to a relation in WindowsForms for example: I have a typed DataSet with an "Orders" table and a "Order Details" table. They have a relation called "OrderRelation". I have a ComboBox as master and DataGrid as slave: OrdersAdapter.Fill(northwindDataSet1,"Orders"); OrderDetailsAdapter.Fill(northwindDataSet1,"Order Details"); comboBox1.DataSource = northwindDataSet1.Orders; comboBox1.DisplayMember = "OrderID";...
6
1869
by: Peter W Johnson | last post by:
Hi Guys, I am trying to display a datagrid containing two related datasets. I have filled and related them on MemberID with the following code:- Dim myconnection As Odbc.OdbcConnection
0
2916
by: Ambica Jain | last post by:
I have a data grid called Files, which has some columns like FileName, Col1, Col2, ... , Col8. Then i have a combobox which allows user to select from Col1 to Col8 and based on this selection, i generate a report from data in grid Files. It displays like (e.g. is Col1 is selected): Col1 Count ---------------------- Val1 x Val2 y .....
1
1601
by: archana | last post by:
Hi all, I am having application where in i am having datagrid showing 2 tables with relation between them. When i click on relation which set i get records of second table in same datagrid. And to go back i have to click on back buttoon. What i want is to display both tables in same grid. Like when i click on relation records should gets displayed in same grid. Means like tree view structure.
0
1213
by: rengaraj | last post by:
Hello. I have an @Entity class, Person, with a @ManyToMany relation with itself. This relation is the friend association. The code of the class is as follows: @Entity @Table(name = "PERSON", uniqueConstraints={@UniqueConstraint(columnNames={"PERSON_NAME"})}) public class Person { @Id @GeneratedValue
0
2095
by: rupalirane07 | last post by:
Both grids displays fine. But the problem is only parent datagrid sorting works fine but when i clik on child datagrid for sorting it gives me error: NullReferenceException error Any help........pls urgent ========================================================= <%@ Page Language="vb" AutoEventWireup="false" Codebehind="WebForm3.aspx.vb" Inherits="TestDatagrids.WebForm3"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">...
0
9843
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9683
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10630
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9407
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7811
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7005
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5851
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4045
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3126
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.