[英]Delete data row from sql in a gridview
I am trying to delete data from a gridview using aa link button. 我正在尝试使用一个链接按钮从gridview删除数据。 I need help with the logic and making the link button remove the row data from the database on click.
我需要逻辑方面的帮助,并且使链接按钮在单击时从数据库中删除行数据。
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>
Store precedure thats importing data to 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();
}
Add OnClick
attribute for the LinkButton, for example (added OnClick
to your linkbutton code) 例如,为LinkButton添加
OnClick
属性(将OnClick
添加到您的linkbutton代码中)
<asp:LinkButton ID="LnkBtnRemove" runat="server" CausesValidation="false" CommandName="DeleteItem" CommandArgument='<%# Eval("OtherDataID") %>' OnClick='LnkBtnRemove_Click'>Delete</asp:LinkButton>
Have OnClick
listener as 将
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
}
Just attach ItemCommand
event to your grid 只需将
ItemCommand
事件附加到网格
On web page in your grid definition add: 在网页上的网格定义中添加:
<asp:DataGrid OnItemCommand="Grid_ItemCommand" />
after that in code behind: 之后,在后面的代码中:
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();
}
Convert your boundfiled into Item Template, Here using sourceid
will fire delete
Query. 将您的装订文件转换为项目模板,在此使用
sourceid
将触发delete
查询。
Code look like this: 代码如下:
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.