Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Good day all.
Let me start by saying I am missing something obvious and I know, but cant seem to get it.
I am working on a randomization application.
I need to pull a list of active users(with USERID's) from my database, load them into a Datagridview, Well that works fine, now after that I load the USERID's into a int[], also works fine. Now my problem is that I need to loop these USERID's into a random function and populate a different Datagridview with it and the random is only supposed to select USERID's in the array.
This is how far I have gotten.


conn.Open();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < select.Length; i++)
            {
                ds = new DataSet();
                int use = Users.Next(15, Int32.Parse(select.GetValue(i).ToString()));
                if (select.Contains(use))
                {
                    sb.Append(use + "\r\n");
                    da = new SqlDataAdapter("SELECT USERID,FULLNAME FROM USERS WHERE TYPE = 'EMPLOYEE' AND ACTIVE = 'True' AND USERID ='" + use + "' ORDER BY USERID", conn);

                }                
            }
            
            build = new SqlCommandBuilder(da);
            da.Fill(ds, "USERS");
            dgSelected.DataSource = ds;
            dgSelected.DataMember = "USERS";
            conn.Close();


select is the int[]

Any help please?
Posted
Updated 23-Jul-13 3:20am
v2

1 solution

The way I do it is not to use an array but a List instead:
C#
List<int> newOrder = new List<int>();
while (users.Count > 0)
    {
    int removeIndex = rand.Next(users.Count)
    newOrder.Add(users[removeIndex]);
    users.RemoveAt(removeIndex);
    }
What this does is select an item at random, and then remove it so it can't be repeated.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900