Link to home
Start Free TrialLog in
Avatar of animated405
animated405

asked on

How to reference the array "column" into a datalist??


Hello, new to .NET. I'm using a class to populate an array from a dataset (I know a dataset is an array, but I have to do some other stuff) and I can't figure out how to set which "column" from the array to display when I bind it to a DataList. Since my ArrayList doesn't have column names, I can't use this syntax:

# DataBinder.Eval(Container.DataItem, "column1")

And I thought I could do this:

# DataBinder.Eval(Container.DataItem, "Item[2]")
or
# DataBinder.Eval(Container.DataItem, Item[2])

But that doesn't work either. If I do this: # Container.DataItem...it spits out the whole array, which looks like:

id, my name, my company

How do you reference a specific ordinal out of the arraylist in the datalist?

This is how I'm building my array, perhaps something is wrong here:

public void Add(DataRow dr)
{
   _ID = IncrementID();
   _RecipientName = dr["Recipient Name"].ToString();
   _Company = dr["Company"].ToString();
   Info.Add(string.Concat(S_ID, ", ", RecipientName, ", ", Company));
}

thanks in advance!!
Avatar of Thogek
Thogek
Flag of United States of America image

I'm guessing that Info is your System.Collections.ArrayList instance...

    Info.Add(string.Concat(S_ID, ", ", RecipientName, ", ", Company));

This is concatenating your S_ID, RecipientName, and Company strings into one string, and adding that string as a member of the Info ArrayList.  (In which case the "id, my name, my company" output sounds right.)  Is that what you meant to do?
If you need to keep track of your columns separately, then maybe you need three separate ArrayLists?

    InfoID.Add(_ID);
    InfoRecipient.Add(_RecipientName );
    InfoCompany.Add(_Company);

Then bind to whichever one you need at the time.
Avatar of animated405
animated405

ASKER


thanks for the reply. I don't want to have more than one array. What I was doing originally was just looping through the datarows and adding each one to an array and instanciating a class each time through as well. so I had this:

arArray = new ArrayList();
foreach(DataRow dr in myDataSet.Tables[0].Rows)
{
MyObject myO = new MyObject();
myO.RecipientName = dr["Recipient Name"].ToString();
myO.Company = dr["Company"].ToString();
arArray.Add(myO);
}

this worked fine, I could bind that to a grid as I would expect. So, I thought to make that code cleaner I could just build the array at the same time as I instantiate by calling the class method I added which I will list again:

public void Add(DataRow dr)
{
   _ID = IncrementID();
   _RecipientName = dr["Recipient Name"].ToString();
   _Company = dr["Company"].ToString();
   Info.Add(string.Concat(S_ID, ", ", RecipientName, ", ", Company));
}

But, your right, I'm basically just getting a comma delimited one dimensional array here aren't I? How can I change this so that it will populate the array like I was doing originally. It would be nice to be able to just call MyClass.Info to return the entire array, code would be cleaner that way because I would have all that extra stuff on each page everytime I wanted do that.

thanks again
ASKER CERTIFIED SOLUTION
Avatar of Thogek
Thogek
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

I'm sorry, but I don't think I'm explaining myself well enough. I see how that works if I'm instatiating a class and adding to the array within that same page. But what I'm trying to do is, while looping through the dataset, instantiate the class, call the classes Add method by passing in the datarow, which in turn inside that class will add another object to a multidemisional array that I can reference by calling that from the class.

So, if in the class my multidimesional array called thisArr, then I would want to be able to call it from the other page and bind it to a grid like so

DataList.Source = MyClass.thisArr;

I guess the problem I'm having is just how to you add to a multidimensional array? thanks again

hey Thogek, thanks for the replies....I managed to get the array working as I intended, however maybe you know the answer to other part of my original question....

now that I have the array, then I bind it to the datalist, what syntax is used in the aspx page in the itemtemplate for accessing the "column" from the array. Since there are no column names with the array, I figured you could refernce it my [0] or something but that doesn't work...

thanks