2012-10-15 2 views
0

안녕하세요 저는 텍스트 박스의 데이터를 데이터베이스가 아닌 로컬 gridview에 추가하고 있습니다. 그것의 판매원이 제품을 saling하는 salepage처럼. 지금은 그 gridview에서 행을 삭제하려면하지만 난 항상 삭제하는 동안 예외가 있어요. 친절하게 도와주세요. 여기에 내 코드를 어디에 내가 데이터를 가져 와서 gridview에 저장됩니다. 감사.gridview에서 특정 행을 삭제하는 방법. gridview의 기본 삭제 명령 단추를 사용하십시오.

DataTable dt1 = new DataTable(); 

    protected void Page_Load(object sender, EventArgs e) 
    { 
     if (!Page.IsPostBack) 
     { 
      gridVIEWData(); 
      GridView1.DataSource = dt1; 
      GridView1.DataBind(); 
     } 
private void gridVIEWData() 
    { 

     dt1.Columns.Add("pName", typeof(string)); 
     dt1.Columns.Add("pCategory", typeof(string)); 
     dt1.Columns.Add("price", typeof(string)); 
     dt1.Columns.Add("pQuantity", typeof(string)); 
     dt1.Columns.Add("totalPrice", typeof(string)); 
     Session["dtInSession"] = dt1; //Saving Datatable To Session 
    } 
protected void Button3_Click(object sender, EventArgs e) 
    { 
     if (Session["dtInSession"] != null) 
      dt1 = (DataTable)Session["dtInSession"]; //Getting datatable from session 


      Int32 total = Convert.ToInt32(txt_quantity.Text) * Convert.ToInt32(txt_price.Text); 
      DataRow dr = dt1.NewRow(); 
      dr["pName"] = DropDownList2.SelectedItem; 
      dr["pCategory"] = DropDownList1.SelectedItem; 
      dr["price"] = txt_price.Text; 
      dr["pQuantity"] = txt_quantity.Text; 
      dr["totalPrice"] = total; 
      dt1.Rows.Add(dr); 


      Session["dtInSession"] = dt1;  //Saving Datatable To Session 

      GridView1.DataSource = dt1; 
      GridView1.DataBind(); 

     } 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
    { 

     // string value = GridView1.DataKeys[e.RowIndex].Values["price"].ToString(); 
     //GridView1.DeleteRow(Convert.ToInt32(value)); 
     //GridView1.DeleteRow(GridView1.SelectedIndex); 

      // dt1.Rows.RemoveAt(GridView1.SelectedIndex); 
     Int32 index = GridView1.SelectedIndex; 
     GridView1.DeleteRow(index); 
    } 

답변

0

그냥 코드에 부합하는

방법 항목 :

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e) 
{ 
     if (Session["dtInSession"] != null) 
      dt1 = (DataTable)Session["dtInSession"]; 

     Int32 index = GridView1.SelectedIndex; 
     dt1 = dt1.Rows[index].Delete(); 

     dt1.AcceptChanges(); 

     Session["dtInSession"] = dt1; //Saving Datatable To Session 
     GridView1.DataSource = dt1;       
     GridView1.DataBind(); 
} 

방법 2 :

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="pName" onselectedindexchanged="GridView1_SelectedIndexChanged"> 

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) 
{ 
    string _pName = Convert.ToString(GridView1.SelectedDataKey.Value); 

    // Once you get the pName you can query from DataTable and Delete the row which the pName = _pName 
    // BindGrid and Store DataTable in session. 
} 

도움이 될 것입니다. 아직도의 오류가

+0

m 당신은 DataKeyNames =를 사용할 필요가 –

+0

"위치 -1에서 어떤 행이 없다" "pName은은"[pName은] 고유해야합니다. –

-1
protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e) 
     { 
      if (ViewState["CurrentTable"] != null) 
       dt = (DataTable)ViewState["CurrentTable"];    
      if (dt.Rows.Count > 0) 
      { 
       int Index = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["Pk"].ToString()); 
       dt.Rows.Cast<DataRow>().Where(o => o.Field<Int32>("Pk").Equals(Index)).FirstOrDefault().Delete(); 
       // dt.Rows.RemoveAt(Index-1); 
       gvDetails.DataSource = dt; 
       gvDetails.DataBind(); 
      } 
     } 
+1

이 질문에 대한 답변을 드릴 수 있지만, 5 줄의 서식없는 텍스트를 붙여 넣기보다는 수행중인 내용에 대한 설명을 제공해주십시오. – Deanna

관련 문제