GridViews非常擅長顯示和管理數據。您聲明您擁有「與」「行關聯的」ID「值。但你不知道,你實際上做了一個數據庫調用以後檢索ID
對我來說這個尖叫「DataKey」。就我個人而言,我會找到一種方法來使您需要的ID的一部分填充GridView的數據集,然後將該ID添加爲GridView DataKey。這是您將ID與GridViewRow關聯的方式。這可以讓你消除選擇數據庫調用UpdateSelectedRecords()
但是,有時候你沒有這個選項,所以這裏是你的代碼,用這兩種解決方案修改和評論一些事情,供您考慮。
protected void UpdateSelectedRecords()
{
////////////////////////////////////////////
// Since these are all local to the function
//
string strPerSignStart = "%";
string strPerSignEnd = "%";
string strDbSearch = strPerSignStart + ddlDatabasesUpdateTCID.Text + strPerSignEnd;
//
//////////////////////////////////////////////
// Consider :
string strDbSearch = String.Format("%{0}%", ddlDatabasesUpdateTCID.Text);
//////////////////////////////////////////////
// FYI These are never used
//
object NewTCID = txtTargetCID.Text;
DateTime curtstmp = DateTime.Now;
//////////////////////////////////////////////
// Instantiate and open a connection once
// also consider using the using() statement
// it make managing connections much easier
//
using(OdbcConnection Postgreconnect2 = new OdbcConnection("Driver= {PostgreSQL Unicode};Server=localhost;Port=5433;Database=postgres;Uid=xxx;Pwd=xxx"));
{
Postgreconnect2.Open();
// Instantiate commands once
OdbcCommand cmd = new OdbcCommand("", Postgreconnect2);
OdbcCommand cmd1 = new OdbcCommand("", Postgreconnect2);
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
chkBox = (row.Cells[0].Controls[0] as CheckBox);
//////////////////////////////////
// chkBox.Checked *is* a boolean value, no need to convert to Bool
//
if (chkBox != null && chkBox.Checked)
{
//////////////////////////////////
// Since strDbSearch doesn't change
// just make it part of the select string you are building
// This eliminates the need for parameter passing
//
// Also, check the string you are building
// It doesn't look like it builds a proper SELECT statement
// You're missing a field after "AND"
cmd.CommandText =
"SELECT id FROM conceptidmapping "
"WHERE database ILIKE " + strDbSearch + " AND " +
(chkBox.Checked == true) + " ORDER BY id ASC";
//////////////////////////////////
// No parameters necessary, but...
// This already assigns the value to the parameter
//
//cmd.Parameters.AddWithValue("@database", strDbSearch);
//
// So there is no need to do it again here
//
//cmd.Parameters["@database"].Value = Convert.ToString(strDbSearch);
string idVal = (string)cmd.ExecuteScalar();
///////////////////////////////////
// IF YOU ARE ABLE TO MAKE THE ID A DATAKEY
// Then the entire cmd for the select call above
// can be removed in favor of this:
//
string idVal = Gridview1.DataKeys(row.RowIndex).Values("id")
cmd1.CommandText = "UPDATE conceptidmapping SET selected = true WHERE database ILIKE " + strDBSearch + " AND id = " + idVal ;
cmd1.ExecuteNonQuery();
}
}
}
}
}
同上面的代碼沒有註釋,並用datakey做
protected void UpdateSelectedRecords()
{
string strDbSearch = String.Format("%{0}%", ddlDatabasesUpdateTCID.Text);
using(OdbcConnection Postgreconnect2 = new OdbcConnection("Driver= {PostgreSQL Unicode};Server=localhost;Port=5433;Database=postgres;Uid=xxx;Pwd=xxx"));
{
Postgreconnect2.Open();
OdbcCommand cmd1 = new OdbcCommand("", Postgreconnect2);
foreach (GridViewRow row in GridView1.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
chkBox = (row.Cells[0].Controls[0] as CheckBox);
if (chkBox != null && chkBox.Checked)
{
string idVal = Gridview1.DataKeys(row.RowIndex).Values("id");
cmd1.CommandText = String.Format("UPDATE conceptidmapping SET selected = true WHERE database ILIKE {0} AND id = {1}", strDbSearch, idVal) ;
cmd1.ExecuteNonQuery();
}
}
}
}
}
我的道歉起初對這個沒有提供很多信息。發生的事情是程序進入'For'循環時,它將逐行瀏覽Gridview(從上到下)。當它看到複選框被選中時,它會輸入第二個IF語句(使用ODBC命令)。第一個ODBC命令試圖找到該行的「ID」值。一旦它具有'ID'值,它就會在第二個ODBC命令中使用它來將'selected'列從'false'更新爲'true'。 – M72
發生了什麼事是它從正確的數據庫中獲取'ID'值,但它從來沒有選擇複選框。有沒有人看到我在這種方法中有任何簡單的缺陷?我在'代碼隱藏'文件中也是這樣做的(C#),因爲該過程的第一步是從下拉列表中選擇「數據庫」值。我可能是錯的,但認爲在標記中填充的命令中使用這種方法很難。任何建議都非常歡迎! – M72