正如标题所说,
DataGridview.ScrollBars = 垂直;
假设 DataGridview 的视觉可以包含 4 行,如果行到 6,它有一个 Vertical ScrollBar。但是如果点击滚动条,程序就会崩溃。如果我们设置 DataGridview.ScrollBars = None,就不会出现问题。
public partial class visitorLeave : Form
{
public visitorLeave()
{
InitializeComponent();
}
bool isWorkerStopped = false;
bool clickstart = false;
ManageEmployee me = null;
Thread tr1;
private void visitorLeave_Load(object sender, EventArgs e)
{
me = new ManageEmployee(10);
dataGridView1.AutoGenerateColumns = false;
dataGridView1.DataSource = me.DataSource;
tr1 = new Thread(new ThreadStart(Add));
tr1.IsBackground = true;
tr1.Start();
}
void Add()
{
while (!isWorkerStopped)
{
if (clickstart)
{
me.AddEmployee(new EmployeeData("new" + DateTime.Now.Ticks.ToString(), "0", "0", "0", "0", "0", null));
dataGridView1.DataSource = me.DataSource;
dataGridView1.UpdateRowHeightInfo(0, true);
clickstart = false;
}
}
}
private void button1_Click(object sender, EventArgs e)
{
clickstart = !clickstart;
}
}