Solved

DataGrid Question

Posted on 2004-08-26
6
205 Views
Last Modified: 2008-03-06
Can someone tell me how I can traverse through a datagrid row by row in vb.net.  I'm using WebForms.

I would like to be able to say something like this.

For i < mydatagrid.totalrows
  Dim myRow =  mydatagrid.rows(i)
  If myRow("MyColumnName") = Value then
     Do Something
  End If
end For
0
Comment
Question by:Fraser_Admin
[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
6 Comments
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11902479
Use the datagrid event ItemDataBound

you could something like this:

private void dgUsers_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
  if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
  {
      if (e.Item.Cells[<<index of col Name>>].Text == value)
     {
         dp something;
     }
  }
}


Regards,

Walter.
0
 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11902538
BTW,

this is a link to a serie of excellent articles about datagrid, and the examples are in VB.NET

http://aspnet.4guysfromrolla.com/articles/040502-1.aspx

Regards,

Walter.
0
 

Author Comment

by:Fraser_Admin
ID: 11902686
I want to compare values with previous records. So I don't think the itembound event will work?
0
How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

 
LVL 16

Expert Comment

by:Walter Ritzel
ID: 11902766
If you use a global var to store the value of the previous row, I believe that yes, you could use.

Regards,

Walter.
0
 
LVL 18

Expert Comment

by:tusharashah
ID: 11903119
You can simply access datagrid using this for loop:
---------------------------------------------------------------------------------
// Vb

For i = 0 To Me.DataGrid1.Items.Count - 1

          Me.DataGrid1.Items(i).Cells(0).Text = "Test"                  //For the first Column  // You can get/set value similarly
     
next

---------------------------------------------------------------------------------
//C#
for(int i=0; i < DataGrid1.Items.Count; i++)
      {
          DataGrid1.Items[i].Cells[0].Text = "Test";
      }
---------------------------------------------------------------------------------

-tushar
0
 
LVL 2

Accepted Solution

by:
Shahankit earned 500 total points
ID: 11906280
For i = 0 To DataGrid1.Items.Count - 1
         For j=1 To DataGrid1.Items.Count - 1
               If DataGrid1.Items(i).Cells(0).Text = DataGrid1.Items(j).Cells(0).Text Then
       
               End If
          next
next

OR

Public Sub datagrid1_itemcreated(ByVal sender As Object, ByVal e As DataGridItemEventArgs) Handles Datagrid1.ItemCreated
        If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Or e.Item.ItemType = ListItemType.SelectedItem Then
            Dim i As String
            i = CType(e.Item.Cells(0).Controls(1), String)
       End If
    End Sub
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
This article will inform Clients about common and important expectations from the freelancers (Experts) who are looking at your Gig.
Viewers will get an overview of the benefits and risks of using Bitcoin to accept payments. What Bitcoin is: Legality: Risks: Benefits: Which businesses are best suited?: Other things you should know: How to get started:
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

763 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