0

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...

2
  • Please give sample of arrays. It's not clear how you keep data for different users in access type array Commented Apr 28, 2014 at 11:23
  • @Sergey Berezovskiy all the arrays are populated from a database and the data is taken according to the username. So what I am saying is that the order of the values from the access array corresponds to the order of the usernames from the username array in blocks of 31 values. Commented Apr 28, 2014 at 11:37

4 Answers 4

1

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

Ok, so I'm trying to use your second method and you forgot to loop through each item when inserting it to the listview. Although I've done that, all the values are inserted on the first column only. How can I insert the values on the corresponding column in the listview?
but the thing is we should avoid foreach loop... The implementation has no flaw in it but we should try to avoid loops..
1

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++ 
                    );

Comments

0

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] }));   
}

Comments

0

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.

1 Comment

The thing I was trying to avoid is to manually input all those 31 items because I've set that number into a variable that might change in the future, so I needed to loop through them instead. But thanks for the answer anyway. Much appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.