Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

DataGrid Question

Posted on 2004-08-26
6
Medium Priority
?
218 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 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
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.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Q&A with Course Creator, Mark Lassoff, on the importance of HTML5 in the career of a modern-day developer.
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…
Any person in technology especially those working for big companies should at least know about the basics of web accessibility. Believe it or not there are even laws in place that require businesses to provide such means for the disabled and aging p…
Suggested Courses

972 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