Solved

DataGrid Question

Posted on 2004-08-26
6
195 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
6 Comments
 
LVL 15

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 15

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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 15

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will get a basic understanding of what section 508 compliance can entail, learn about skip navigation links, alt text, transcripts, and font size controls.

706 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

22 Experts available now in Live!

Get 1:1 Help Now