正在使用多个DataKeyNames更新GridView中的单行

本文关键字:GridView 更新 单行 DataKeyNames | 更新日期: 2023-09-27 17:59:54

我有一个包含多个数据关键字的网格视图(这是因为数据库中没有PK):

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
    DataSourceID="SqlDataSource1" DataKeyNames = "id,group,sto,busn" 
    onrowcommand="Add_RowCommand" onrowupdating="GridView1_RowUpdating" >

当该行更新时,我只希望该行发生更改,并试图通过将原始值存储在字符串中并在where子句中使用它来做到这一点。我在SQLServerManagementServer中对此进行了测试,结果运行良好。

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string connectionString = ConfigurationManager.ConnectionStrings["connectionString"].ConnectionString;
        SqlConnection conn = new SqlConnection(connectionString);
        TextBox id = (TextBox)rowIndex.FindControl("idTB");
        string old_id = e.OldValues["ID"].ToString();
        TextBox first_name = (TextBox)rowIndex.FindControl("fnameTB");
        TextBox last_name= (TextBox)rowIndex.FindControl("lnameTB");
        DropDownList group =(DropDownList)rowIndex.FindControl("groupDDL");
        string old_group= e.OldValues["Group"].ToString();
        TextBox sto= (TextBox)rowIndex.FindControl("stoTB");
        string old_sto= e.OldValues["Sto"].ToString();
        DropDownList busn= (DropDownList)rowIndex.FindControl("buDDL");
        string old_busn= e.OldValues["Busn"].ToString();
        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "UPDATE tablename SET id= @id, first_name= @first_name, last_name= @last_name, group= @group, sto= @sto, busn= @busn, WHERE id= @old_id AND group = @old_group AND (sto = @old_sto OR (@sto is null and sto is null)) AND busn = @old_busn";
        cmd.Parameters.AddWithValue("@id", id.Text);
        cmd.Parameters.AddWithValue("@old_id", old_id);
        cmd.Parameters.AddWithValue("@first_name", first_name.Text);
        cmd.Parameters.AddWithValue("@last_name", last_name.Text);
        cmd.Parameters.AddWithValue("@group", group.SelectedValue);
        cmd.Parameters.AddWithValue("@old_group", old_group);
        cmd.Parameters.AddWithValue("@sto", CheckGroupingCode(sto.Text));
        cmd.Parameters.AddWithValue("@old_sto", old_sto);
        cmd.Parameters.AddWithValue("@busn", busn.SelectedValue);
        cmd.Parameters.AddWithValue("@old_busn", old_busn);
        cmd.Connection = conn;
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
        Response.Redirect("tablename.aspx");
    }

更新:问题是我的OldValues返回为null。

正在使用多个DataKeyNames更新GridView中的单行

将DataKey值检索为

string values = GridView1.DataKeys[e.RowIndex].Value.ToString();

现在

values ="id,group,sto,busn"; //this is no code..showing what `values` will actually look like;

现在通过,(逗号)对它们进行拆分以获得单个值

string[] valueArr = values.Split(',');
var old_id = valueArr[0];
var old_group= valueArr[1];
var old_sto= valueArr[2];
var old_busn= valueArr[3]; 

然后您可以在查询中使用它。

string old_id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string old_group = GridView1.DataKeys[e.RowIndex].Values[1].ToString();
int old_sto = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[2]);
string old_busn = GridView1.DataKeys[e.RowIndex].Values[3].ToString();

^将获得以前的值,并且只更改一行