我现在遇到了问题。我已成功将数据检索到textbox
和picturebox
。但是,当我单击不同的行时,数据始终显示datagridview
中的第一行。当然,我想显示其他行的数据。
以下是我使用的示例代码。
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
DataTable dt = StaffInfo();
DataRow row = dt.Rows[0];
textBox1.Text = row["FName"].ToString();
textBox2.Text = row["MName"].ToString();
textBox3.Text = row["LName"].ToString();
textBox4.Text = row["Username"].ToString();
textBox5.Text = row["Password"].ToString();
textBox6.Text = row["Password"].ToString();
pictureBox3.Image = LoadPhoto((byte[])row["AccImage"]);
}
private Image LoadPhoto(byte[] photo)
{
MemoryStream ms = new MemoryStream(photo);
return Image.FromStream(ms);
}
private DataTable StaffInfo()
{
DataTable dtStaff = new DataTable();
OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\patrick\Desktop\New folder\Database1.accdb");
OleDbCommand cmd = new OleDbCommand();
cmd.CommandText = "Select * From Accounts1";
using (conn)
{
using (cmd)
{
conn.Open();
cmd.Connection = conn;
cmd.Parameters.AddWithValue("FName", textBox1.Text);
OleDbDataReader reader = cmd.ExecuteReader();
dtStaff.Load(reader);
conn.Close();
}
}
return dtStaff;
}
另外,我正在使用Microsoft Access Database