我在下面的代码中显示来自访问数据库的 datagridview 中的数据。我有不同的行,但只显示数据库中的最后一行数据。我不知道我的代码有什么问题。
dataGridView1.Columns.Add("UserID", "UserID");
dataGridView1.Columns.Add("FirstName", "FirstName");
dataGridView1.Columns.Add("MI", "MI");
dataGridView1.Columns.Add("LastName", "LastName");
dataGridView1.Columns.Add("Birthdate", "Birthdate");
dataGridView1.Columns.Add("Address", "Address");
dataGridView1.Columns.Add("UserName", "UserName");
dataGridView1.Columns.Add("UserPassword", "UserPassword");
dataGridView1.Columns.Add("Rights", "Rights");
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\\SISC-STRONGHOLD\MIS!\wilbert.beltran\SEEDBucksDbase.accdb");
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "SELECT * From TableAcct";
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
dataGridView1.Rows.Add();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserID"].Value = reader[0].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["FirstName"].Value = reader[1].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["MI"].Value = reader[2].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["LastName"].Value = reader[3].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Birthdate"].Value = reader[4].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Address"].Value = reader[5].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserName"].Value = reader[7].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["UserPassword"].Value = reader[8].ToString();
dataGridView1.Rows[dataGridView1.Rows.Count - 1].Cells["Rights"].Value = reader[9].ToString();
}
conn.Close();
}