Solved

looping through datagrid rows and updating columns

Posted on 2004-08-31
7
738 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Article by: Najam
Having new technologies does not mean they will completely replace old components.  Recently I had to create WCF that will be called by VB6 component.  Here I will describe what steps one should follow while doing so, please feel free to post any qu…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

895 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now