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
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();
}
}
}