我在此處顯示訪問數據庫中datagridview中的數據時使用的代碼。我有不同的行,但只顯示數據庫中的最後一行數據。我不知道我的代碼中有什麼問題。如何在訪問數據庫中顯示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();
}
@JohnSaunders - 我很抱歉,但在C#中我只是一個初學者。代碼工作,但它只顯示我的表中的最後一行數據,但它應該顯示錶中的所有行。 – bhert 2013-03-01 02:41:05