2013-05-27 55 views
1

我想使用鏈接按鈕從gridview中刪除數據。我需要關於邏輯的幫助,並使鏈接按鈕從點擊數據庫中刪除行數據。在gridview中從sql中刪除數據行

的GridView

 <asp:GridView Style="width: 100%" ID="GvReportResults" runat="server"      AutoGenerateColumns="False" EmptyDataText="No data" ShowHeaderWhenEmpty="True"> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:LinkButton ID="lnkBtnEdit" runat="server" CausesValidation="false" >Edit</asp:LinkButton> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:LinkButton ID="LnkBtnRemove" runat="server" CausesValidation="false" CommandName="DeleteItem" CommandArgument='<%# Eval("OtherDataID") %>'>Delete</asp:LinkButton> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:BoundField DataField="SourceID" HeaderText="ID" /> 
       <asp:BoundField DataField="LastName" HeaderText="Last Name" /> 
       <asp:BoundField DataField="FirstName" HeaderText="First Name" /> 
       <asp:BoundField DataField="MiddleName" HeaderText="Middle Name" /> 
       <asp:BoundField DataField="Title" HeaderText="Title" /> 
       <asp:BoundField DataField="NationalID" HeaderText="SSN" /> 
       <asp:BoundField DataField="DOB" HeaderText="DOB" /> 
       <asp:BoundField DataField="HireDate" HeaderText="Hire Date" /> 
       <asp:BoundField DataField="Address1" HeaderText="Address" /> 
       <asp:BoundField DataField="City" HeaderText="City" /> 
       <asp:BoundField DataField="State" HeaderText="State" /> 
       <asp:BoundField DataField="PostalCode" HeaderText="Zip Code" /> 
      </Columns> 
     </asp:GridView> 

商店precedure這就是數據導入到GridView控件

private void BindGrid() 
{ 
    //set up arguments for the stored proc 
    int? FacilityID = (ddlFacility2.SelectedValue.Equals("-1")) ? (int?)null : int.Parse(ddlFacility2.SelectedValue); 
    int? OtherDataID = null; 

    //bind 
    GvReportResults.DataSource = this.DataLayer.model.MS_spGetOtherData(FacilityID, OtherDataID); 
    GvReportResults.DataBind(); 
} 
+0

您可以通過'項目template'添加一個命令argurment或使用'Gridview_RowDeleting'事件轉寄此插上U基本思路HTTP在'RowCommand'事件刪除://satindersinght.blogspot .in/2012/08/how-to-addupdate-record-using-gridview.html –

回答

1

添加OnClick屬性爲LinkBut​​ton的,例如(添加OnClick到你的LinkBut​​ton的代碼)

<asp:LinkButton ID="LnkBtnRemove" runat="server" CausesValidation="false" CommandName="DeleteItem" CommandArgument='<%# Eval("OtherDataID") %>' OnClick='LnkBtnRemove_Click'>Delete</asp:LinkButton>

OnClick監聽器

protected void LnkBtnRemove_Click(object sender,EventArgs e) 
{ 
    string id = ((LinkButton)sender).CommandArgument;//CommandArgument is always returns string 
    Do_DeleteRow(id);//method to delete 
    BindGrid(); 
} 

private void Do_DeleteRow(string id) 
{ 
    //your delete code will be added here 
} 
+0

它在Do_DeleteRow(ID)上出錯。說在當前的情況下不存在? – user2425793

+0

'Do_DeleteRow(ID)'是您必須定義要刪除的方法。 對不起,我還沒有添加,作爲評論。 我會編輯這個... – Bharadwaj

0

只是ItemCommand事件附加到網格

在網頁中網格定義添加:

<asp:DataGrid OnItemCommand="Grid_ItemCommand" /> 

後,在後面的代碼:

void Grid_ItemCommand(Object sender, DataGridCommandEventArgs e) 
{ 
    var id = Int32.Parse(e.CommandArgument.ToString()); //use parse if OtherDataID is int 

    //here goes logic for deleting row 
    this.DataLayer.model.spDeleteData(id); 

    //after deleting rebind grid 
    BindGrid(); 
} 
+0

將一個ItemCommand事件添加到gridview將如何使鏈接按鈕刪除數據?對不起,只是混淆了我爲項目命令事件放置的位置和內容。謝謝 – user2425793

+0

做了一些編輯,使其更清楚。在鏈接按鈕上單擊該事件將觸發關於控制單擊,命令名稱和命令參數的信息。只要處理刪除,你會沒事的。 – gzaxx

+0

它現在在this.DataLayer.model.spDeleteData(id)中的「spDeleteData」上發生錯誤;說我的模型不包含deleteData的定義。你也可以告訴我如何將這個事件放在onclick事件中嗎? – user2425793

0

將您的boundfiled轉換爲項目模板,這裏使用sourceid將激發delete查詢。
代碼如下所示:

protected void GvReportResults_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "DeleteItem") 
    { 
     int getrow = Convert.ToInt32(e.CommandArgument); 
     Label lblSourceID = (Label)GvReportResults.Rows[getrow].FindControl("lblSourceID"); 
     bool flag=deleteRecord(lblSourceID.text); 
     if(flag) 
     { 
     succes msg! 
     } 
     else{ failed msg} 
     } 
    } 

    public bool deleteRecord(String SourceID) 
    { 
    try 
     { 
     SqlCommand cmd = new SqlCommand("Your Delete Query where condtion ", conn); 
     conn.Open(); 
     cmd.ExecuteNonQuery(); 
     conn.Close(); 
     return true; 
    } 
    catch(Exception ac) 
    { 
     return false; 
    } 

}