Solved

Datalist itemdatabound problem c#

Posted on 2006-07-10
2
3,009 Views
Last Modified: 2011-09-20
Hello experts,

I have binding data to me datalist from codebehind.
I need to grab the contents of a label in the datalist, make some calculations and rebind the result to the label.

But my datalist count method returns 0 even if there there are records returned, so this does not work;-

 for (int i = 0; i < DataList1.Items.Count; i++)
{
//calcualte stuff
}

and so my calcualtion are not taking place

Help!!!
0
Comment
Question by:claracruz
2 Comments
 
LVL 5

Expert Comment

by:GENTP
Comment Utility
I think you're trying to do this with the control's name, as opposed to the argument past in's name. Also, on the item bound, you have one complete row, not a column, so you'll need to use the cells property too. Try this:

private void dgHistory_ItemBound(object source, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
     for(int i=0;i<e.Item.Cells[i].Count; i++)
     {
          //do stuff to each cell going across the line
          //use the text property and System.Convert to do the math you need to do.
     }
}
0
 
LVL 9

Accepted Solution

by:
bele04 earned 500 total points
Comment Utility
The datalist's ItemDataBound event is fired after an item in a datalist is binded to a certain data but before it is displayed/rendered to the page so you can still make changes to the datalist prior to rendering it.  So let's say you have 3 items in your datalist, the ItemDataBound event therefore is fired 3 times, once for each item.

So if you want to change a label in your datalist then you do not need to iterate through the entire list of items in the datalist since not all of them has been databounded yet.  You can do something like this instead:

private void DataList1_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
    Label label1 = (Label) e.Item.FindControl("<name of your label");
    //calculating code here...
}

If you want to get other items in your datalist then you can just use the FindControl method to get an instance of the control object and just type cast it to the proper webcontrol type.

hope that helps...
-bele04-
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

728 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

12 Experts available now in Live!

Get 1:1 Help Now