当我更新DataGridView
中的数据时,垂直滚动条无法正常工作。
加载所有数据后,不显示垂直滚动条向上和向下按钮。我无法滚动垂直滚动条。
以下是更新代码:
private void Form1_Load(object sender, EventArgs e)
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Columns.Add("StudentId", "Student Id");
dataGridView1.Columns.Add("StudentName", "Student Name");
dataGridView1.Columns.Add("StudentCNIC", "Student CNIC");
dataGridView1.Columns.Add("StudentDoB", "Student DoB");
}
private void button1_Click(object sender, EventArgs e)
{
Thread trd = new Thread(this.threadtask);
trd.IsBackground = true;
trd.Start();
}
public void threadtask()
{
OleDbConnection connect = new OleDbConnection();
connect.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/Obaid Abbasi/Documents/Students1.accdb;Persist Security Info=False;";
connect.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connect;
command.CommandText = "SELECT * From Students";
OleDbDataReader reader = command.ExecuteReader();
while (reader.Read())
{
dataGridView1.AllowUserToAddRows = false;
dataGridView1.Rows.Add();
dataGridView1.ReadOnly = true;
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[0].Value = reader[0].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells[1].Value = reader[1].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["StudentCNIC"].Value = reader[2].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["StudentDoB"].Value = reader[3].ToString();
Thread.Sleep(100);
}
connect.Close();
}
我使用后台线程并在每条记录之间休眠,因为我想在1秒后添加每一行。