Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1).
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old February 10th, 2006, 11:19 AM
Registered User
Join Date: Feb 2006
Location: , , .
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to Loop Through Datagrid

I've created a datagrid and populated it. Now I would like to loop through each row in the datagrid, and save the values in columns 1 and 2 so I can pass the values to a stored procedure. I haven't been successful in finding the code construct for doing this. Any suggestions?

  #2 (permalink)  
Old February 11th, 2006, 10:56 AM
Registered User
Join Date: Jan 2006
Location: Crawley, Sussex, United Kingdom.
Posts: 9
Thanks: 0
Thanked 0 Times in 0 Posts

As another beginner I have found datagrid difficult to use.
However I assume you have bound the data grid to some data of some sort? If so just extract what you need from the array, or whatever it's bound to, and use that.
If the datagrid is not bound to some data I would be interested to know how you did it.
Good luck

  #3 (permalink)  
Old February 28th, 2007, 11:52 AM
Registered User
Join Date: Feb 2007
Location: , Virginia, USA.
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts

Had a similar problem/requirement in C# (using .NET 1.1):

// Assumes you have DataView DView
// DataView has a DataTable getter/setter
System.Data.DataTable DTable = DView.Table;

// Use DataRow collection in DataTable to loop
foreach (DataRow DRow in DTable.Rows)
  // My DTable has a column name "SampleId",
  // Yours will obviously be different...
  Console.WriteLine("Column Value :" + DRow["SampleId"].ToString());

  // You can also use array-type indexing, to get the
  // correct column value from the current row
  // As always, indices start with 0...
  Console.WriteLine("Column Value :" + DRow[0].ToString());

Found this link useful, too: http://aspalliance.com/6_Summary_Row..._of_Techniques

Good luck...
  #4 (permalink)  
Old April 27th, 2007, 11:49 PM
vbn vbn is offline
Authorized User
Join Date: May 2005
Location: , , .
Posts: 23
Thanks: 0
Thanked 0 Times in 0 Posts


In the below code myTable is a DataTable which is underlying the datagrid.

for (i = 0; i <= myTable.Rows.Count - 1;i++)
            if (Convert.ToDouble(myTable.Rows[i][1]) ==0)
            { Amt = Convert.ToDouble(myTable.Rows[i][2]) * (-1);
                Amt = Convert.ToDouble(myTable.Rows[i][1]);}
                prmTcode.Value = VouNo;
                prmTdate.Value = Tdate;
                prmAccName.Value = myTable.Rows[i][0];
                prmTamt.Value = Amt;
                prmCat.Value = PrivateTransactionCat;
        }// for end
        MessageBox.Show("Transaction Written to Database");
    }// end try
    catch (SqlException ex)
        MessageBox.Show("Could not Add Transaction to Database");

Database programming usingVisual Basic 2005

Similar Threads
Thread Thread Starter Forum Replies Last Post
Custom paging in Datagrid with datagrid page count madhusrp ASP.NET 1.0 and 1.1 Professional 12 June 2nd, 2008 01:15 PM
User COntrol Datagrid inside datagrid rodmcleay ASP.NET 1.0 and 1.1 Professional 3 April 14th, 2007 10:11 AM
Loop through a datagrid asters General .NET 2 April 3rd, 2007 09:21 AM
problem with foreach loop in Checkboxes datagrid mbge9pjb .NET Framework 2.0 2 December 20th, 2005 10:03 AM
nested while loop doesn't loop hosefo81 PHP Databases 5 November 12th, 2003 08:46 AM

Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.