向listview添加新行将删除上面选定的下拉列表清除 [英] Adding new row to listview removes above selected dropdown Clearing

查看:84
本文介绍了向listview添加新行将删除上面选定的下拉列表清除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                FirstListViewRow();
                BindDataToGridviewDropdownlist();
            }
        }
        protected void BindDataToGridviewDropdownlist()
        {
            DataSet dsDept = new DataSet();
            dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
            DataView dv = dsDept.Tables[0].DefaultView;
            foreach (var list in listview1.Items)
            {
                if (list.ItemType == ListViewItemType.DataItem)
                {
                    DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
                    ddf.DataSource = dv;
                    ddf.DataTextField = "value";
                    ddf.DataBind();
                    ddf.Items.Insert(0, new ListItem("--Select--", "0"));
                }
            }
        }
        private void FirstListViewRow()
        {
            DataTable dt = new DataTable();
            DataRow dr = null;
            dt.Columns.Add(new DataColumn("OrderNo", typeof(string)));
            dt.Columns.Add(new DataColumn("ColumnTitle", typeof(string)));
            dt.Columns.Add(new DataColumn("Datatype", typeof(string)));
            dt.Columns.Add(new DataColumn("Examples", typeof(string)));
            dt.Columns.Add(new DataColumn("Options", typeof(string)));
            dt.Columns.Add(new DataColumn("Delete", typeof(string)));
            dr = dt.NewRow();
            dt.Rows.Add(dr);
            dr["OrderNo"] = 1;
            Session["CurrentTable"] = dt;
            listview1.DataSource = dt;
            listview1.DataBind();
        }
       
        private void AddNewRow()
        {
            int rowIndex = 0;
            if (Session["CurrentTable"] != null)
            {
                DataTable dtCurrentTable = (DataTable)Session["CurrentTable"];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        Label TextBoxorder = (Label)listview1.Items[rowIndex].FindControl("txtorder");
                        TextBox TextBoxcolumnname = (TextBox)listview1.Items[rowIndex].FindControl("txtcolumnname");
                        DropDownList DropDatatype = (DropDownList)listview1.Items[rowIndex].FindControl("ddldatatype");
                        DropDownList Dropexample = (DropDownList)listview1.Items[rowIndex].FindControl("ddlexamples");
                        TextBox TextBoxoptions = (TextBox)listview1.Items[rowIndex].FindControl("txtoptions");
                        CheckBox Checkdel = (CheckBox)listview1.Items[rowIndex].FindControl("chkdel");

                        drCurrentRow = dtCurrentTable.NewRow();
                        drCurrentRow["OrderNo"] = i + 1;

                        // dtCurrentTable.Rows[i - 1]["Order"] = TextBoxorder.Text;
                        dtCurrentTable.Rows[i - 1]["ColumnTitle"] = TextBoxcolumnname.Text;
                        dtCurrentTable.Rows[i - 1]["Datatype"] = DropDatatype.Text;
                        dtCurrentTable.Rows[i - 1]["Examples"] = Dropexample.Text;
                        dtCurrentTable.Rows[i - 1]["Options"] = TextBoxoptions.Text;
                        dtCurrentTable.Rows[i - 1]["Delete"] = Checkdel.Text;

                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    Session["CurrentTable"] = dtCurrentTable;
                    listview1.DataSource = dtCurrentTable;
                    listview1.DataBind();
                    Label txn = (Label)listview1.Items[rowIndex].FindControl("txtorder");
                    txn.Focus();
                }
            }
            else
            {
                Response.Write("Session is null");
            }
            BindDataToGridviewDropdownlist();
        }
        protected void btnGenerate_Click(object sender, EventArgs e)
        {


        }
        protected void btnAdd_Click(object sender, EventArgs e)
        {
            AddNewRow();
        }

        protected void btndelete_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            if (Session["CurrentTable"] != null)
            {
                dt = (DataTable)Session["CurrentTable"];
                int j = 0;
                for (int i = 0; i < listview1.Items.Count; i++)
                {
                    ListViewDataItem items = listview1.Items[i];
                    CheckBox chkBox = (CheckBox)items.FindControl("chkdel");

                    if (chkBox.Checked == true)
                    {
                        dt.Rows.RemoveAt(j);
                        dt.AcceptChanges();
                    }
                    else
                    {
                        j++;
                    }
                }
                Session["CurrentTable"] = dt;
                listview1.DataSource = dt;
                listview1.DataBind();
                BindDataToGridviewDropdownlist();
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            listview1.Items.Clear();
        }
    }
}

推荐答案

嘿那里,



我仍​​然不确定你的问题,但这就是我认为的。



你想要列的DropDownLists来自GridView绑定的DataTable的数据类型列的选定值。



你可以做两个修改并可以这样选择:

在此ListView中添加属性 DataKeyNames ,如下所示:
Hey there,

I am not still sure about your problem, but here is what I think it is.

You want the DropDownLists of the column have a selected value from the Datatype Column of the DataTable that the GridView is binding from.

You can do two modifications and can make the selection this way:
Add a property DataKeyNames in this ListView like this:
<asp:listview id="listview1" runat="server" datakeynames="Datatype" xmlns:asp="#unknown"></asp:listview>

并在 BindDataToGridviewDropdownlist 方法:

and add these lines inside BindDataToGridviewDropdownlist method:

ListViewDataItem di = (ListViewDataItem)list;
                    string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
                    ddf.SelectedValue = dataType;



这里是你如何 BindDataToGridviewDropdownlist 方法看起来像:

protected void BindDataToGridviewDropdownlist()
       {
           DataSet dsDept = new DataSet();
           dsDept.ReadXml(Server.MapPath("XMLFile2.xml"));
           DataView dv = dsDept.Tables[0].DefaultView;
           foreach (var list in listview1.Items)
           {
               if (list.ItemType == ListViewItemType.DataItem)
               {
                   DropDownList ddf = (DropDownList)list.FindControl("ddldatatype");
                   ddf.DataSource = dv;
                   ddf.DataTextField = "value";
                   ddf.DataBind();
                   ddf.Items.Insert(0, new ListItem("--Select--", "0"));

                   ListViewDataItem di = (ListViewDataItem)list;
                   string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
                   ddf.SelectedValue = dataType;
               }

           }
       }





如果我知道是否它是否有效,最重要的是这就是你想要的。



Azee ...



Let me know if it works or not, and most importantly this is what you wanted.

Azee...


这篇关于向listview添加新行将删除上面选定的下拉列表清除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆