Solved

looping through datagrid rows and updating columns

Posted on 2004-08-31
7
837 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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 125 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

705 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