datagridview中使最新增加的一行样式不一样 兼谈网友 skypaf 的问题

本文介绍如何在DataGridView中设置新增行的样式,使其与原有行不同。通过VB.NET代码实现了对新增行的字体、颜色等属性的调整,并提供了避免常见错误的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

版权声明:本文为博主原创文章,转载请在显著位置标明本文出处以及作者网名,未经作者允许不得用于商业目的。
网友skypaf 的原问题发表在csdn的bbs上:http://bbs.youkuaiyun.com/topics/392271272
我个人的理解是他想让新增的一行与原来的行样式不一致。
那么如何在datagridview中完成呢?

 

说实在的以往写代码的时候还真没有这么考虑过这样的问题,
先说看到问题后自己走的一个弯路:
下图中红框标出的部分,

这一行虽然是空白的,但是它的的确确是,datagridview的最后一行,用代码就是:

datagridview.Rows(datagridview.Rows.Count - 1)


我在那里将新增的一行的前景色、字体设置了半天不显示,就是以为有内容的才是最后一行,哪里知道这个自动生成的行才是真正的最后一行。

 

因此,如果需要避免这个问题,请把 DataGridView.AllowUserToAddRows 属性设置为False。

 

那么代码还是比较容易的:

 

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        Call addStudent()
        Call setRowStyle()
    End Sub

    Private Sub addStudent()
        Dim studentName As String = txtName.Text
        Dim studentID As String = txtId.Text
        Dim studentAge As String = txtAge.Text

        Dim student() As String
        ReDim student(2)
        student(0) = studentName
        student(1) = studentID
        student(2) = studentAge
        dgvStudent.Rows.Add(student)
    End Sub

    Private Sub setRowStyle()
        Dim defaultStyle As New DataGridViewCellStyle
        defaultStyle.Font = New Font("宋体", 12)
        defaultStyle.ForeColor = Color.Blue

        Dim newRowStyle As New DataGridViewCellStyle
        newRowStyle.Font = New Font("黑体", 12)
        newRowStyle.ForeColor = Color.Red


        If dgvStudent.Rows.Count > 1 Then
            dgvStudent.Rows(dgvStudent.Rows.Count - 2).DefaultCellStyle = defaultStyle
            dgvStudent.Rows(dgvStudent.Rows.Count - 1).DefaultCellStyle = newRowStyle
        Else
            dgvStudent.Rows(dgvStudent.Rows.Count - 1).DefaultCellStyle = newRowStyle
        End If

    End Sub

显示效果如下:

 

另外一个注意的地方是:

 

代码最后一个判断语句,我开始的时候使用的是:

        If dgvStudent.Rows.Count > 0 Then
            dgvStudent.DefaultCellStyle = defaultStyle
            dgvStudent.Rows(dgvStudent.Rows.Count - 1).DefaultCellStyle = newRowStyle
        End If

想法是,每次增加都是表格所有先使用defaultStyle,然后使新增加的行,也就是最后一行样式变为newRowStyle。

但是结果却不是我想象的:

也就是说,dgvStudent.DefaultCellStyle = defaultStyle 没有发挥作用,推测是因为设置了单个行的defaultcellStyle,所有表格总体样式就对已经设置的行不起作用了。

那只能枚举每一行修改样式:

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Dim newRowStyle As New DataGridViewCellStyle
        newRowStyle.Font = New Font("黑体", 12)
        newRowStyle.ForeColor = Color.Blue
        dgvStudent.DefaultCellStyle = newRowStyle
        For Each singlerow As DataGridViewRow In dgvStudent.Rows
            singlerow.DefaultCellStyle = newRowStyle
        Next
    End Sub

 

 

 

 

由于.net平台下C#和vb.NET很相似,本文也可以为C#爱好者提供参考。

学习更多vb.net知识,请参看vb.net 教程 目录

 

 

 

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

.Net学习

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值