?
Solved

looping through datagrid rows and updating columns

Posted on 2004-08-31
7
Medium Priority
?
967 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
  • 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

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.

Question has a verified solution.

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

Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

864 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