如何在GridView中使用多个数据键? [英] how to work with multiple datakeys in gridview?

查看:44
本文介绍了如何在GridView中使用多个数据键?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个具有多个(两个)数据键的gridview,并尝试从下面包含的代码中提取数据键值.当我加载gridview时,row ["BookTaxId"]给出了使用的正确ID.但是,当我从Gridview数据键代码中提取其值时,同一列将返回"1".有谁能够帮我?代码->

I created a gridview with multiple (two) datakeys and tried to extract the datakey values in the code included below. When I load the gridview, the row["BookTaxId"] gives correct Id being used. But while I'm extracting its value from Gridview datakeys code the same column returns "1". Can anybody help me? Code-->

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更新表:-

Updaing Table through 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();
    }

推荐答案

尝试更改此行

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

对此

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

据我所知,您应该能够根据数据键的位置访问数据键的值,就像访问特定行的数组一样.

As far as I remember you should be able to access values of DataKeys based on their position similar to accessing an array for particular row.

这篇关于如何在GridView中使用多个数据键?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆