Solved

looping through datagrid rows and updating columns

Posted on 2004-08-31
7
713 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
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

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

23 Experts available now in Live!

Get 1:1 Help Now