• <i id='1oHNM'><tr id='1oHNM'><dt id='1oHNM'><q id='1oHNM'><span id='1oHNM'><b id='1oHNM'><form id='1oHNM'><ins id='1oHNM'></ins><ul id='1oHNM'></ul><sub id='1oHNM'></sub></form><legend id='1oHNM'></legend><bdo id='1oHNM'><pre id='1oHNM'><center id='1oHNM'></center></pre></bdo></b><th id='1oHNM'></th></span></q></dt></tr></i><div id='1oHNM'><tfoot id='1oHNM'></tfoot><dl id='1oHNM'><fieldset id='1oHNM'></fieldset></dl></div>
      • <bdo id='1oHNM'></bdo><ul id='1oHNM'></ul>

      <tfoot id='1oHNM'></tfoot>
        <legend id='1oHNM'><style id='1oHNM'><dir id='1oHNM'><q id='1oHNM'></q></dir></style></legend>

      1. <small id='1oHNM'></small><noframes id='1oHNM'>

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

        C# amp; SQL : how to update table without refreshing DataGridView?(C# amp;SQL:如何在不刷新 DataGridView 的情况下更新表?)

          <tbody id='mwIKU'></tbody>
          <bdo id='mwIKU'></bdo><ul id='mwIKU'></ul>

              <i id='mwIKU'><tr id='mwIKU'><dt id='mwIKU'><q id='mwIKU'><span id='mwIKU'><b id='mwIKU'><form id='mwIKU'><ins id='mwIKU'></ins><ul id='mwIKU'></ul><sub id='mwIKU'></sub></form><legend id='mwIKU'></legend><bdo id='mwIKU'><pre id='mwIKU'><center id='mwIKU'></center></pre></bdo></b><th id='mwIKU'></th></span></q></dt></tr></i><div id='mwIKU'><tfoot id='mwIKU'></tfoot><dl id='mwIKU'><fieldset id='mwIKU'></fieldset></dl></div>

              <small id='mwIKU'></small><noframes id='mwIKU'>

                <legend id='mwIKU'><style id='mwIKU'><dir id='mwIKU'><q id='mwIKU'></q></dir></style></legend>
                • <tfoot id='mwIKU'></tfoot>
                  本文介绍了C# &amp;SQL:如何在不刷新 DataGridView 的情况下更新表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我总是用

                  DataGridView.DataSource = (DataTable)tbl; 
                  

                  但是这种方法完全刷新了 Datagridview,比如 selectedrows、滚动条位置、backColor 等.我只想从 SQL 更新单元格数据,而没有完整的 datagridview 刷新

                  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

                  例如 uTorrent 有一个类似 datagridview 的表格,在某些单元格中 x KB/s 的值总是在刷新,但 torrentdatagrid 是静态的.没有滚动移动,没有选择消失等.

                  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.

                  你能帮我做这件事吗?

                  对不起,我的英语不好.谢谢.

                  I'm sorry for my bad English. Thanks.

                  推荐答案

                  如果你的表有一个主键,并且你只想更新现有的/添加新的行,你可以使用 DataTable.Merge 方法 像这样:

                  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:

                  最初

                  dataGridView.DataSource = initial_data_table;
                  

                  更新

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

                  更新: 上面的方法是最简单的,但正如评论中提到的,由于 Merge 方法优化会在操作并以 ListChangedType.Reset 最后引发 ListChanged 事件.所以,这是一个简单而有效的方法,它基于 DataTable.LoadDataRow 方法:

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

                  还有一个样本证明:

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

                  这篇关于C# &amp;SQL:如何在不刷新 DataGridView 的情况下更新表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Adding and removing users from Active Directory groups in .NET(在 .NET 中的 Active Directory 组中添加和删除用户)
                  set equality in linq(在 linq 中设置相等)
                  HashSet conversion to List(HashSet 转换为 List)
                  How to set timeout for webBrowser navigate event(如何为 webBrowser 导航事件设置超时)
                  Test whether two IEnumerablelt;Tgt; have the same values with the same frequencies(测试两个IEnumerablelt;Tgt;具有相同频率的相同值)
                  How do you determine if two HashSets are equal (by value, not by reference)?(您如何确定两个 HashSet 是否相等(按值,而不是按引用)?)
                  <legend id='Av4bC'><style id='Av4bC'><dir id='Av4bC'><q id='Av4bC'></q></dir></style></legend>
                    <tbody id='Av4bC'></tbody>
                    <tfoot id='Av4bC'></tfoot>

                    <small id='Av4bC'></small><noframes id='Av4bC'>

                    <i id='Av4bC'><tr id='Av4bC'><dt id='Av4bC'><q id='Av4bC'><span id='Av4bC'><b id='Av4bC'><form id='Av4bC'><ins id='Av4bC'></ins><ul id='Av4bC'></ul><sub id='Av4bC'></sub></form><legend id='Av4bC'></legend><bdo id='Av4bC'><pre id='Av4bC'><center id='Av4bC'></center></pre></bdo></b><th id='Av4bC'></th></span></q></dt></tr></i><div id='Av4bC'><tfoot id='Av4bC'></tfoot><dl id='Av4bC'><fieldset id='Av4bC'></fieldset></dl></div>

                      • <bdo id='Av4bC'></bdo><ul id='Av4bC'></ul>