winforms 如何以编程方式迭代数据网格行?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:36:23  来源:igfitidea点击:

How to programmatically iterate datagrid rows?

winforms

提问by Mike

I'm suddenly back to WinForms, after years of web development, and am having trouble with something that should be simple. I have an ArrayListof business objects bound to a Windows Forms DataGrid. I'd like the user to be able to edit the cells, and when finished, press a Save button. At that point I'd like to iterate the all the rows and columns in the DataGridto find any changes, and save them to the database. But I can't find a way to access the DataGridrows.

经过多年的 Web 开发,我突然回到了 WinForms,并且遇到了一些应该很简单的事情。我有一个ArrayList绑定到 Windows Forms 的业务对象DataGrid。我希望用户能够编辑单元格,完成后,按保存按钮。那时我想迭代 中的所有行和列 DataGrid以查找任何更改,并将它们保存到数据库中。但我找不到访问DataGrid行的方法。

I'll also want to validate individual cells real time, as they are edited, but I'm pretty sure that can be done. (Maybe not with an ArrayListas the DataSource?) But as for iterating the DataGrid, I'm quite surprised it doesn't seem possible.

我还想实时验证单个单元格,因为它们被编辑,但我很确定可以做到。(也许不使用ArrayListas DataSource?)但至于迭代DataGrid,我很惊讶它似乎不可能。

Must I really stuff my business objects data into datatables in order to use the datagrid?

我真的必须将我的业务对象数据填充到数据表中才能使用数据网格吗?

回答by NotMyself

foreach(var row in DataGrid1.Rows)
{
  DoStuff(row);
}
//Or ---------------------------------------------   
foreach(DataGridRow row in DataGrid1.Rows)
{
  DoStuff(row);
}
//Or ---------------------------------------------
for(int i = 0; i< DataGrid1.Rows.Count - 1; i++)
{
  DoStuff(DataGrid1.Rows[i]);
}

回答by Mike

object cell = myDataGrid[row, col];

回答by Orion Edwards

Is there anything about WinForms 3.0 that is so much better than in 1.1

WinForms 3.0 有什么比 1.1 更好的地方吗?

I don't know about 3.0, but you can write code in VS 2008 which runs on the .NET 2.0 framework. (So, you get to use the latest C# language, but you can only use the 2.0 libraries)

我不知道 3.0,但您可以在 VS 2008 中编写代码,该代码在 .NET 2.0 框架上运行。(因此,您可以使用最新的 C# 语言,但只能使用 2.0 库)

This gets you Generics (List<DataRow>instead of those GodAwful ArrayLists) and a ton of other stuff, you'll literally end up writing 3x less code.

这让你得到泛型(List<DataRow>而不是那些 GodAwful ArrayLists)和大量其他东西,你最终会编写 3 倍的代码。

回答by Mike

Aha, I was really just testing everyone once again! :) The real answer is, you rarely need to iterate the datagrid. Because even when binding to an ArrayList, the binding is 2 way. Still, it is handy to know how to itereate the grid directly, it can save a few lines of code now and then.

啊哈,我真的只是再次测试每个人!:) 真正的答案是,您很少需要迭代数据网格。因为即使绑定到 ArrayList,绑定也是 2 种方式。尽管如此,知道如何直接迭代网格还是很方便的,它可以时不时地节省几行代码。

But NotMyself and Orion gave the better answers: Convince the stakeholders to move up to a higher version of C#, to save development costs and increase maintainability and extensability.

但 NotMyself 和 Orion 给出了更好的答案:说服利益相关者升级到更高版本的 C#,以节省开发成本并提高可维护性和可扩展性。