i m trying to bind dropdown with previus selected selected value inside gried view.
after adding new row dynamically to griedview m not getting selected value of dropdown
protected void ButtonAdd_Click(object sender, EventArgs e)
{
foreach (GridViewRow gr in GrvSalesEntry.Rows)
{
Label Lblproduct = new Label();
Lblproduct = (Label)gr.Cells[2].FindControl("Lblproductcode");
if (Lblproduct.Visible == false)
{
AddNewRowToGrid();
//TextBox ddl = (TextBox)GrvSalesEntry.Rows[GrvSalesEntry.Rows.Count - 1].FindControl("txtCompanyName");
DropDownList ddl = (DropDownList)GrvSalesEntry.Rows[GrvSalesEntry.Rows.Count - 1].FindControl("DropDownListCompany");
ddl.Focus();
break;
}
else
{
}
}
}
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = dtCurrentTable.Rows.Count + 1;
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState for future reference
ViewState["CurrentTable"] = dtCurrentTable;
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
//extract the DropDownList Selected Items
//TextBox ddl1 = (TextBox)GrvSalesEntry.Rows[i].Cells[1].FindControl("txtCompanyName");
//TextBox ddl2 = (TextBox)GrvSalesEntry.Rows[i].Cells[2].FindControl("txtCategoryName");
//TextBox ddl3 = (TextBox)GrvSalesEntry.Rows[i].Cells[3].FindControl("txtProductName");
DropDownList ddl1 = (DropDownList)GrvSalesEntry.Rows[i].Cells[1].FindControl("DropDownListCompany");
DropDownList ddl2 = (DropDownList)GrvSalesEntry.Rows[i].Cells[2].FindControl("DropDownListCategoryname");
DropDownList ddl3 = (DropDownList)GrvSalesEntry.Rows[i].Cells[3].FindControl("DropDownListProduct");
// Update the DataRow with the DDL Selected Items
dtCurrentTable.Rows[i]["Column1"] = ddl1.SelectedValue;
dtCurrentTable.Rows[i]["Column2"] = ddl2.SelectedValue;
dtCurrentTable.Rows[i]["Column3"] = ddl3.SelectedValue;
//extract the TextBox values
TextBox box1 = (TextBox)GrvSalesEntry.Rows[i].Cells[4].FindControl("txtProQty");
TextBox box2 = (TextBox)GrvSalesEntry.Rows[i].Cells[5].FindControl("txtRroRate");
TextBox box3 = (TextBox)GrvSalesEntry.Rows[i].Cells[6].FindControl("txttotAmt");
dtCurrentTable.Rows[i]["Column4"] = box1.Text;
dtCurrentTable.Rows[i]["Column5"] = box2.Text;
dtCurrentTable.Rows[i]["Column6"] = box3.Text;
}
enableField();
//Rebind the Grid with the current data to reflect changes
GrvSalesEntry.DataSource = dtCurrentTable;
GrvSalesEntry.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
// SetPreviousData();
}