简体   繁体   English

C#和SQL:如何在不刷新DataGridView的情况下更新表?

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

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 但是此方法完全引用了Datagridview,例如selectedrows,滚动条位置,backColor等。我只想从SQL中更新单元数据,而无需完整的datagridview刷新。

For Example uTorrent has a table like datagridview, and in some cells x KB/s values always refresing but torrentdatagrid is static. 例如,uTorrent有一个像datagridview的表,在某些单元格中,x KB / s的值始终是引用的,但是torrentdatagrid是静态的。 there is No scroll moving, no selection dissapearing etc. 没有滚动,没有选择消失等。

Can you help me to do this? 你能帮我做到吗?

I'm sorry for my bad English. 对不起,我的英语不好。 Thanks. 谢谢。

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: 如果表具有主键,并且您只想更新现有/添加新行,则可以使用DataTable.Merge方法,如下所示:

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. 更新:上面的方法是最简单的方法,但是如注释中所述,由于Merge方法的优化,该方法具有副作用,该方法可以抑制操作期间的更改通知,并在最后以ListChangedType.Reset引发ListChanged事件。 So, here is a simple and effective method that does the trick, based on DataTable.LoadDataRow Method : 因此,这是一个简单有效的方法,该方法基于DataTable.LoadDataRow方法

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

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. 第一列是ID主键列。 I m Updating by a timer and real time passing data to my datagridview. 我正在更新计时器,并将数据实时传递到我的datagridview。 thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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