Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:-
C#
<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:-
C#
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);

            }

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

        }

Updaing Table through GridView TaxGV:-

C#
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; 
                
        }
}


What I have tried:

I've tried the above code but still can't collect true value of BookTaxId in
Edit_Booking_Tax()
Posted
Updated 28-Jul-17 2:31am
v2

1 solution

Did you try to change the foreach like that?

C#
foreach(GridViewRow row in TaxGV.Rows)
           {
               int BookTaxId = Convert.ToInt32(row.Cells[0].Text);
               //I already test and is working

       }

You know the column position in your query, no need to use DataKey for this froeach.

Happy codding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900