我有asp gridview,它与数据表绑定。只有一个复选框的模板列与源数据表列(sel
)绑定。
这是标记示例:
<asp:GridView ID="testGrid"
CssClass="ObjSelection"
AutoGenerateColumns="false"
OnRowDataBound="testGrid_RowDataBound"
runat="server">
<Columns>
<asp:TemplateField HeaderText=" ">
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" Enabled="true" AutoPostBack="true" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox AutoPostBack="true" ID="chkRow" runat="server" Checked='<%# DataBinder.Eval(Container.DataItem, "sel")%>' OnCheckedChanged="ChkRow_OnCheckChange" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
如您所见,我没有使用RowCommand删除行。我在单独的div
中有一个工具栏,其中显示了删除按钮。
您是否可以指导如何删除另一个both from DataSource and GridView
中存在的按钮点击行div
?
答案 0 :(得分:1)
Use below code to remove selected row from datatable which is either in ViewState or in Session and assign data source
Aslo assign datakey to GridView
<asp:GridView ID="testGrid" DataKeyNames="PrimaryKey"
CssClass="ObjSelection"
AutoGenerateColumns="false"
OnRowDataBound="testGrid_RowDataBound"
runat="server">
Code Behind:
protected void btnRemove(object sender, EventArgs e)
{
// Check session exists
if (Session["Key"] != null)
{
// Opening / Retreiving DataTable.
DataTable dt = (DataTable)Session["Key"];
foreach (GridViewRow row in testGrid.Rows)
{
CheckBox chkRow= row.Cells[0].FindControl("chkRow") as CheckBox;
if (chkRow!= null && chkRow.Checked)
{
int Id = Convert.ToInt32(testGrid.DataKeys[row.RowIndex].Value);
DataRow[] drs = dt.Select("PrimaryKey = '" + Id + "'"); // replace with your criteria as appropriate
if (drs.Length > 0)
{
dt .Rows.Remove(drs[0]);
}
}
}
Session["Key"] = dt ;
testGrid.DataSource = dt ;
testGrid.DataBind();
}
}
答案 1 :(得分:0)
你可以使用隐藏字段通过javascript和Jquery
输入行的id<asp:HiddenField runat="server" ID="IDField"/>
的jQuery
<script>
$(document).ready(function(){
$("#testGrid tr").click(function(){
$("#IDField").val($(this).children("td.IDColumn").text());
$(this).Index(); // will give you row index
});
});
</script>
点击删除按钮
protected void delete_Click(object sender,EventArg e)
{
//delete from datasource use this.IDField.Value
//to refresh data in grid
this.testGrid.DataSource = dataTable;
this.testGrid.DataBind();
}
答案 2 :(得分:0)
gridview_RowDataBound(Sender sender,eventArgs e)
{
CheckBox lblChkRow = (CheckBox)e.Row.FindControl("IDField");
if(lblChkRow.Checked)
{
// Delete Value here and bind with datasource
}