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