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

VB.NET中DataGridView绑定Kimtoo.BindingProvider报错无列问题求助

VB.NET中Kimtoo.BindingProvider绑定DataGridView报错:无法向无列的DataGridView添加行

异常详情

System.InvalidOperationException was unhandled
  HResult=-2146233079
  Message=No row can be added to a DataGridView control that does not have columns. Columns must be added first.
  Source=System.Windows.Forms
  StackTrace:
       at System.Windows.Forms.DataGridViewRowCollection.AddInternal(Boolean newRow, Object[] values)
       at System.Windows.Forms.DataGridViewRowCollection.Add()
       at Kimtoo.BindingProvider.DGVBindingProvider.Bind[T](DataGridView grid, IEnumerable`1 items, Boolean append)

VB.NET相关代码

Public Class Payment
    <PrimaryKey, AutoIncrement>
    Public Property Id() As Integer

    <ForeignKey(GetType(Goal), OnDelete := "CASCADE")>
    Public Property GoalId() As Integer

    Public Property Description() As String = ""
    Public Property Amount() As Double

    Public Property CreatedAt() As DateTime = DateTime.Now
    'binding features for Kimtoo Toolkit

    Public Function GetGoal() As Goal
        Return New Goal() With {.Name = "New Car"}
    End Function

    Public Function GetGoalBadge() As DataGridViewBadge
        Return New DataGridViewBadge() With {
            .BackColor = Color.FromArgb(0, 212, 99),
            .ForeColor = Color.White,
            .Value = "New House"
        }
    End Function

    Public Function GetBalance() As Double
        Return 257
    End Function

    Public Function GetBalanceBar() As DatagridViewProgressBar
        Return New DatagridViewProgressBar() With {
            .BackColor = Color.FromArgb(34, 63, 58),
            .FillColor = Color.FromArgb(0, 212, 99),
            .Value = 60
        }
    End Function

    Public Function GetAvatar() As DataGridViewAvatar
        Return New DataGridViewAvatar() With {
            .Value = Me.Description.ToString(),
            .Icon = Image.FromFile("money.png")
        }
    End Function
End Class

Public Sub New()
    If Me.IsInDesignMode() Then
        Return
    End If
    InitializeComponent()
    LoadData()
End Sub

Private Sub LoadData()
    Dim payments As New List(Of Payment)()
    'todo - add testing data
    For i As Integer = 0 To 9
        payments.Add(New Payment() With {
            .Description = $"payment {i + 1}",
            .Amount = 1000,
            .GoalId = 0
        })
    Next i

    'bind data using kimtoo toolkit
    grid.Bind(payments)
End Sub

C#对比代码

public class Payment
{
    [PrimaryKey, AutoIncrement]
    public int Id { get; set; }

    [ForeignKey(typeof(Goal), OnDelete = "CASCADE")]
    public int GoalId { get; set; }

    public string Description { get; set; } = "";
    public double Amount { get; set; }

    public DateTime CreatedAt { get; set; } = DateTime.Now;
    //binding features for Kimtoo Toolkit
    public Goal GetGoal()
        => new Goal()
        {
            Name = "New Car"
        };

    public DataGridViewBadge GetGoalBadge()
   => new DataGridViewBadge()
   {
       BackColor = Color.FromArgb(0, 212, 99),
       ForeColor = Color.White,
       Value = "New House"
   };

    public double GetBalance()
        => 257;

    public DatagridViewProgressBar GetBalanceBar()
         => new DatagridViewProgressBar()
         {
             BackColor = Color.FromArgb(34, 63, 58),
             FillColor = Color.FromArgb(0, 212, 99),
             Value = 60 //todo
         };

    public DataGridViewAvatar GetAvatar()
     => new DataGridViewAvatar()
     {
         Value = this.Description.ToString(),
         Icon = Image.FromFile("money.png")
     };
}

public PagePayments()
{
    if (this.IsInDesignMode()) return;
    InitializeComponent();
    LoadData();
}

private void LoadData()
{
    List<Payment> payments = new List<Payment>();
    //todo - add testing data
    for (int i = 0; i < 10; i++)
    {
        payments.Add(new Payment()
        {
            Description = $"payment {i + 1}",
            Amount = 1000,
            GoalId = 0, //todo,
        });
    }

    //bind data using kimtoo toolkit
    grid.Bind(payments);
}

解决方案分析及处理步骤

1. 调整VB.NET属性定义语法

C#与VB.NET的属性在IL层面存在细微差异,Kimtoo的反射逻辑可能无法识别VB.NET的旧版属性写法。将属性定义改为简化的自动属性语法:

' 原写法
Public Property Id() As Integer
' 修改为
Public Property Id As Integer

去掉属性名后的括号,确保反射时能正确识别属性字段。

2. 手动预生成DataGridView列

如果自动列生成失效,可在绑定数据前手动添加对应列,避免直接添加行时报错:

Private Sub LoadData()
    Dim payments As New List(Of Payment)()
    'todo - add testing data
    For i As Integer = 0 To 9
        payments.Add(New Payment() With {
            .Description = $"payment {i + 1}",
            .Amount = 1000,
            .GoalId = 0
        })
    Next i

    ' 手动添加基础列
    grid.Columns.Add("Id", "ID")
    grid.Columns.Add("Description", "描述")
    grid.Columns.Add("Amount", "金额")
    grid.Columns.Add("CreatedAt", "创建时间")
    ' 自定义类型列(如DataGridViewBadge)可按Kimtoo文档配置添加

    'bind data using kimtoo toolkit
    grid.Bind(payments)
End Sub

3. 验证Kimtoo组件的VB.NET兼容性

检查当前使用的Kimtoo.BindingProvider版本是否支持VB.NET,部分组件可能优先适配C#环境,尝试更新至最新版本或查阅官方文档中VB.NET的绑定说明。

4. 确认特性与访问修饰符正确性

确保Payment类的所有属性均为Public,且特性(如PrimaryKey、AutoIncrement)在VB.NET中语法正确,多特性间的逗号分隔无错误。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 01:05:46