Solved

Datalist itemdatabound problem c#

Posted on 2006-07-10
2
3,025 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

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…
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

617 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