여기에 무슨 일이 일어나는지 알 수없는 것처럼 보입니다. 주어진 시간에 500 행 이상이 아닌 보통 200 또는 300의 dataGridView가 있습니다. 그리드를 반복합니다. 사용자 상호 작용에 따라 버튼 텍스트와 색상을 설정할 수 있습니다. 예 :DataGridView 셀에 값을 할당 할 때 느려짐
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
관련 값 (소스를 변경)을 사용하여 '데이터 소스'를 만들 수 없으며 바인딩 할 수 있습니까? 'CellFormatting' 이벤트의 색상 – V4Vendetta
똑같은 문제가 너무 커서이 포스트를 발견했습니다. MyClass의 메모리 사전에있는 DataGridView에 각각 12 개의 셀이있는 400 개의 행을 추가하면 15 초가 소요됩니다. 하지만이 두 줄의 코드를 추가하면 "dgv_mainlist.Rows [r_index] .Cells [3] .Value =" ";" 모든 것을 실행하려면 1 초 이상 걸립니다. 행당 2 개의 셀에 값을 추가하여 거의 5-6 배 더 길게. - 3 년이 게시물에 아직 대답 ... 나는 내 숨을 참지 않을 것 같아요. 컨트롤에 문제가있는 것 같습니다. –