chrisszz
chrisszz

Reputation: 75

Insert values into listview from multiple arrays C#

So I have 4 arrays, and they contain first names, last names, usernames and access type (read, read/write, none). I need to insert the values from these arrays into a listview with 34 columns, the first 3 corresponding to the first 3 arrays and the rest need to contain the access type for these users (the column headers being the folder names in which they have access or not). My problem lies with the access type array. How can I insert 31 values on each row into the listview from the access type array? The first 31 values from the access array correspond to the first username, the next 31 to the second username and so on. Till now I've inserted the first 3 arrays like this:

for (int i = 0; i < rows; i++)
{
    list.Items.Add(new ListViewItem(new string[] { fname[i], lname[i], uname[i] }));   
}

This adds all the users.

Don't know how to insert the other values...

Upvotes: 0

Views: 1688

Answers (4)

Anirudh Agarwal
Anirudh Agarwal

Reputation: 697

There is another sample solution to the question above.

String[] firtNames = new String[] { "abc", "def", "ghi", "jkl" };
String[] lastNames = new String[] { "mno", "pqr", "stu", "vwx" };
String[] userNames = new String[] { "gb", "aa", "ss", "am" };
String[] actions = new String[] { "Get", "Set", "Submit", "Get", "Set", "Submit","Get","Set", "Submit", "Get", "Set", "Submit" };

Int32 counter = 31;
 var p = firtNames.Select((col, index) => new { FirstName = col, LastName = lastNames[index], UserName = userNames [index] , a1 = actions[index * counter], a2 = actions[index * counter + 1],..., a31 = actions[index * counter + 30] }).ToList();
        ListView1.DataSource = p;
        ListView1.DataBind();

Bind the grid view column with the name you mentioned here as FirstName, LastName.

Upvotes: 0

Grant Winney
Grant Winney

Reputation: 66449

Assuming your last array was called accesslist, and assuming it has exactly 31 values for every user, this would work, although it'll get pretty long.

for (int i = 0; i < rows; i++)
{
    list.Items.Add(new ListViewItem(new string[] { fname[i], lname[i], uname[i],
        accesslist[0 + (i * 31)], accesslist[1 + (i * 31)], ... , accesslist[30 + (i * 31)] }));   
}

Instead, create a string array inside the loop, and populate that by iterating through accesslist:

for (int i = 0; i < rows; i++)
{
    var items = new string[34];

    items[0] = fname[i];
    items[1] = lname[i];
    items[2] = uname[i];

    for (int j = 0; j < 31; j++)
        items[j + 3] = accesslist[j + (i * 31)];

    list.Items.Add(new ListViewItem(items));
}

Upvotes: 1

Anirudh Agarwal
Anirudh Agarwal

Reputation: 697

convert your array of "access type" into a list. Make a class having those 31 values and make list of that class. then your work will be fast4er and also reduced.

public class accessType
{

public String prop1{get;set;}
public String prop2{get;set;}
public String prop3{get;set;}
.
.
.
public String prop31{get;set;}

}

Now make a list of class:

List<accessType> accessType=new List<accessType>();

then here you bind your list view:

Int32 i=0;
 accessType.ForEach(x=>
                lstViewTest.Items.Add(new ListViewItem{fname[i],lname[i],uName[i],x.prop1,x.prop2,x.prop3,....,x.prop31}); i++ 
                    );

Upvotes: 1

ajg
ajg

Reputation: 1753

something like.....

for (int i = 0; i < rows; i++)
{
    list.Items.Add(new ListViewItem(new string[] { fname[i], lname[i], uname[i], accesstype[i * 31], accesstype[i * 31 + 1], etc, etc, ..., accesstype[i * 31 + 30] }));   
}

Upvotes: 0

Related Questions