Solved

DataGrid Question

Posted on 2004-08-26
6
208 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
PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WordPress 8 70
Website Question - New Site 3 49
Customising IE behaviour on certain pages 2 99
Display field if column exists 7 58
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Dramatic changes are revolutionizing how we build and use technology. Every company is automating, digitizing, and modernizing operations. We need a better, more connected way to work together as teams so we can harness the insights from our system…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
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…

734 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