怎样让.NET中的DataGrid控件定制的显示列内容
.NET中的DataGrid控件功能十分强大,它完全可以实现您的需求。您可以参照下列步骤:1、从DataGridTextBoxColumn类继承一个新的类,比如MyColumnStyle。
.NET中的DataGrid控件功能十分强大,它完全可以实现您的需求。您可以参照下列步骤:
1、从DataGridTextBoxColumn类继承一个新的类,比如MyColumnStyle。
2、重载paint事件。注意:DataGrid控件有三个不同的paint事件的实现。
3、在paint事件里,对当前cell的值进行判断,如果〉50,那就让它显示红色,〈50,显示绿色。
4、把MyColumnStyle作为Price列的GridColumnStyles.
下面是一段简单的例子:
Public Class Form1
Inherits System.Windows.Forms.Form
Private ds As New DataSet("myDataSet")
Dim t2 As New DataTable("Table2")
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents DataGrid1 As System.Windows.Forms.DataGrid
Friend WithEvents Button1 As System.Windows.Forms.Button
Friend WithEvents Button2 As System.Windows.Forms.Button
Private Sub InitializeComponent()
Me.DataGrid1 = New System.Windows.Forms.DataGrid()
Me.Button1 = New System.Windows.Forms.Button()
Me.Button2 = New System.Windows.Forms.Button()
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).BeginInit()
Me.SuspendLayout()
'
'DataGrid1
'
Me.DataGrid1.ColumnHeadersVisible = False
Me.DataGrid1.DataMember = ""
Me.DataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText
Me.DataGrid1.Location = New System.Drawing.Point(16, 8)
Me.DataGrid1.Name = "DataGrid1"
Me.DataGrid1.Size = New System.Drawing.Size(408, 176)
Me.DataGrid1.TabIndex = 0
'
'Button1
'
Me.Button1.Location = New System.Drawing.Point(256, 216)
Me.Button1.Name = "Button1"
Me.Button1.Size = New System.Drawing.Size(160, 32)
Me.Button1.TabIndex = 1
Me.Button1.Text = "Fill Data"
'
'Button2
'
Me.Button2.Location = New System.Drawing.Point(72, 208)
Me.Button2.Name = "Button2"
Me.Button2.Size = New System.Drawing.Size(120, 32)
Me.Button2.TabIndex = 2
Me.Button2.Text = "Change Color"
'
'Form1
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(472, 273)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.Button2, Me.Button1, Me.DataGrid1})
Me.Name = "Form1"
Me.Text = "Form1"
CType(Me.DataGrid1, System.ComponentModel.ISupportInitialize).EndInit()
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'Initialize the DataSet by santoxin
InitDataSet()
End Sub
Private Sub InitDataSet()
Dim i As Integer
Dim col3 As New DataColumn("id", Type.GetType("System.Int32"))
Dim col4 As New DataColumn("Price", Type.GetType("System.Int32"))
Dim col5 As New DataColumn("extra", Type.GetType("System.String"))
t2.Columns.Add(col3)
t2.Columns.Add(col4)
t2.Columns.Add(col5)
' Set primary key column.
Dim t2KeyCol(1) As DataColumn
t2KeyCol(0) = col3
t2.PrimaryKey = t2KeyCol
' Add two rows. Note that the id column can't be the
' same as existing rows in the DataSet table.
Dim newRow As DataRow
For i = 0 To 9
newRow = t2.NewRow()
newRow("id") = i
newRow("Price") = 45 + i
newRow("extra") = "extra Column " + i.ToString
t2.Rows.Add(newRow)
Next i
ds.Tables.Add(t2)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
DataGrid1.DataSource = ds
DataGrid1.DataMember = "Table2"
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim myGridTableStyle As DataGridTableStyle = New DataGridTableStyle()
myGridTableStyle.MappingName = "Table2"
Dim txtCol As New DataGridTextBoxColumn()
txtCol.MappingName = "id"
txtCol.HeaderText = "Is ID"
myGridTableStyle.GridColumnStyles.Add(txtCol)
txtCol = Nothing
txtCol = New MyColumnStyle()
txtCol.MappingName = "Price"
txtCol.HeaderText = "Is Price"
myGridTableStyle.GridColumnStyles.Add(txtCol)
txtCol = Nothing
txtCol = New DataGridTextBoxColumn()
txtCol.MappingName = "extra"
txtCol.HeaderText = "Is Extra"
myGridTableStyle.GridColumnStyles.Add(txtCol)
txtCol = Nothing
DataGrid1.TableStyles.Clear()
DataGrid1.TableStyles.Add(myGridTableStyle)
' Note that DataGridColumnStyle objects will
' be created automatically for the first DataGridTableStyle
' when you add it to the GridColumnStylesCollection.*/
End Sub
End Class
Class MyColumnStyle : Inherits DataGridTextBoxColumn
Protected Overloads Overrides Sub Paint(ByVal g As Graphics, ByVal bounds As Rectangle, ByVal source As CurrencyManager, ByVal rowNum As Integer)
MyBase.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 forebrush As SolidBrush = New SolidBrush(Color.Red)
Dim backbrush As SolidBrush = New SolidBrush(Color.Green)
MyBase.Paint(g, bounds, source, rowNum, backbrush, forebrush, alignToRight)
End Sub
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)
Dim redbrush As SolidBrush = New SolidBrush(Color.Red)
Dim greenbrush As SolidBrush = New SolidBrush(Color.Green)
'GetColumnValueAtRow(source, rowNum)
If CType(GetColumnValueAtRow(source, rowNum), Integer) > 50 Then
MyBase.Paint(g, bounds, source, rowNum, backBrush, redbrush, alignToRight)
Else
MyBase.Paint(g, bounds, source, rowNum, backBrush, greenbrush, alignToRight)
End If
End Sub
End Class
- 微软全球技术中心 VB开发支持
本文作者:佚名 来源:http://www.51blog.net
CIO之家 www.ciozj.com 微信公众号:imciow
免责声明:本站转载此文章旨在分享信息,不代表对其内容的完全认同。文章来源已尽可能注明,若涉及版权问题,请及时与我们联系,我们将积极配合处理。同时,我们无法对文章内容的真实性、准确性及完整性进行完全保证,对于因文章内容而产生的任何后果,本账号不承担法律责任。转载仅出于传播目的,读者应自行对内容进行核实与判断。请谨慎参考文章信息,一切责任由读者自行承担。
延伸阅读