简体   繁体   中英

Split Data of 1 dataGridView into 2 using C#

i am creating C# application in which i am loading data from database in winform dataGridView. i have timer of 10 seconds interval. i am refreshing gridView by timer. all i want is that when gridView loads data again it load new data into second grid. i want new rows in second grid.

WHAT I HAVE DONE

i am trying this by calculating gridView rows and then count new rows in dataset that loads after refreshing the data. if there are some other rows in dataset, i want these rows in new grid.

please help me or suggest me how to apply loop

for (int i = gridRows; i < dataSetRows; i++)
 {
 drForSMS["USERID"] = ds.Tables[0].Rows[0][0].ToString();
 drForSMS["NAME"] = dataSetForDefautLoad.Tables[0].Rows[0][0].ToString();
 }

I think more clear way would be to clear all rows and then bind the grid to the newly (refreshed) dataset.

When you first open the application, bind the first grid to the DataSet and the second grid to null . Keep the first dataset into a collection variable locally.

On timer event load your dataset into some collection and do the binding.

List<int> list1 = new List<int>();

            list1.Add(1);
            list1.Add(2);

            // Now this is your refresh which will load all data from first load, plus the refresh load
            List<int> list2 = new List<int>();
            list2 = list1;
            list2.Add(3);
            list2.Add(4);
            list2.Add(5);

            // Now you want the new values only in list2
            // Like we said, keep the original list1 in local variable so you can do this
            foreach (var l in list1)
            {
                if (list2.Contains(l))
                    list2.Remove(l);
            }

Then simply bind the second grid to null , then to list2 .

2. Another way to do this is: You keep the count of the first query when you run the application. For example you got 5 records. Then for the second grid you loop from 5 and above to load the second grid.

DataSet dsFirst = new DataSet(); DataSet dsNew = new DataSet();

    var rows = dsFirst.Tables[0].Rows;

    var newRows = dsNew.Tables[0].Rows;

    // Choose your collection type here...
    var forBinding = new List<object>();

    for (int i = rows.Count - 1; i < newRows.Count; i++)
    {
        forBinding.Add(newRows[i]);
    }


    dataGrid.DataSource = forBinding;
  1. An example with DataSet

    DataSet dsFirst = new DataSet(); var initialCount = dsFirst.Tables[0].Rows.Count;

      DataSet dsRefreshed = new DataSet(); for (int i = 0; i < initialCount; i++) { dsRefreshed.Tables[0].Rows.RemoveAt(i); } dg.DataSource = dsRefreshed; 

Just ask if you need to adjust this to your code.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM