0

因此,对于一个项目,我试图从数据库中检索一个值,并在单击编辑按钮时将其存储在占位符中。我已经尝试了很多方法来让它工作,但它只返回第一行第一列的值。我希望它从我选择编辑的行中返回值。到目前为止,这就是我在后面的代码中所拥有的。

protected void grdEditPersonnel_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Edit")
    {
        //sets the value of the row selected for possible use??
        int index = Convert.ToInt32(e.CommandArgument);
        GridViewRow selectedRow = grdEditPersonnel.Rows[index];

        try
        {

            OleDbConnection connection = new OleDbConnection(ConfigurationManager.ConnectionStrings["RiskManager_DBConnectionString"].ConnectionString);
            OleDbCommand command = new OleDbCommand("SELECT [ID] FROM [tblProjects]", connection);

            connection.Open();
            OleDbDataReader reader = command.ExecuteReader(CommandBehavior.SingleRow);

            while (reader.Read())
            {
                //sends the value of the ID to the Project ID Place Holder
                Session["ProjIDPH"] = reader.IsDBNull(reader.GetOrdinal("ID")) ? null :
                                          reader["ID"].ToString();
            }

            reader.Close();

            //Redirects user to the Add Project page
            Response.Redirect("frmProjects.aspx");
        }
        catch
        {

        }
    }
}
4

1 回答 1

0

您没有使用从 gridview 收集的索引值(至少我不能看到)。您可以对命令文本使用类似的内容来选择正确的值。

        "SELECT ID FROM tblProjects WHERE ID = " & GridView.SelectedDataKey.Value.ToString

我将在 gridview 的选定索引更改事件处理程序中执行此操作,以检索稍后编辑按钮所需的值。然后当按下编辑按钮时,使用该值从数据库中检索数据。

希望我没有完全错过重点。

于 2013-06-19T00:32:51.613 回答