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

VB.NET中DataGridView加载时行标题未激活,点击才显示问题

VB.NET DataGridView行标题加载后未自动激活的问题处理

这不是DataGridView的控件异常,属于默认行为——窗体加载完成后,DataGridView并不会自动获得焦点,而行标题的激活样式需要控件处于焦点状态且存在选中行才会显示。

解决方法

要实现窗体加载后行标题自动激活,只需在合适的时机给DataGridView设置焦点并选中指定行即可,推荐两种场景的实现方式:

  1. 静态数据绑定场景(窗体加载时已完成数据填充)
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    ' 先确认DataGridView存在数据行
    If DataGridView1.Rows.Count > 0 Then
        ' 选中第一行
        DataGridView1.Rows(0).Selected = True
        ' 让DataGridView获得焦点
        DataGridView1.Focus()
    End If
End Sub
  1. 动态数据绑定场景(比如从数据库异步拉取数据)
Private Sub DataGridView1_DataBindingComplete(sender As Object, e As DataGridViewBindingCompleteEventArgs) Handles DataGridView1.DataBindingComplete
    If DataGridView1.Rows.Count > 0 Then
        DataGridView1.Rows(0).Selected = True
        DataGridView1.Focus()
    End If
End Sub

效果对比

  • 未处理状态(无激活行标题):无激活行标题
  • 处理后状态(激活行标题):激活行标题

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.23 17:42:04