将 Windows 窗体数据网格正确绑定到数组

发布于 2024-07-13 10:10:40 字数 768 浏览 4 评论 0原文

我正在尝试使用 Windows 窗体数据网格(.NET 1.1)来更新二维数组的元素。 使用微软文档,我已经能够让数据网格正确显示数组。

编辑:我遵循的 MS 说明位于 http://support.microsoft.com/kb/315786。

class myrow {
   private int a, b;
   public int A {get { return a; } set {a = value;}}
   public int B {get { return b; } set {b = value;}}
}

myrow[] myRows = new myrow[5] {
    new myrow(3,2),
    new myrow(2,2),
    new myrow(1,2),
    new myrow(3,3),
    new myrow(3,1),
}

myDataGrid.SetDataBinding(myRows, "");

我还将 DataGrid 的 TableStyle.MappingName 属性设置为 "myrow[]"

让我抓狂的是我无法更新它。 我在表单的单元格中输入一个新值,例如“1123”,然后按 Enter 或 Tab 键,光标移动到下一个单元格,并且该值变回原始值。

我怎样才能让它接受更新?

I'm trying to use a windows forms datagrid (.NET 1.1) to update the elements of a two dimensional array. Using the microsoft documentation, I've been able to get the datagrid to display the array properly.

EDIT: The MS instructions I followed are at http://support.microsoft.com/kb/315786.

class myrow {
   private int a, b;
   public int A {get { return a; } set {a = value;}}
   public int B {get { return b; } set {b = value;}}
}

myrow[] myRows = new myrow[5] {
    new myrow(3,2),
    new myrow(2,2),
    new myrow(1,2),
    new myrow(3,3),
    new myrow(3,1),
}

myDataGrid.SetDataBinding(myRows, "");

I've also got the DataGrid's TableStyle.MappingName property set to "myrow[]".

What's been driving me crazy is that I can't update it. I enter a new value in a cell on the form, say "1123", and hit enter or tab, and the cursor moves to the next cell and the value changes back to the original value.

How can I get it to take the updates?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

几度春秋 2024-07-20 10:10:40

这超出了我的想象,但我认为您需要实现一个在更改网格单元格或单击更新按钮时触发的事件。

This is off the top of my head, but I think you need to implement an event that gets fired when you change a grid cell or click the update button.

笑饮青盏花 2024-07-20 10:10:40

不幸的是我无法检查.NET 1.1,但对于替换DataGrid的.NET 2.0控件DataGridView,不需要手动更新数据源。

希望这会对您有所帮助。

Unfortunately I can't check it for .NET 1.1, but as for .NET 2.0 control DataGridView which replaced DataGrid, it isn't needed to update data source manually.

Hope this will help you somehow.

皇甫轩 2024-07-20 10:10:40

有一个解决方法,如果将数组更改为 ArrayList 或其他集合类,则更新会起作用。

这不是一个真正的答案,因为这仍然没有使用对象数组,并且仍然不太适合常规的二维数据数组。

There's a workaround, if you change the array to an ArrayList or other collection class, then the updates work.

Not a true answer, because that's still not using the object array, and still doesn't fit well with a regular 2D array of data.

烈酒灼喉 2024-07-20 10:10:40

在我的脑海里,我认为你需要......
升级到 Visual Studio 的最新版本。

Off the top of my head, I think you need to ...
Upgrade to a more recent version of Visual Studio.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文