请教Windows Form的问题,怎样DataGrid中设置鼠标点选当前行背景色? --up有分

pfengk 2004-12-10 05:54:05
如题
小弟我这一周都在搞这个,现在还没有解决。也郁闷了一周了! :(

麻烦不要给我第三方厂商 的控件,那种控件我有一个,而且非常不错,完成这个功能很容易只要设置属性就可以了。但是程序要求用.net"原装"的datagrid。
...全文
148 20 打赏 收藏 转发到动态 举报
写回复
用AI写文章
20 条回复
切换为时间正序
请发表友善的回复…
发表回复
pfengk 2004-12-13
  • 打赏
  • 举报
回复
结贴了。 谢谢各位的帮忙
pfengk 2004-12-13
  • 打赏
  • 举报
回复
回复人: landlordh(software) ( )

谢谢你了,你提供的代码中以下代码是我想要的答案
If DataGrid1.CurrentRowIndex >= 0 Then
DataGrid1.Select(DataGrid1.CurrentRowIndex)
End If
CloneCenter 2004-12-13
  • 打赏
  • 举报
回复
UP↑↑↑↑
pfengk 2004-12-13
  • 打赏
  • 举报
回复
回复人: lyj670(空中一只鸟) ( ) 信誉:75 2004-12-13 08:24:00 得分: 0


虽然你们说的都不错,但是我想楼主是希望点中其中一格,整行都变色吧.



是呀是呀 老兄你说的没有错。我要的就是这样的效果
qiashzhou 2004-12-13
  • 打赏
  • 举报
回复
up
_jfeng 2004-12-13
  • 打赏
  • 举报
回复
ding,jf啦!@#
随风的心雨 2004-12-13
  • 打赏
  • 举报
回复
一起进来学习,UP
landlordh 2004-12-13
  • 打赏
  • 举报
回复
那么你可以参考以下代码:

'通用样式化Datagird
'Power by:landlordh
'调用:
' strHeaders() 里面改为自已需要的列名
' iWidths() 里面改为相对应的列宽
' inputnum 在样式化之前赋值
' style(DataGrid1, DataSet1.Tables("codes"))
'
'=================================================================

'列名,字段对应名
Public strHeaders() As String = {"编号", "时间", "主题", "内容"}
'列宽,为0隐藏(优先级低于inputnum),对应的列宽
Public iWidths() As Integer = {0, 70, 130, 0}
'显示几列,为空则显示所有,为0则隐藏所有
Public inputnum As String

Public Sub style(ByVal datagridname As DataGrid, ByVal dataname As DataTable)
Try
datagridname.DataSource = dataname
'dataname.DefaultView.AllowNew = False
'dataname.DefaultView.AllowEdit = False
Dim i As Integer
If Trim(inputnum) <> "" Then
Dim num As Integer
Try
num = CInt(Trim(inputnum))
Try
For i = 0 To iWidths.Length - 1
If i >= inputnum Then
iWidths(i) = 0
End If
Next
Catch ex As Exception
End Try
Catch ex As Exception
MsgBox("你输入的字符不符合要求")
End Try
End If
Dim ts As New DataGridTableStyle
ts.MappingName = dataname.TableName
ts.AlternatingBackColor = System.Drawing.Color.PapayaWhip
ts.BackColor = System.Drawing.Color.WhiteSmoke
ts.SelectionBackColor = System.Drawing.Color.LightSteelBlue
ts.SelectionForeColor = Color.Black
ts.RowHeaderWidth = 10
ts.PreferredRowHeight = 20
For i = 0 To dataname.Columns.Count - 1
Dim ac As New DataGridTextBoxColumn
'处理点击行↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
AddHandler ac.TextBox.Enter, AddressOf TextBoxEnterHandler
ac.HeaderText = strHeaders(i)
ac.MappingName = dataname.Columns(i).ColumnName
ac.TextBox.Width = iWidths(i)
ts.PreferredColumnWidth = ac.TextBox.Width
'哪列,i=第几列数-1
If i = 1 Then
ac.Format = "yyyy-mm-dd hh:mm:ss"
'ac.ReadOnly = True
'ac.NullText = ""
ac.Alignment = HorizontalAlignment.Center
End If
If i = 1 Or i = 2 Then
'ac.ReadOnly = True
'ac.NullText = ""
ac.Alignment = HorizontalAlignment.Center
End If
ts.GridColumnStyles.Add(ac)
Next
datagridname.TableStyles.Clear()
datagridname.TableStyles.Add(ts)
Catch ex As Exception
MessageBox.Show("格式化网格出错了")
End Try
End Sub

Public Sub TextBoxEnterHandler(ByVal sender As Object, ByVal e As System.EventArgs)
Dim inner As Control = sender
inner.Visible = False
If DataGrid1.CurrentRowIndex >= 0 Then
DataGrid1.Select(DataGrid1.CurrentRowIndex)
End If
'filltext()
End Sub
lyj670 2004-12-13
  • 打赏
  • 举报
回复
虽然你们说的都不错,但是我想楼主是希望点中其中一格,整行都变色吧.
pfengk 2004-12-12
  • 打赏
  • 举报
回复
回复人: landlordh(software) ( )

老兄能不能具体点? 当前行对象是什么属性呢?
GnuDoyng 2004-12-12
  • 打赏
  • 举报
回复
啧~难道SelectionBackColor不可以设置么?
xisat 2004-12-12
  • 打赏
  • 举报
回复
^_^
ximazi 2004-12-12
  • 打赏
  • 举报
回复
dim a as datagird
a.TableStyles(0).SelectionBackColor = Color.red
chinahth 2004-12-11
  • 打赏
  • 举报
回复
呵呵,上面的答案几乎都正确,还很不错呢
landlordh 2004-12-11
  • 打赏
  • 举报
回复
单击光标键来改变Datagrid当前行的颜色(可以在属性中选择或写代码)
ts.SelectionBackColor = System.Drawing.Color.LightSteelBlue
wuxuetie 2004-12-11
  • 打赏
  • 举报
回复
.net"原装"的datagrid,本身选中就会改变背景色,当然要先设置好SelectionBackColor 属性!
daisy8675 2004-12-11
  • 打赏
  • 举报
回复
那UP好了^^
ZJMT 2004-12-11
  • 打赏
  • 举报
回复
allanli 2004-12-10
  • 打赏
  • 举报
回复
如果DataGrid装载了样式,那么
DataGrid.TableStyles(0).SelectionBackColor = Color.LightBlue
否则
DataGrid.SelectionBackColor = Color.LightBlue
kenMoxi 2004-12-10
  • 打赏
  • 举报
回复
up

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧