单击Asp.Net中的GirdView中的添加按钮时,如何在下拉菜单中删除上一个选定项目

时间:2018-07-26 07:48:28

标签: asp.net sql-server c#-4.0

如何在下拉添加按钮中删除最后一行选择的项目,单击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();
}

1 个答案:

答案 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
    }
  }
}