tags:

views:

2094

answers:

5

Hi,

I'm suddenly back to WinForms, after years of web development, and am having trouble with something that should be simple. I have an ArrayList of 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 DataGrid to find any changes, and save them to the database. But I can't find a way to access the DataGrid rows.

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 ArrayList as the DataSource?) But as for iterating the DataGrid, I'm quite surprised it doesn't seem possible.

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

Thanks,
Mike

+3  A: 
                        foreach(var row in DataGrid1.Rows)
            {
             DoStuff(row);
            }

     foreach(DataGridRow row in DataGrid1.Rows)
            {
              DoStuff(row);
            }

for(int i = 0; i< DataGrid1.Rows.Count - 1; i++)
{
  DoStuff(DataGrid1.Rows[i]);
}

                  
NotMyself
A: 

Hey, no cheating, you have to use .NET 1.1! :) Sorry I forgot to mention that little detail.

Is there anything about WinForms 3.0 that is so much better than in 1.1, that I could use to convince my employer to make the switch? I'm brand new here, and don't want to stir up trouble. And I'm only doing this one little app for them, and then I'm off to somewhere else.

Thanks, Mike

Mike
A: 

Is there anything about WinForms 3.0 that is so much better than in 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)

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.

Orion Edwards
A: 

I was just testing you guys. Here's the answer:

object cell = myDataGrid[row, col];

Actually a colleague provided the answer.

Thanks,
Mike

Mike
A: 

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.

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.

Mike