Solved

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

Posted on 2004-08-27
7
200 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

685 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