在 datagridview 上加载数据的更快方法?
Posted
技术标签:
【中文标题】在 datagridview 上加载数据的更快方法?【英文标题】:Faster way to load data on datagridview? 【发布时间】:2020-03-21 10:23:17 【问题描述】:我想更快地加载数据,因为完成所有这些过程大约需要 2 分钟。我想要发生的是更快地加载它,有没有办法做到这一点?
每个 datagridview 我有将近 1k 数据,数据库处理大约需要 1-2 秒。
下面是我的代码
试试 暗循环 As Integer = txt_Cycle.Text 昏暗 cyclelast, cycle2last, cycle3last As Integer 暗淡 rt = txt_Route.Text cyclelast = 周期 - 1 cycle2last = cyclelast - 1 cycle3last = cycle2last - 1 '2个月 使用命令作为新的 SqlCommand() command.Connection = conn 将参数名称调暗为新列表(字符串)(dt_data.RowCount - 2) For i As Integer = 0 To dt_data.RowCount - 3 Dim parameterName As String = "@meter_num_" & i 暗淡meter_number As String = dt_data.Rows(i).Cells(3).Value command.Parameters.AddWithValue(parameterName,meter_number) 参数名称。添加(参数名称) 下一个 command.CommandText = String.Format("SELECT * FROM customer WHERE cycle = @cycle2last and Meter_num IN (SELECT Meter_num FROM customer WHERE cycle = @cyclelast and rt = @rt and meter_num IN (0)) AND Meter_num IN (SELECT Meter_num FROM customer WHERE cycle = @cycle AND rt = @rt AND Meter_num IN (0)) ORDER BY Client_Name ASC,meter_num ASC, MtrType ASC", String.Join(",", parameterNames)) command.Parameters.AddWithValue("@cycle2last", cycle2last) command.Parameters.AddWithValue("@cyclelast", cyclelast) command.Parameters.AddWithValue("@cycle", 循环) command.Parameters.AddWithValue("@rt", rt) 将 da 作为新的 SqlDataAdapter(命令) 暗淡为新数据集 da.Fill(ds, "客户") Compare_Reading.dt_last2month.DataSource = ds.Tables(0) 结束使用 '上个月 使用命令作为新的 SqlCommand() command.Connection = conn 将参数名称调暗为新列表(字符串)(dt_data.RowCount - 2) For i As Integer = 0 To dt_data.RowCount - 3 Dim parameterName As String = "@meter_num_" & i 暗淡meter_number As String = dt_data.Rows(i).Cells(3).Value command.Parameters.AddWithValue(parameterName,meter_number) 参数名称。添加(参数名称) 下一个 command.CommandText = String.Format("SELECT * FROM customer WHERE cycle = @cyclelast and meter_num IN (SELECT meter_num FROM customer WHERE cycle = @cycle2last and rt = @rt and meter_num IN (0)) AND Meter_num IN (SELECT Meter_num FROM customer WHERE cycle = @cycle AND rt = @rt AND Meter_num IN (0)) ORDER BY Client_Name ASC,meter_num ASC, MtrType ASC", String.Join(",", parameterNames)) command.Parameters.AddWithValue("@cyclelast", cyclelast) command.Parameters.AddWithValue("@cycle2last", cycle2last) command.Parameters.AddWithValue("@cycle", 循环) command.Parameters.AddWithValue("@rt", rt) 将 da 作为新的 SqlDataAdapter(命令) 暗淡为新数据集 da.Fill(ds, "客户") Compare_Reading.dt_lastmonth.DataSource = ds.Tables(0) 结束使用 '本月 使用命令作为新的 SqlCommand() command.Connection = conn 将参数名称调暗为新列表(字符串)(dt_data.RowCount - 2) For i As Integer = 0 To dt_data.RowCount - 3 Dim parameterName As String = "@meter_num_" & i 暗淡meter_number As String = dt_data.Rows(i).Cells(3).Value command.Parameters.AddWithValue(parameterName,meter_number) 参数名称。添加(参数名称) 下一个 command.CommandText = String.Format("SELECT * FROM customer WHERE cycle = @cycle AND Meter_num IN (SELECT Meter_num FROM customer WHERE cycle = @cyclelast AND rt = @rt AND Meter_num IN (0)) AND Meter_num IN (SELECT Meter_num FROM customer WHERE cycle = @cycle2last AND rt = @rt AND Meter_num IN (0)) ORDER BY Client_Name ASC,meter_num ASC, MtrType ASC", String.Join(",", parameterNames)) command.Parameters.AddWithValue("@cycle", 循环) command.Parameters.AddWithValue("@cyclelast", cyclelast) command.Parameters.AddWithValue("@cycle2last", cycle2last) command.Parameters.AddWithValue("@rt", rt) 将 da 作为新的 SqlDataAdapter(命令) 暗淡为新数据集 da.Fill(ds, "客户") Compare_Reading.dt_curmonth.DataSource = ds.Tables(0) 结束使用 我.Hide() Compare_Reading.computation() Compare_Reading.txt_Route.Text = txt_Route.Text Compare_Reading.txt_billday.Text = txt_BillDay.Text Compare_Reading.txt_itn.Text = txt_itn.Text Compare_Reading.nup_cycle.Value = txt_Cycle.Text '标题名称 Compare_Reading.headername() Compare_Reading.Show() 捕获 ex As SqlException MsgBox(例如消息,MsgBoxStyle.Critical,“SQL 错误”) 抓住前任作为例外 MsgBox(例如Message,MsgBoxStyle.Critical,“一般错误”) '结束尝试 '抓住前任例外 'MessageBox.Show(String.Format("Error: 0", ex.Message), "Error", MessageBoxButtons.OKCancel, MessageBoxIcon.Error) 结束尝试
这是计算类
公共子计算() 'Showmonth GridView For i As Integer = 0 To dt_curmonth.RowCount - 1 dt_showmonth.Rows.Add(dt_curmonth.Rows(i).Cells(0).Value) dt_showmonth.Rows(i).Cells(1).Value = dt_curmonth.Rows(i).Cells(4).Value '行程 dt_showmonth.Rows(i).Cells(2).Value = dt_curmonth.Rows(i).Cells(11).Value 'mtr num dt_showmonth.Rows(i).Cells(3).Value = dt_last2month.Rows(i).Cells(14).Value ' dec dt_showmonth.Rows(i).Cells(4).Value = dt_lastmonth.Rows(i).Cells(14).Value ' jan dt_showmonth.Rows(i).Cells(5).Value = dt_curmonth.Rows(i).Cells(14).Value ' 2 月 'dt_showmonth.Rows(i).Cells(10).Value = dt_curmonth.Rows(i).Cells(11).Value 'mtr num dt_showmonth.Rows(i).Cells(10).Value = dt_curmonth.Rows(i).Cells(12).Value 'mtr 类型 dt_showmonth.Rows(i).Cells(11).Value = dt_last2month.Rows(i).Cells(18).Value 'mtr ff dt_showmonth.Rows(i).Cells(12).Value = dt_lastmonth.Rows(i).Cells(18).Value 'mtr ff dt_showmonth.Rows(i).Cells(13).Value = dt_curmonth.Rows(i).Cells(18).Value 'mtr ff 如果 dt_last2month.Rows(i).Cells(14).Value = 0 那么 'last2 dt_showmonth.Rows(i).Cells(3).Value = dt_lastmonth.Rows(i).Cells(13).Value 万一 如果 dt_lastmonth.Rows(i).Cells(14).Value = 0 那么 'last dt_showmonth.Rows(i).Cells(4).Value = dt_curmonth.Rows(i).Cells(13).Value 万一 Dim month2nd As Integer = dt_showmonth.Rows(i).Cells(3).Value Dim month1st As Integer = dt_showmonth.Rows(i).Cells(4).Value Dim curmonth As Integer = dt_showmonth.Rows(i).Cells(5).Value Dim high20 As Double 将差异变暗为整数 将 diff1 调暗为整数 Dim per20 As Double Dim less20 As Double 差异 = 第 1 个月 - 第 2 个月 diff1 = curmonth - month1st per20 = 差异 * 0.3 high20 = Math.Round((diff + per20), 2) less20 = Math.Round((diff - per20), 2) dt_showmonth.Rows(i).Cells(6).Value = diff dt_showmonth.Rows(i).Cells(7).Value = diff1 dt_showmonth.Rows(i).Cells(8).Value = less20 dt_showmonth.Rows(i).Cells(9).Value = high20 下一个 结束子
【问题讨论】:
你要拉回多少行?数据库检索需要多长时间?本地计算需要多长时间?将页面数据传输到客户端需要多长时间? 整个过程大约需要 2 分钟 我知道,但是您必须对每个部分进行计时才能知道哪个部分需要改进。因此,我们需要知道所有这些单独的时间。 您可以使用 3 个 select 语句进行 1 个 db 调用,并以更少的代码在一个数据集中加载 3 个表 使用服务器端分页实现显示数据。没有人会对在屏幕上一次看到 1000 行感兴趣。 【参考方案1】:缓慢是由于在计算过程中更新 UI 的 datagridview。您不应该为数据绑定网格编辑 DataGridView 的 .Cells 属性。除此之外,单击列标题以按该列排序会造成严重破坏。
首先在内存中进行所有计算,并在所有计算完成后在最后设置 DataSource。这样 DataGridView 只会渲染一次屏幕。
【讨论】:
以上是关于在 datagridview 上加载数据的更快方法?的主要内容,如果未能解决你的问题,请参考以下文章
ASP中 devexpress 的gridview如何让数据加载时就排序
将数据从父表和子表加载到 DataGridView 的最简单方法,可以进行排序
如何在在WinFrom的DataGridView中做到数据持续动态加载而不卡死
datagridView 加载数据中的数据,数据显示很慢,一行一行显示出来 解决方案