Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

DataGrid Question

Posted on 2004-08-26
6
Medium Priority
?
215 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

FAQ pages provide a simple way for you to supply and for customers to find answers to the most common questions about your company. Here are six reasons why your company website should have a FAQ page
When crafting your “Why Us” page, there are a plethora of pitfalls to avoid. Follow these five tips, and you’ll be well on your way to creating an effective page.
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

721 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