我如何從數據庫讀取記錄並可以插入到DataGridView
中。我不想綁定數據源。只想使用While循環讀取和插入。我知道如何從數據庫中讀取Sql Server 2008 R2 Express Edition
但是無法在Datagridview中插入記錄。從數據庫中讀取並插入到DataGridView中
任何人都可以幫助我嗎?
string query = "Select id, IdNo, status From Members Where [email protected]";
sqlCommand = new SqlCommand(query, sqlConnection);
sqlConnection.Open();
sqlCommand.Parameters.AddWithValue("@Head", familyMemberId);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.HasRows)
{
//What to write here while my DataGridView Name is ufGView and it has
//4 column. Names are Id, IdNo{TextBox}, Status{CheckBox}, Action{HyperLink}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.Message.ToString(), "Exception in CheckFamilyMembers");
}
我期待這樣的事情。
int i = 0;
while (sqlDataReader.Read())
{
ufGview.Rows[i].Cells["Column3"].Value = sqlDataReader["id"].ToString();
ufGview.Rows[i].Cells["Column1"].Value = sqlDataReader["IdNo"].ToString();
ufGview.Rows[i].Cells["Column2"].Value = sqlDataReader["status"].ToString();
i++;
}
您是否嘗試過寫什麼去了'if'語句裏面?可能重複[此帖子。](http://stackoverflow.com/questions/6584817/direct-method-from-sql-command-text-to-dataset) – Hank
[直接方法從SQL命令文本到DataSet的可能重複(http://stackoverflow.com/questions/6584817/direct-method-from-sql-command-text-to-dataset) – Hank