如何在下拉添加按钮中删除最后一行选择的项目,单击gridview asp.net。
GridViewRow gvrow = (GridViewRow)((Control)e.CommandSource).NamingContainer;
if (e.CommandName == "Add")
{
ddt = CreateDt();
int j = 0;
foreach (GridViewRow gr in GvPatientAccomDtls.Rows)
{
ddt.Rows.Add();
ddt.Rows[j]["TreatmentCode"] = ((DropDownList)gr.FindControl("ddlTreatment")).SelectedValue;
ddt.Rows[j]["Days"] = ((TextBox)gr.FindControl("txtDays")).Text;
ddt.Rows[j]["Cost"] = ((TextBox)gr.FindControl("txtCost")).Text;
j++;
}
ddt.Rows.Add();
GvPatientAccomDtls.DataSource = ddt;
GvPatientAccomDtls.DataBind();
}
答案 0 :(得分:0)
您可以使用 RowDataBound 事件
进行处理void GridView1_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var thisRow = (DataRowView)e.Row.DataItem;
var source = thisRow.DataView;
var lastRowIndex = e.Row.DataItemIndex -1;
DataRowView lastRow = null;
var ddl = (DropDownList)e.Item.FindControl("ddl");
DropDownList ddlLast = null;
if(lastRowIndex>=0){
lastRow = source[lastRowIndex];
ddlLast = (DropDownList)((GridView)sender).Rows[lastRowIndex].FindControl("ddl");
//remove the items of this ddl according to the items of the last dll
}
}
}