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

时间:2016-08-02 17:36:55

标签: c# sql asp.net gridview

我有一个包含多个datakeynames的gridview(这是因为数据库中没有PK):

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

当行更新时,我希望只更改该行并尝试通过将原始值存储在字符串中并在where子句中使用它来尝试这样做。我在SQL Server Management Server中对此进行了测试,结果运行良好。

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。

2 个答案:

答案 0 :(得分:1)

将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]; 

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

答案 1 :(得分:0)

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();

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