Link to home
Start Free TrialLog in
Avatar of Aida Nazari
Aida Nazari

asked on

Ramdomly choose 30 rows from datagrideview

Hi experts
I have two datagridview and one button.my data has been shown in the first datagridview from sql. I want some code to do bellow but i don't know how can I write it:
When I clicking on the button,30 rows of first datagrideview choose and transfer to second one
Please help me
Avatar of Nathan Riley
Nathan Riley
Flag of United States of America image

Easiest way would be to use an already built plugin for this, one of my favorites is:
https://datatables.net/

You feed the query data to it and it will paginate, allow search, easy sorting and more.
You can do that either through server side or JavaScript.
C#:
when you fetch the data from your data source, store the fetched data [which you might need to transfer] in some generic collection and on click event, just clear the first grid and bind the second one to the data you stored.

Java script:
This is actually more convenient especially if you are using some framework [JQuery,angular...]. All what you have to do is, on click iterate through the first grid, get the data you need and store them in an array of objects if you like. the rest is just cleaning the first grid and append the new rows to the second grid.
Avatar of Aida Nazari
Aida Nazari

ASKER

Hello,
I read both of your answers and I appreciate you but I can’t write my code with your guides. I wrote it as follows. It works well but it has a mistake.
For example, I want to select three random rows from "dgridPresent" to show in "dgridSample".
This code select three random rows and delete them from "dgridPresent" but only show last row in "dgridSample".
Please help me

            db.tblAddAccounts.Load();

            List<string> list3 = new List<string>();
            var idaccount = db.tblAddAccounts.ToList();
            foreach (var i in idaccount)
                list3.Add(Regex.Match(i.IDAccount, @"([2-2]|-)+").ToString());
            if (list3.Count > 0)
            {
                int num = list3.Count();
                _MaxRow = (num );
            }              



                Random rnd = new Random();
                int _MaxRowE = _MaxRow;
                int MaxNumRow = dgridSample.RowCount;
               

                //Generate 3 random numbers for showing randomly in dgridSampleInstallment
                while (MaxNumRow <= 2)
                {
                    for (int j = 1; j <= 3; j++)
                    {
                        int row = rnd.Next(1, _MaxRowE);
                        string id = dgridPresent.Rows[row].Cells["IDAccount"].Value.ToString();

                       
                        var selectaccount = (from cf in db.tblAddAccounts.Local
                                             join s in db.tblSampleAccounts.Local on cf.IDAccount equals s.IDAccount
                                             select new
                                             {
                                                 cf.IDAccount
                                             }).ToList();


                        tblSampleAccount SampleAccount = new tblSampleAccount
                        {
                            IDAccount= id,
                            IDSample = j,
                        };
                        db.tblSampleAccounts.Add(SampleAccount);


                        var selectaccount2 = (from cf in db.tblAddAccounts.Local
                                              join s in db.tblSampleAccounts.Local on cf.IDAccount equals s.IDAccount
                                              select new
                                              {
                                                  s.IDSample,
                                                  cf.IDAccount,
                                                  cf.IDCustomer,
                                                  cf.Period,
                                                  cf.Time,
                                                  cf.Amount,

                                              }).ToList();

                        dgridSampleInstallment.DataSource = selectaccount2;


                        MaxNumRow++;

                        db.tblAddAccounts.Load();
                        string number = id;
                        var account = db.tblAddAccounts.Where(c => c.IDAccount == number).FirstOrDefault();

                        db.tblAddAccounts.Remove(account);
                        db.SaveChanges();

                        dgridPresent.Refresh();

                        _MaxRowE--;


                        db.SaveChanges();

                    }
                    break;
                 }
This question needs an answer!
Become an EE member today
7 DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform.
View membership options
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.