C# & SQL : how to update table without refreshing DataGridView?

孤者浪人 提交于 2019-12-23 02:37:00

问题


I always use

DataGridView.DataSource = (DataTable)tbl; 

But this method is completely refresing the Datagridview something like selectedrows, scrollbar position, backColor etc.. I only want to update cell data from SQL witouth full datagridview refresh

For Example uTorrent has a table like datagridview, and in some cells x KB/s values always refresing but torrentdatagrid is static. there is No scroll moving, no selection dissapearing etc.

Can you help me to do this?

I'm sorry for my bad English. Thanks.


回答1:


If your table has a primary key, and you want only to update existing/add new rows, you can use DataTable.Merge Method like this:

Initially

dataGridView.DataSource = initial_data_table;

Update

((DataTable)dataGridView.DataSource).Merge(new_data_table);

UPDATE: The method above is the simplest, but as mentioned in the comments, it has side effects due to the Merge method optimizations which suppress the change notifications during the operation and raising ListChanged event with ListChangedType.Reset at the end. So, here is a simple and effective method that does the trick, based on DataTable.LoadDataRow Method:

foreach (var dataRow in newTable.AsEnumerable())
    table.LoadDataRow(dataRow.ItemArray, LoadOption.OverwriteChanges);

And a sample proving that:

using System;
using System.Data;
using System.Linq;
using System.Windows.Forms;

namespace Samples
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            var form = new Form();
            var dg = new DataGridView { Dock = DockStyle.Fill, Parent = form };
            var data = GetData();
            dg.DataSource = data;
            var updateTimer = new Timer { Interval = 200, Enabled = true };
            updateTimer.Tick += (sender, e) =>
            {
                foreach (var dr in GetData().AsEnumerable())
                    data.LoadDataRow(dr.ItemArray, LoadOption.OverwriteChanges);
            };
            Application.Run(form);
        }
        static DataTable GetData()
        {
            var dt = new DataTable();
            dt.Columns.Add("Id", typeof(int));
            dt.Columns.Add("Name");
            dt.Columns.Add("Score", typeof(int));
            dt.PrimaryKey = new[] { dt.Columns["Id"] };
            var random = new Random();
            for (int i = 1; i <= 1000; i++)
                dt.Rows.Add(i, "Player #" + i, random.Next(1, 100000));
            return dt;
        }
    }
}



回答2:


Maybe I slog to explain problem. I m sorry. but, this code solved my problem,

            for (int i = 0; i < dg.Rows.Count; i++)
            {
                for (int j = 1; j < dg.Columns.Count ; j++)
                {
                    dg.Rows[i].Cells[j].Value = NewDataTable.Select("ID=" + dg.Rows[i].Cells[0].Value)[0][j].ToString();
                }
            }

first column is ID Primary key column. I m Updating by a timer and real time passing data to my datagridview. thanks



来源:https://stackoverflow.com/questions/33163471/c-sharp-sql-how-to-update-table-without-refreshing-datagridview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!