‘重写DataGridTextBoxColumn Private Class DataGridColoredTextBoxColumn Inherits DataGridTextBoxColumn '继承接口 Private _parentDagaGrid As DataGrid Public Sub New(ByRef parentName As DataGrid) _parentDagaGrid = parentName End Sub Private Function GetText(ByVal Value As Object) As String If TypeOf (Value) Is System.DBNull Then Return "" ElseIf Value Is Nothing Then Return "" Else Return Value.ToString End If End Function 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, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean) Dim text As String Dim iCount As Integer Try text = GetText(GetColumnValueAtRow(source, rowNum)).Trim Catch ex As Exception Finally If Me._parentDagaGrid.CurrentRowIndex = rowNum Then MyBase.PaintText(g, bounds, "", Brushes.Green, foreBrush, alignToRight) ’画背景 g.DrawString(text, New Font("宋体", 14), Brushes.Red, bounds.X + 2, bounds.Y + 2) ’画字,设置字体及其颜色 Else MyBase.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight) End If End Try End Sub End Class 至于datagrid的选中操作这里就不写了。
在Click事件中用HitTest测试一下点是哪行,然后用DataGrid.Select(n)方法选中该行.
在Click事件中用HitTest测试一下点是哪行,然后用DataGrid.Select(n)方法选中该行.
{
datagrid1.CurrentCell = new DataGridCell(datagrid1.CurrentCell.RowNumber,0);
datagrid1.Select(datagrid1.CurrentCell.RowNumber);
}
要重写dataGrid 的 DataGridTextBoxColumn 类
private void Gridaaa_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
if(e.Item.ItemType==ListItemType.Item || e.Item.ItemType==ListItemType.AlternatingItem )
{
e.Item.Attributes.Add("onmousedown","this.style.backgroundColor='Silver'");
}
}
color color 设置或获取对象文本的颜色。
cursor cursor 设置或获取当鼠标指针指向对象时所使用的鼠标指针。
fontFamily 设置或获取对象文本所使用的字体名称。
fontSize 设置或获取对象文本使用的字体大小。
fontStyle 设置或获取对象的字体样式,如斜体、常规或倾斜。
fontVariant 设置或获取对象文本是否以小型大写字母显示。
fontWeight 设置或获取对象的字体宽度。
height 设置或获取对象的高度。
imeMode 获取输入方法编辑器(IME)的状态。
MessageBox.Show(dataGrid1.SelectionForeColor.Name.ToString());
Private Class DataGridColoredTextBoxColumn
Inherits DataGridTextBoxColumn '继承接口 Private _parentDagaGrid As DataGrid Public Sub New(ByRef parentName As DataGrid)
_parentDagaGrid = parentName
End Sub Private Function GetText(ByVal Value As Object) As String If TypeOf (Value) Is System.DBNull Then
Return ""
ElseIf Value Is Nothing Then
Return ""
Else
Return Value.ToString
End If End Function
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, ByVal backBrush As System.Drawing.Brush, ByVal foreBrush As System.Drawing.Brush, ByVal alignToRight As Boolean)
Dim text As String
Dim iCount As Integer Try
text = GetText(GetColumnValueAtRow(source, rowNum)).Trim Catch ex As Exception
Finally
If Me._parentDagaGrid.CurrentRowIndex = rowNum Then
MyBase.PaintText(g, bounds, "", Brushes.Green, foreBrush, alignToRight) ’画背景
g.DrawString(text, New Font("宋体", 14), Brushes.Red, bounds.X + 2, bounds.Y + 2) ’画字,设置字体及其颜色
Else
MyBase.PaintText(g, bounds, text, backBrush, foreBrush, alignToRight)
End If End Try
End Sub End Class
至于datagrid的选中操作这里就不写了。