我似乎無法弄清楚這裏發生了什麼......我有一個dataGridView,在任何給定時間不超過500行,但通常在200或300左右。我遍歷網格並根據用戶交互設置按鈕文字和顏色。例如:DataGridView SLOW當給單元格賦值時
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
DataGridViewButtonColumn btn;
ContextMenuStrip ctxtStartStop;
public Form1()
{
InitializeComponent();
formatGrid();
populateGrid();
ctxtStartStop = new ContextMenuStrip();
ctxtStartStop.Items.Add("START ALL");
ctxtStartStop.Items.Add("STOP ALL");
ctxtStartStop.ItemClicked += new ToolStripItemClickedEventHandler(ctxtMenuStrip_ItemClicked);
}
private void formatGrid()
{
btn = new DataGridViewButtonColumn();
btn.Text = "START";
btn.Name = "colStartStop";
btn.HeaderText = "Start/Stop";
btn.DefaultCellStyle.BackColor = Color.LightGreen;
btn.DefaultCellStyle.ForeColor = Color.Black;
btn.ReadOnly = false;
btn.UseColumnTextForButtonValue = false;
btn.FlatStyle = FlatStyle.Standard;
btn.AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells;
gridDisplay.AutoGenerateColumns = false;
gridDisplay.AllowUserToAddRows = false;
gridDisplay.RowHeadersVisible = false;
gridDisplay.Columns.Add(new DataGridViewTextBoxColumn()
{
Name = "colSymbol",
HeaderText = "Symbols",
ReadOnly = true,
AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill,
MinimumWidth = 50
});
gridDisplay.Columns.Add(btn);
gridDisplay.MouseClick += new MouseEventHandler(gridDisplay_MouseClick);
}
private void populateGrid()
{
for (int i = 0; i < 500; i++)
{
gridDisplay.Rows.Add("XYZ", "START");
}
}
private void gridDisplay_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
return;
int rowPosition = gridDisplay.HitTest(e.X, e.Y).RowIndex;
int colPosition = gridDisplay.HitTest(e.X, e.Y).ColumnIndex;
if (rowPosition == -1 && colPosition == 1)
{
ctxtStartStop.Show(gridDisplay.PointToScreen(e.Location));
}
}
private void ctxtMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
if (e.ClickedItem.Text == "START ALL")
{
ctxtStartStop.Hide();
startAll();
}
else if (e.ClickedItem.Text == "STOP ALL")
{
ctxtStartStop.Hide();
stopAll();
}
}
private void startAll()
{
string action = string.Empty;
int idx = 1;
for (int i = 0; i < gridDisplay.Rows.Count; i++)
{
var btnCell = gridDisplay.Rows[i].Cells[idx];
action = (string)btnCell.Value;
if (action == "START")
{
btnCell.Value = "STOP";
gridDisplay.Rows[i].Cells["colStartStop"].Style.BackColor = Color.Red;
gridDisplay.Rows[i].Cells["colStartStop"].Style.ForeColor = Color.White;
}
}
}
private void stopAll()
{
string action = string.Empty;
int idx = 1;
for (int i = 0; i < gridDisplay.Rows.Count; i++)
{
var btnCell = gridDisplay.Rows[i].Cells[idx];
action = (string)btnCell.Value;
if (action == "STOP")
{
btnCell.Value = "START";
gridDisplay.Rows[i].Cells["colStartStop"].Style.BackColor = Color.LightGreen;
gridDisplay.Rows[i].Cells["colStartStop"].Style.ForeColor = Color.Black;
}
}
}
}
}
有趣的是,設置顏色工作正常,但是當我設置值運行速度非常慢。
有人可以請解釋我在做什麼錯在這裏。
謝謝 -DA
你可以請一些更好的代碼佈局/格式的代碼,因爲這是不友好的閱讀或解決 – JohnnBlade
你不能讓一個'DataSource'與相關的值(在源中進行更改)並綁定它們,處理'CellFormatting'事件中的顏色 – V4Vendetta
我發現這篇文章是因爲我也有同樣的問題。我從MyClass的內存字典中爲每個DataGridView添加了400行,每行有十幾個單元格,耗時0.15秒。但是,如果我添加2行代碼,如「dgv_mainlist.Rows [r_index] .Cells [3] .Value =」「;」它需要1秒鐘來運行整個事情。通過爲每行2個單元添加值,將時間延長將近5至6倍。 - 3年,仍然沒有迴應這個職位...我想我不會屏住呼吸。看起來像是控制中的問題。 –