2013-03-14 147 views
1

我試圖刪除一個GridView中的一行(數據還沒有在數據庫中),所以它應該是一個簡單的刪除行,如果有人可以請指點我在正確的方向... 。 這是我做了什麼:GridView刪除行

 else if (e.CommandName == "DeletePart") 
    { 
     int index = Convert.ToInt32(e.CommandArgument); //#1 line 
     GridView1.DeleteRow(index); //#2 line 
    } 

是我收到的錯誤是:「輸入字符串的不正確的格式」 (這發生在1號線)...

這是在GridView的樣子:(使用一個LinkBut​​ton做刪除)的GridView控件如何填充和綁定

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
           EmptyDataText="No parts has been added to the model, please add a part." 
           BorderColor="Black" BorderStyle="Solid" BorderWidth="2px" 
           CellPadding="3" onrowcommand="GridView1_RowCommand" 
          onrowediting="GridView1_RowEditing" onrowdeleting="GridView1_RowDeleting"> 
           <Columns> 
           <asp:TemplateField HeaderText=""> 
           <ItemTemplate> 
            <asp:LinkButton ID="EditButton" runat="server" CssClass="EditButton" CommandName="Edit" Text="Edit" /> 
           </ItemTemplate> 
           </asp:TemplateField> 
           <asp:TemplateField HeaderText=""> 
           <ItemTemplate> 
            <asp:LinkButton ID="DeleteButton" runat="server" CssClass="DeleteButton" CommandName="DeletePart" CommandArgument='<%# Container.DataItemIndex %>' Text="Delete" /> 
           </ItemTemplate> 
           </asp:TemplateField> 
           </Columns> 
          <HeaderStyle BackColor="#FFFFCC" BorderColor="Black" BorderStyle="Solid" 
           BorderWidth="2px" /> 
         </asp:GridView> 

類:

 public int companyID = 0; 
public int methodID = 0; 
DataTable dt; 
#region SQLConnections 

string connectionString = ConfigurationManager.ConnectionStrings["SOSConnectionString"].ConnectionString; 
SqlConnection conn; 
#endregion 

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!Page.IsPostBack) 
    { 
     dt = new DataTable(); 
     MakeDataTable(); 
     EmptyDataString(); 
    } 
    else 
    { 
     dt = (DataTable)ViewState["DataTable"]; 
    } 
    ViewState["DataTable"] = dt; 

} 

#region GridView 

private void EmptyDataString() 
{ 
    TemplateBuilder tmpEmptyDataTemplate = new TemplateBuilder(); 
    tmpEmptyDataTemplate.AppendLiteralString("No parts has been added to the model, please add a part."); 
    GridView1.EmptyDataTemplate = tmpEmptyDataTemplate; 
    GridView1.DataBind(); 
} 

private void MakeDataTable() 
{ 
    dt.Columns.Add("Item"); 
    dt.Columns.Add("Quantity"); 
    dt.Columns.Add("Price P/Quantity"); 
} 

private void AddToDataTable() 
{ 
    DataRow dr = dt.NewRow(); 
    dr["Item"] = txtPart.Text; 
    dr["Quantity"] = numQuantity.Text; 
    dr["Price P/Quantity"] = txtPricePQ.Text; 
    dt.Rows.Add(dr); 
} 

private void BindGrid() 
{ 
    GridView1.DataSource = dt; 
    GridView1.DataBind(); 
} 

protected void btnAddPart_Click(object sender, EventArgs e) 
{ 
    AddToDataTable(); 
    BindGrid(); 
    ClearNewParts(); 
} 
#endregion 

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Edit") 
    { 
     for (int i = 0; i < GridView1.Rows.Count; i++) 
     { 
      GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
      txtPart.Text = row.Cells[2].Text; 
      numQuantity.Text = row.Cells[3].Text; 
      txtPricePQ.Text = row.Cells[4].Text; 
     } 
    } 
    else if (e.CommandName == "DeletePart") 
    { 
     //int iCount = GridView1.Rows.Count; 
     //for (int i = 1; i <= iCount; i++) 
     //{ 
     // GridView1.DeleteRow(i); 
     //} 
     // int rowIndex = Convert.ToInt32(GridView1.SelectedRow); 

     int index = Convert.ToInt32(e.CommandArgument); 
     GridView1.DeleteRow(index); 

     //int index = Convert.ToInt32(e.CommandArgument); 
     //GridView1.DeleteRow(rowIndex); 
    } 


    GridView1.DataBind(); 
} 
+1

你能調試,並出示e.CommandArgument的價值? – 2013-03-14 07:12:41

+0

它打破了這個特定的行,如果我看看它只給這個值「」...有沒有另一種方法來刪除行? – Kerieks 2013-03-14 07:17:05

+0

您是否嘗試過'GridView1.SelectedIndex'來獲取已刪除行的索引? – noobob 2013-03-14 07:30:50

回答

2

找到了解決辦法,唯一需要做的一個數據綁定....這裏是工作代碼:

else if (e.CommandName == "Delete") 
    {  
     int index = Convert.ToInt32(e.CommandArgument); 
     GridView1.DeleteRow(index); 
     ((DataTable)ViewState["DataTable"]).Rows[index].Delete(); 
     ((DataTable)ViewState["DataTable"]).AcceptChanges(); 
     GridView1.DataSource = (DataTable)ViewState["Data"]; 
     GridView1.DataBind(); 
    } 
+0

無需編寫'GridView1。DeleteRow(index);'如果你將最後一個'ViewState [「DataTable」]''分配給你的Gridview。 – Chirag 2014-02-03 13:39:41

0

嘗試int.TryParse,這將解決輸入字符串fromat問題,但爲什麼發生需要檢查您設置的CommandArgument。

else if (e.CommandName == "DeletePart") 
{ 
    int index =-1; 
    if(int.TryParse(e.CommandArgument,out index)) //#1 line 
     GridView1.DeleteRow(index); //#2 line 
} 
+0

e .CommandArgument自從他創建該行後將始終爲空。 – noobob 2013-03-14 07:32:30

+0

我正在顯示正確的解析方式,實際問題出在別的地方,我已經提到 – TalentTuner 2013-03-14 07:33:22

+0

這種方法會得到另一個錯誤:「最好的重載方法匹配'int.TryParse(string,out int)'有一些無效參數」,我試圖獲得CommandArgument的原因是因爲我需要獲取rowindex ....我查看了互聯網上的幾個網站,發現這是最常用的刪除行的方式....感謝也許只需要得到另一種獲得索引的方法.... – Kerieks 2013-03-14 07:46:46

1

Seems like you didn't mention CommandArgument property in the GridView .

<asp:GridView runat="server" ID="gv" 
       CommandName="DeletePart" 
       CommandArgument='<%# Container.DataItemIndex %>'>   
</asp:GridView> 

Add the CommandArgument as shown above.

+0

爲GridView添加CommandArgument,但我仍然沒有找回應該刪除的rowindex .... – Kerieks 2013-03-14 07:50:15

1

試試這個..

在ASPX

<asp:GridView runat="server" ID="Gridview1" CommandName="delete" CommandArgument='<%#Container.DataItem.Index %>'/> 

else if (e.CommandName == "DeletePart") 
{ 
    int index = Convert.ToInt32(e.CommandArgument); 
    GridView1.DeleteRow(index); 
} 
+0

索引返回爲0。 ..該行仍然不會從gridview中刪除..... – Kerieks 2013-03-14 07:58:24

+0

@ user2042152其中是要刪除的數據?你不在gridview中綁定任何東西,那麼我如何刪除一行? – coder 2013-03-14 07:59:43

+0

當行被插入它綁定到gridview,將更新我的帖子爲你看 – Kerieks 2013-03-14 08:10:34

0
Thy This:) 

<asp:GridView ID="GridView1" runat="server" CssClass="gridview" 
          HorizontalAlign="Left" PagerSettings-Visible="true" AllowPaging="True" 
          PageSize ="5" Width="300px" > 
          <Columns> 
           <asp:CommandField ShowDeleteButton="True" ButtonType="Button" />  
          </Columns> 
          </asp:GridView> 

then code behinde: 


    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting 
     DirectCast(ViewState("CurrentDataReference"), DataTable).Rows.RemoveAt(e.RowIndex) 
     GridView1.DataSource = DirectCast(ViewState("CurrentDataReference"), DataTable) 
     GridView1.DataBind() 

    End Sub