|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
|
Announcements
Want a new Job?
Chapters
Services
Feature Zones
|
IntroductionI was asked by someone how to create a There are many Using the CodeOverriding the methodsTo create a column style, you need to subclass the abstract Protected Overrides Sub Abort(ByVal rowNum As Integer)
The function Protected Overrides Function Commit (ByVal datasource As _
CurrencyManager, ByVal rowNum As Integer) As Boolean
The function Protected Overloads Overrides Sub Edit(ByVal source As _
System.Windows.Forms.CurrencyManager, ByVal rowNum As Integer, ByVal bounds _
As System.Drawing.Rectangle, ByVal [readOnly] As Boolean, _
ByVal instantText As String)
Protected Overrides Function GetMinimumHeight() As Integer
Protected Overrides Function GetPreferredHeight(ByVal g _
As Graphics, ByVal value As Object) As Integer
Protected Overrides Function GetPreferredSize(ByVal g _
As Graphics, ByVal value As Object) As System.Drawing.Size
Protected Overloads Overrides Sub Paint(ByVal g As _
System.Drawing.Graphics, ByVal bounds As System.Drawing.Rectangle, _
ByVal source As System.Windows.Forms.CurrencyManager, _
ByVal rowNum As Integer)
In this Protected Overloads Overrides 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)
This is the second of the three overloaded (and second of the two abstract) Creating Custom ColumnStylesProtected Overloads Overrides 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 o As Object = Me.GetColumnValueAtRow([source], rowNum)
Dim linkText As String = Microsoft.VisualBasic.IIf((o Is Nothing _
OrElse o Is DBNull.Value), Me.NullText, o.ToString)
If (links.Contains(rowNum)) Then
m_link = CType(links(rowNum), LinkLabel)
If Not (m_link.Bounds.Equals(bounds)) Then
m_link.Bounds = bounds
m_link.Visible = True
End If
Else
m_link = New LinkLabel
m_link.Text = linkText
m_link.Parent = Me.DataGridTableStyle.DataGrid
m_link.Links.Add(0, linkText.Length, o)
m_link.Bounds = bounds
AddHandler m_link.LinkClicked, AddressOf link_LinkClicked
links(rowNum) = m_link
End If
End Sub
Usage: Bind Controls to DataGrid - How to use the column style in your application? Private Sub BindControls()
Dim dmTable As New DataTable("Test")
dmTable.Columns.Add(New DataColumn("SNO", GetType(Integer)))
dmTable.Columns.Add(New DataColumn("AuthorName", GetType(String)))
dmTable.Columns.Add(New DataColumn("WebAddress", GetType(String)))
Dim dr As DataRow = dmTable.NewRow()
dr("SNO") = 1
dr("AuthorName") = "Chris"
dr("WebAddress") = "http://msdn.microsoft.com/smartclient/" & _
"default.aspx?pull=/library/en-us/" & _
"dnwinforms/html/datagridcolumnstyle2.asp"
dmTable.Rows.Add(dr)
Dim dr2 As DataRow = dmTable.NewRow()
dr2("SNO") = 2
dr2("AuthorName") = "Evan"
dr2("WebAddress") = "http://msdn.microsoft.com/smartclient/default.aspx" & _
"?pull=/library/en-us/dnwinforms/html/" & _
"datagridcolumnstyle1.asp"
dmTable.Rows.Add(dr2)
Dim dr3 As DataRow = dmTable.NewRow()
dr3("SNO") = 3
dr3("AuthorName") = "Chris"
dr3("WebAddress") = "http://www.micrsoft.com/"
dmTable.Rows.Add(dr3)
dataGrid.TableStyles.Add(CreateTableStyle())
dataGrid.DataSource = dmTable
End Sub 'BindControls
Create Table Style:Private Function CreateTableStyle() As DataGridTableStyle
Dim dst As New DataGridTableStyle
dst.MappingName = "Test"
Dim cs As New ColumnStyles.ColumnStyles.DataGridLinkLabelColumn
AddHandler cs.LinkClicked, AddressOf cs_LinkClicked
cs.MappingName = "WebAddress"
cs.HeaderText = "Web Address"
Dim cb As New DataGridTextBoxColumn
cb.MappingName = "SNO"
cb.HeaderText = "S No"
Dim cs2 As New DataGridTextBoxColumn
cs2.MappingName = "AuthorName"
cs2.HeaderText = "Author Name"
dst.GridColumnStyles.Add(cb)
dst.GridColumnStyles.Add(cs2)
dst.GridColumnStyles.Add(cs)
Return dst
End Function 'CreateTableStyle
Adding handler for the Private Sub cs_LinkClicked(ByVal sender As Object, _
ByVal e As LinkLabelLinkClickedEventArgs)
MessageBox.Show("cs_linkclicked:You have clicked the link")
End Sub
To Fix ItIf the contents of the column are larger (as seen in the figure), then it cannot be viewed properly even if we increase the size of the AcknowledgementsMany thanks for giving the idea of how to create a
|
||||||||||||||||||||||