[英]Delete data row from sql in a gridview
我正在嘗試使用一個鏈接按鈕從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>
存儲將數據導入到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();
}
例如,為LinkButton添加OnClick
屬性(將OnClick
添加到您的linkbutton代碼中)
<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
}
只需將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();
}
將您的裝訂文件轉換為項目模板,在此使用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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.