Solved

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

Posted on 2004-08-27
7
195 Views
Last Modified: 2010-04-15

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!!
0
Comment
Question by:animated405
  • 3
  • 3
7 Comments
 
LVL 15

Expert Comment

by:Thogek
ID: 11915073
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?
0
 
LVL 15

Expert Comment

by:Thogek
ID: 11915107
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.
0
 

Author Comment

by:animated405
ID: 11915324

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
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 15

Accepted Solution

by:
Thogek earned 500 total points
ID: 11916616
I'm not sure I understand the full question, but to attempt to meld your two methods together...

    public void Add(DataRow dr)
    {
        _ID = IncrementID();
        _RecipientName = dr["Recipient Name"].ToString();
        _Company = dr["Company"].ToString();

        MyObject myO = new MyObject();
        myO.ID = _ID;  // Not sure if this is how your IDs generate
        myO.RecipientName = _RecipientName;
        myO.Company = _Company;

        _alInfo.Add(myO);
    }

    public ArrayList Info
    {
        get { return _alInfo; }
    }
0
 

Author Comment

by:animated405
ID: 11917102

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
0
 

Author Comment

by:animated405
ID: 11918084

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
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

20 Experts available now in Live!

Get 1:1 Help Now