?
Solved

looping through datagrid rows and updating columns

Posted on 2004-08-31
7
Medium Priority
?
896 Views
Last Modified: 2010-04-15
i have a system.windows.forms.datagrid in which i want to loop through the rows.  while looping i need to get the value of two columns and update a third column.  

the datagrid datasource is a dataTable set at startup.

not to sure how to do this, can anyone get me started or point me in the right direction, thanks
0
Comment
Question by:meyerc74
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 9

Expert Comment

by:msdixon
ID: 11945688
do it after you call the .DataBind() method on your grid.

the Items property of the grid correlates to the rows, and the Cells property of the item correlates to the cells in the row.

datagrid.Item[0].Cells[3] is the first row, forth cell.
0
 
LVL 1

Author Comment

by:meyerc74
ID: 11945858
i'm building a windows application using system.windows.forms.datagrid
0
 
LVL 9

Expert Comment

by:msdixon
ID: 11947564
oh, sorry. i haven't used the data grid much. we used a 3rd party grid. i'll see if i can find some time to help you figure this out.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 1

Author Comment

by:meyerc74
ID: 11952275
ok...i found if i set my DataTable as a class member(rather than a local function var) i can loop through the DataTable rows, update and then reset the DataSource of my datagrid.

while this does do what i need it to do, it seems horribly inefficent, and i would prefer to do each row individually so the end user can see at any giving time which row is currently being processed.
0
 
LVL 9

Accepted Solution

by:
msdixon earned 500 total points
ID: 11952594
here's something to get you started. apparently the grid has an indexer myGrid[colOrdinal, cellOrdinal]

i also saw two properties VisibleRowCount, and VisibleCellCount.

you could try something like this:

         for (int r = 0; r < grid.VisibleRowCount; r++)
         {
            for (int c = 0; c < grid.VisibleColumnCount; c++)
            {
               // manipulate the cells
               grid[r, c] += "abc";
            }
         }

i haven't tested this, but it SHOULD work...  ;-)

taken from:
http://www.syncfusion.com/FAQ/WinForms/FAQ_c44c.asp#q898q


5.63 How do I iterate through all the rows and columns in my datagrid?
      

You use the row index and column index as indexers on the DataGrid object.
[C#]
     private void button1_Click(object sender, System.EventArgs e)
     {
          CurrencyManager cm = (CurrencyManager)this.BindingContext[this.dataGrid1.DataSource];

          int rowCount = cm.Count;
          //assumes datasource is a datatable...
          int colCount = ((DataTable)this.dataGrid1.DataSource).Columns.Count;

          for(int row = 0; row < rowCount; row++)
          {
               for(int col = 0; col < colCount; col++)
               {
                    object cellValue = this.dataGrid1[row, col];
                    Console.Write(cellValue.ToString() + " ");
               }
               Console.WriteLine("");
          }
     }

[VB.NET]
     Private Sub button1_Click(sender As Object, e As System.EventArgs)
          Dim cm As CurrencyManager = CType(Me.BindingContext(Me.dataGrid1.DataSource), CurrencyManager)

          Dim rowCount As Integer = cm.Count
          'assumes datasource is a datatable...
           Dim colCount As Integer = CType(Me.dataGrid1.DataSource, DataTable).Columns.Count

          Dim row As Integer
          For row = 0 To rowCount - 1
               Dim col As Integer
               For col = 0 To colCount - 1
                    Dim cellValue As Object = Me.dataGrid1(row, col)
                    Console.Write((cellValue.ToString() + " "))
               Next col
               Console.WriteLine("")
          Next row
     End Sub 'button1_Click
0
 
LVL 1

Author Comment

by:meyerc74
ID: 11953193
excellent...thank you. the visibleRowCount worked.  probably should have seen that myself, but we live and learn :)

thanks again

0
 
LVL 9

Expert Comment

by:msdixon
ID: 11957045
no problem.

it wasn't very intuitive, and it brings up a question: if i have hidden columns or rows, would that throw things off? or are the hidden ones inaccessible?

glad i could help.
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question