Click here to Skip to main content
15,902,032 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
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();
        }
    }
}
Posted
Updated 2-Oct-13 19:39pm
v2
Comments
karthik mushyam 3-Oct-13 1:35am    
When i add the new row to the List view above selected dropdownlist gets cleared .what is the reason for this problem plz suggest me
Azee 3-Oct-13 1:41am    
Is the DropDownList that is clearing, inside the ListView?
karthik mushyam 3-Oct-13 1:44am    
listview has dropdownlist column ..when i select dropdownlist value and add new row to the listview ..above selected dropgown gets cleared this is the exact problem with this
Azee 3-Oct-13 1:50am    
You are binding the ListView in your AddRow method,
listview1.DataSource = dtCurrentTable;
listview1.DataBind();

It will reset all data inside the ListView, hence reseting the DropDownLists.

Do you want to keep the state of that dropdownlist?
karthik mushyam 3-Oct-13 1:52am    
yes

1 solution

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.NET
<asp:listview id="listview1" runat="server" datakeynames="Datatype" xmlns:asp="#unknown"></asp:listview>
and add these lines inside BindDataToGridviewDropdownlist method:
C#
ListViewDataItem di = (ListViewDataItem)list;
                    string dataType = listview1.DataKeys[di.DisplayIndex].Values[0].ToString();
                    ddf.SelectedValue = dataType;

here is how you BindDataToGridviewDropdownlist method would look like:
C#
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;
               }

           }
       }


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

Azee...
 
Share this answer
 
Comments
karthik mushyam 3-Oct-13 2:28am    
Thanq u very mush Azee ...It works for my as i need ...
Azee 3-Oct-13 2:30am    
Great, cheers :)
karthik mushyam 3-Oct-13 2:33am    
:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900