Solved

Datalist itemdatabound problem c#

Posted on 2006-07-10
2
3,018 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
ID: 17076345
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
ID: 17078360
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

830 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