2017-07-28 2 views
0

Ich erstellte eine Gridview mit mehreren (zwei) Datakeys und versuchte, die Datakey-Werte in dem unten angegebenen Code zu extrahieren. Wenn ich die Gridview lade, wird in der Zeile ["BookTaxId"] die richtige ID verwendet. Aber während ich den Wert aus dem Gridview-Datakeys-Code extrahiere, gibt dieselbe Spalte "1" zurück. Kann mir jemand helfen? -Code ->wie mit mehreren Datensätzen in Gridview arbeiten?

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 [email protected] 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; 

    } 

Updaing Tabelle durch Grid 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 [email protected], [email protected], [email protected], [email protected], [email protected] where [email protected]", 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(); 
    } 

Antwort

0

Versuchen Sie, diese Linie zu ändern

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

dieser

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

Soweit ich erinnere dich an dich Ich könnte auf Werte von DataKeys basierend auf ihrer Position zugreifen, ähnlich wie beim Zugriff auf ein Array für eine bestimmte Zeile.

+0

Es funktionierte! Vielen Dank –

Verwandte Themen