Link to home
Start Free TrialLog in
Avatar of CatsSolutions
CatsSolutions

asked on

Populating a gridview with array or strings?

I want to be able create a list of online users, I have the code below to get the username, firstname and lastname for each online user but I want to get that into a gridview. How can I get the information (currently in strings or an array) into a gridviw?

MembershipUserCollection users = Membership.GetAllUsers();
foreach (MembershipUser user in users)
{
  if (user.IsOnline)
  {
     //in here I have code to get information for Username, First Name and Last Name.
  }
}
GridView1.AutoGenerateColumns = true;
GridView1.DataSource = userList;
GridView1.DataBind();
Avatar of Velio
Velio
Flag of South Africa image

hi,

the code you have seems fine, what is the markup for your gridview?

have you checked to see if userList actually has any elements?
Avatar of CatsSolutions
CatsSolutions

ASKER

I can create an array called userList and populate it but then when I try to bind the datagrid I get an error on saying:

Array was not a one-dimensional array.

This is the code I was using for the array:

MembershipUserCollection users = Membership.GetAllUsers();
string[,] onlinelist = new string[Convert.ToInt32(Membership.GetNumberOfUsersOnline()),3];
        int usercount = 0;
        foreach (MembershipUser useronline in users)
        {
            if (useronline.IsOnline)
            {
                //userlist.Add(useronline.UserName);
                onlinelist[usercount, 0] = useronline.UserName.ToString();
                onlinelist[usercount, 1] = FirstName;
                onlinelist[usercount, 2] = LastName;
                usercount++;
            }
        }
        GridView1.AutoGenerateColumns = true;
        GridView1.DataSource = onlinelist;
        GridView1.DataBind();

Open in new window

ah i see, it seems gridview can't handle multi-dimensional arrays.

You'll have to use another type of data source.

you could either bind an array/collection/list of objects (ie Create a class User which contains Username. FirstName, LastName properties to represent each user)

or construct a DataTable with Username, FirstName and LastName columns and add a DataRow for each user.
something similar to the code below.
you can either use an ObjectDataSource or just bind the result of the static method directly...


    public class MyUser
    {
        private string username;
        private string firstName;
        private string lastName;
 
        public MyUser(string username, string firstName, string lastName)
        {
            this.username = username;
            this.firstName = firstName;
            this.lastName = lastName;
        }
 
        public static User[] GetOnlineMembers()
        {
            List<User> list = new List<User>();
            foreach (MembershipUser user in Membership.GetAllUsers())
            {
                if (user.IsOnline)
                {
                    list.Add(new User(user.UserName, /*first name*/"", /*surname*/""));
                }
            }
            return list.ToArray();
        }
 
        public string Username
        {
            get
            {
                return username;
            }
            set
            {
                username = value;
            }
        }
 
        public string FirstName
        {
            get
            {
                return firstName;
            }
            set
            {
                firstName = value;
            }
 
        }
 
        public string LastName
        {
            get
            {
                return lastName;
            }
            set
            {
                lastName = value;
            }
        }
    }

Open in new window

Ok s how would I construct the data table and add a datarow for each user?
just a correction, i changed from user to myuser halfway through, so the method is a bit wrong... anyway, i'll stop posting until your next comment :)

        public static MyUser[] GetOnlineMembers()
        {
            List<MyUser> list = new List<MyUser>();
            foreach (MembershipUser user in Membership.GetAllUsers())
            {
                if (user.IsOnline)
                {
                    list.Add(new MyUser(user.UserName, /*first name*/"", /*surname*/""));
                }
            }
            return list.ToArray();
        }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Velio
Velio
Flag of South Africa 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