Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Looping round a datagrid

Status
Not open for further replies.

ggeorgiou01

Programmer
Apr 11, 2005
60
GB
Hi all,

I have a datagrid and i have a itemdatabound event on that datagrid. What i want to do is in the itemdatabound event i want to loop round every single row, and place some code against it...

How do i loop round a datagrid ???

Many Thanks,

George
 
Why do you want to do this? It seems to me like a bit of a waste of time to loop through the datagrid everytime a item is bound to the grid.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Hi Thanks for replying,

I want to do this because, i have an edit hyperlink in and options column... and basically if the RequestType field is either Amend or Add then i want to show the edit hyperlink, and if the RequestType field is Delete i DONT want to show the edit hyperlink...

Many Thanks,

George
 
But why do you want to loop through the DataGrid every time the ItemDataBound event fires (this means that you would be showing/hiding each row's hyperlink every time a row was bound)? Surely all you need to do is for each item that is data bound, set the visibility of the corresponding hyperlink...


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks for your help...

I am relativly new too .net and have only been using it for a couple of months... Does anybody know of a website which could send me in the right direction of how to do this ???

Many Thanks,

George
 
Yes, try reading the following article (there's 18 parts to it so it's a fair amount of reading but it's an excellent series of articles on the DataGrid control)



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
Thank you for your help - you've been a great help - cheers !

Many Thanks,

George
 
The ItemDataBound event is basically a run through each row that is bound, including headers/footers...here's sample code that does some conditional formatting like you're talking about...

Code:
Private Sub dgrSearch_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles dgrSearch.ItemDataBound
    If e.Item.ItemType = ListItemType.Item Or _
       e.Item.ItemType = ListItemType.AlternatingItem Then

      e.Item.Attributes.Add("onmouseover", "this.style.cursor='default'; this.style.backgroundColor=""#C2B9A2""")
      e.Item.Attributes.Add("onmouseout", "this.style.backgroundColor=""#ECE9D8"";")

      Dim lbtn As LinkButton
      lbtn = e.Item.FindControl("lbtnGo")
      If e.Item.DataItem(2) = "1" Then
        lbtn.Font.Strikeout = True
        lbtn.Attributes.Add("onclick", "javascript:alert('This is a closed client!\nTo create a budget please re-open the client and the project that needs a budget.');return false;")
      ElseIf e.Item.DataItem(3) = "1" Then
        lbtn.Attributes.Add("onclick", "javascript:alert('This is a suspended client!\n To create a budget please un-suspend the client and the project that needs a budget.');return false;")
      Else
        lbtn.Attributes.Add("onclick", "javascript:window.location.href='wfrmSearch.aspx?CID=" & CStr(e.Item.DataItem(0)) & "&CName=" & Replace(Replace(CStr(e.Item.DataItem(1)), "'", "\'"), "&", "31448z") & "';return false;")
      End If
      lbtn.Text = e.Item.DataItem(1)
    End If
  End Sub

this gives different attributes to a link button within the datagrid depending on a dataItem field...

"...we both know I'm training to become a cagefighter...see what happens if you try 'n hit me..."
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top