You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET WinForms DataGridView员工标签行不显示问题求助

解决VB.NET WinForms DataGridView中员工标签行不显示的问题

可能的原因及对应解决方案

1. DataTable约束导致无效行被过滤

如果summaryTable的EmpId列设置了主键或非空约束,标签行中EmpId赋值为DBNull.Value会违反约束,DataGridView会自动隐藏这类无效行。

解决代码:

' 移除EmpId列的主键约束(如果存在)
If summaryTable.PrimaryKey IsNot Nothing AndAlso summaryTable.PrimaryKey.Any(Function(col) col.ColumnName = "EmpId") Then
    summaryTable.PrimaryKey = New DataColumn() {}
End If

' 确保EmpId列允许空值
summaryTable.Columns("EmpId").AllowDBNull = True

2. 标签行与考勤行的插入顺序错误

如果先添加了员工的考勤行,再将标签行追加到summaryTable末尾,标签行就会出现在该员工考勤行的后面,而非前面。

正确的插入顺序代码:

' 遍历每位员工
For Each empRow As DataRow In selectedEmpTable.Rows
    Dim empId = empRow("EmpId").ToString()
    Dim empName = empRow("EmpName").ToString()

    ' 步骤1:先插入员工标签行
    Dim labelRow As DataRow = summaryTable.NewRow()
    labelRow("EmpId") = DBNull.Value
    labelRow("Date") = DBNull.Value
    labelRow("IN1") = DBNull.Value
    labelRow("OUT2") = DBNull.Value
    labelRow("LateMin") = DBNull.Value
    labelRow("WorkMin") = DBNull.Value
    labelRow("OTMin") = DBNull.Value
    labelRow("Remarks") = $"Employee: {empId} - {empName}"
    summaryTable.Rows.Add(labelRow)

    ' 步骤2:再添加该员工的所有考勤行
    Dim empAttendanceRows = originalAttendanceTable.Select($"EmpId = '{empId}'")
    For Each attRow As DataRow In empAttendanceRows
        summaryTable.Rows.Add(attRow.ItemArray.Clone()) ' 克隆行避免引用问题
    Next
Next

3. DataGridView的筛选或排序设置干扰

如果summaryTable绑定后设置了RowFilter或者DataGridView开启了排序,可能导致标签行被过滤或排到非预期位置。

清除筛选与排序的代码:

' 重置数据源,清除筛选
dgvFinal.DataSource = Nothing
dgvFinal.DataSource = summaryTable

' 禁止自动排序(可选,避免用户误操作打乱顺序)
For Each col As DataGridViewColumn In dgvFinal.Columns
    col.SortMode = DataGridViewColumnSortMode.NotSortable
Next

4. 优化标签行视觉效果(可选)

当前的CellFormatting事件仅设置了样式,可扩展为让标签行跨列显示,提升可读性:

Private Sub dgvFinal_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles dgvFinal.CellFormatting
    Dim row = dgvFinal.Rows(e.RowIndex)
    If row.Cells("Remarks").Value IsNot Nothing Then
        Dim remarksText = row.Cells("Remarks").Value.ToString()
        If remarksText.StartsWith("Employee:") Then
            ' 设置标签行样式
            e.CellStyle.BackColor = Color.DarkGreen
            e.CellStyle.ForeColor = Color.White
            e.CellStyle.Font = New Font("Segoe UI", 10, FontStyle.Bold Or FontStyle.Italic)
            e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter

            ' 清空其他列内容,视觉上实现跨列效果
            For Each cell As DataGridViewCell In row.Cells
                If cell.ColumnIndex <> dgvFinal.Columns("Remarks").Index Then
                    cell.Value = String.Empty
                    cell.Style.BackColor = Color.DarkGreen
                End If
            Next
        End If
    End If
End Sub

' 调整标签行高度
Private Sub dgvFinal_RowPrePaint(sender As Object, e As DataGridViewRowPrePaintEventArgs) Handles dgvFinal.RowPrePaint
    Dim row = dgvFinal.Rows(e.RowIndex)
    If row.Cells("Remarks").Value IsNot Nothing AndAlso 
       row.Cells("Remarks").Value.ToString().StartsWith("Employee:") Then
        row.Height = 32 ' 根据字体大小调整高度
    End If
End Sub

内容的提问来源于stack exchange,提问作者Ajit Karode

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 07:44:52