如何在gridview中使用多个datakeys?

时间:2017-07-28 06:21:42

标签: c# asp.net gridview

我创建了一个包含多个(两个)数据键的gridview,并尝试在下面的代码中提取datakey值。当我加载gridview时,行[“BookTaxId”]给出了正确的Id。但是当我从Gridview datakeys代码中提取其值时,同一列返回“1”。有谁能够帮我? 码 - >

Gridview:-
<asp:GridView runat="server" ID="TaxGV" DataKeyNames="Id, BookTaxId" AutoGenerateColumns="false" OnDataBound="TaxGV_DataBound" OnRowCommand="TaxGV_RowCommand" CssClass="table table-bordered table-hover table-responsive" ShowFooter="true">

LoadingGridview TaxGV:-
public void LoadTaxGridView_Table()
    {
        double Value = 0;
        int bkId = Convert.ToInt32(bookId);
        string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        SqlConnection con = new SqlConnection(str);
        con.Open();
        SqlCommand cmd = new SqlCommand("select b.BookTaxId as BookTaxId, b.TaxId as Id, t.Name as Name, b.TaxValue as Value from BookingTax as b inner join Taxes as t on t.Id = b.TaxId where b.BookingId=@BookingId and b.Is_Delete=0", con);
        cmd.Parameters.AddWithValue("@BookingId", bkId);
        DataTable dt_BookTax = new DataTable();
        SqlDataReader reader = cmd.ExecuteReader();
        dt_BookTax.Load(reader);
        con.Close();
        foreach(DataRow row in dt_BookTax.Rows)
        {
            int Id = Convert.ToInt32(row["BookTaxId"]);//This gives correct Value of BookTaxId
            Value = Convert.ToDouble(row["Value"]);
            total_tax_Amount += (temp_Amount * Value / 100);

        }

        GetParameters_BookingTax(Value);

        DataRow dr = null;
        dr = dt_BookTax.NewRow();
        dr["BookTaxId"] = 0;
        dr["Id"] = 0;
        dr["Name"] = string.Empty;
        dr["Value"] = 0;
        dt_BookTax.Rows.Add(dr);

        ViewState["BookTax_Table"] = dt_BookTax;
        TaxGV.DataSource = dt_BookTax;
        TaxGV.DataBind();
        TaxGV.Rows[TaxGV.Rows.Count - 1].Visible = false;

    }

通过GridView TaxGV更新表: -

public void Edit_Booking_Tax()
    {
        int bkId = Convert.ToInt32(bookId);
        string str = ConfigurationManager.ConnectionStrings["ConStr"].ConnectionString;
        SqlConnection con = new SqlConnection(str);
        con.Open();
        foreach(GridViewRow row in TaxGV.Rows)
        {
            int rowindex = row.RowIndex;
            int BookTaxId = Convert.ToInt32(TaxGV.DataKeys[rowindex].Values["BookTaxId"]);
            //This BookTaxId gives value=1; 
            Label lb_TaxValue = (Label)row.Cells[1].FindControl("Item_Value");
            HiddenField hf_TaxId = (HiddenField)row.Cells[2].FindControl("hf_TaxId");
            if (lb_TaxValue.Text != "0")
            {
                SqlCommand cmd = new SqlCommand("Update BookingTax set BookingId=@BookingId, TaxId=@TaxId, TaxValue=@TaxValue, ModifiedBy=@ModifiedBy, ModifiedDate=@ModifiedDate where BookTaxId=@BookTaxId", con);
                cmd.Parameters.AddWithValue("@BookingId", bkId);
                cmd.Parameters.AddWithValue("@TaxId", Convert.ToInt32(hf_TaxId.Value));
                cmd.Parameters.AddWithValue("@TaxValue", Convert.ToDouble(lb_TaxValue.Text));
                cmd.Parameters.AddWithValue("@ModifiedBy", Session["id"].ToString());
                cmd.Parameters.AddWithValue("@ModifiedDate", DateTime.Now);
                cmd.Parameters.AddWithValue("@BookingId", bkId);
                cmd.Parameters.AddWithValue("BookTaxId", BookTaxId);
                cmd.ExecuteNonQuery();
            }
        }
        con.Close();
    }

1 个答案:

答案 0 :(得分:0)

尝试更改此行

int BookTaxId = Convert.ToInt32(TaxGV.DataKeys[rowindex].Values["BookTaxId"]);

到这个

int BookTaxId = Convert.ToInt32(TaxGV.DataKeys[rowindex].Values[1]);

据我所知,您应该能够根据其位置访问DataKeys的值,类似于访问特定行的数组。