How to programmatically iterate over datagrid rows?

I suddenly return to WinForms, after several years of web development, and I am having problems with something that should be simple. I have ArrayListbusiness objects related to Windows Forms DataGrid. I want the user to be able to edit cells, and when finished, click the "Save" button. At this point, I would like to repeat all the rows and columns in DataGridto find any changes and save them to the database. But I can not find a way to access the strings DataGrid.

I also want to check individual cells in real time as they are being edited, but I'm sure it can be done. (Maybe not with ArrayListhow DataSource?) But as for the iteration DataGrid, I am completely surprised that this is not possible.

Do I really need to populate business object data in datatables in order to use a datagrid?

+3
source share
4 answers
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]);
}
+5
source
object cell = myDataGrid[row, col];
+1
source

- WinForms 3.0, , 1.1

3.0, VS 2008, .NET 2.0. (, #, 2.0)

Generics (List<DataRow> GodAwful ArrayLists) , , 3 .

0

Yeah, I really just tested everyone again! :) The real answer: you rarely need to iterate over a datagrid. Because even when binding to ArrayList, binding has 2 ways. Nevertheless, it is convenient to know how to do this directly in the grid; it can save several lines of code from time to time.

But NotMyself and Orion gave better answers: Encourage stakeholders to upgrade to a higher version of C # to save development costs and increase maintainability and extensibility.

-2
source

Source: https://habr.com/ru/post/1696387/


All Articles