I want to Dynamically adding and deleting rows from ASP.NET GridView like this link
https://www.codeproject.com/Articles/467788/Dynamically-adding-and-deleting-rows-from-ASP-NET
I have designed the same page but my requirement is bit different. in the column of shedName
I want to get the list of ShedName from database instead of Textbox.
I am using Eval but it is showing me an error like this
dddlPenE has a SelectedValue which is invalid becase it does not exist in the list of items.
Parameter name: Value
how to get solution pls?
private void FirstGridViewRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Col1", typeof(string)));
dt.Columns.Add(new DataColumn("Col2", typeof(string)));
dt.Columns.Add(new DataColumn("Col3", typeof(string)));
dt.Columns.Add(new DataColumn("Col4", typeof(string)));
dt.Columns.Add(new DataColumn("Col5", typeof(string)));
dt.Columns.Add(new DataColumn("Col6", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Col1"] = string.Empty;
//dr["Col2"] = string.Empty;
dr["Col2"] = string.Empty;
dr["Col3"] = string.Empty;
dr["Col4"] = string.Empty;
dr["Col5"] = string.Empty;
dr["Col6"] = string.Empty;
dt.Rows.Add(dr);
ViewState["CurrentTable"] = dt;
grvStudentDetails.DataSource = dt;
grvStudentDetails.DataBind();
TextBox txn = (TextBox)grvStudentDetails.Rows[0].Cells[1].FindControl("txtTag");
txn.Focus();
Button btnAdd = (Button)grvStudentDetails.FooterRow.Cells[5].FindControl("ButtonAdd");
Page.Form.DefaultFocus = btnAdd.ClientID;
}
private void AddNewRow()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//TextBox TextBoxName = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox TextBoxTag = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtTag");
DropDownList DrpPen = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("ddlPenE");
TextBox TextBoxWeight = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtWeight");
RadioButtonList RBLGender = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
TextBox TextBoxIden = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("txtIden");
DropDownList DrpBread = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[6].FindControl("drpBread");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
//dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxTag.Text;
dtCurrentTable.Rows[i - 1]["Col2"] = DrpPen.SelectedValue.ToString();
dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxWeight.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = RBLGender.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxWeight.Text;
dtCurrentTable.Rows[i - 1]["Col6"] = DrpBread.SelectedValue;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grvStudentDetails.DataSource = dtCurrentTable;
grvStudentDetails.DataBind();
TextBox txn = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtTag");
txn.Focus();
// txn.Focus;
}
}
else
{
Response.Write("ViewState is null");
}
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
//TextBox TextBoxName = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox TextBoxTag = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtTag");
DropDownList DrpPen = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("ddlPenE");
TextBox TextBoxWeight = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtWeight");
RadioButtonList RBLGender = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
TextBox TextBoxIden = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("txtIden");
DropDownList DrpBread = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[6].FindControl("drpBread");
// drCurrentRow["RowNumber"] = i + 1;
grvStudentDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
//TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
TextBoxTag.Text = dt.Rows[i]["Col1"].ToString();
DrpPen.SelectedValue = dt.Rows[i]["Col2"].ToString();
TextBoxWeight.Text = dt.Rows[i]["Col3"].ToString();
RBLGender.SelectedValue = dt.Rows[i]["Col4"].ToString();
TextBoxIden.Text = dt.Rows[i]["Col5"].ToString();
DrpBread.SelectedValue = dt.Rows[i]["Col6"].ToString();
rowIndex++;
}
}
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRow();
}
protected void grvStudentDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SetRowData();
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
int rowIndex = Convert.ToInt32(e.RowIndex);
if (dt.Rows.Count > 1)
{
dt.Rows.Remove(dt.Rows[rowIndex]);
drCurrentRow = dt.NewRow();
ViewState["CurrentTable"] = dt;
grvStudentDetails.DataSource = dt;
grvStudentDetails.DataBind();
for (int i = 0; i < grvStudentDetails.Rows.Count - 1; i++)
{
grvStudentDetails.Rows[i].Cells[0].Text = Convert.ToString(i + 1);
}
SetPreviousData();
}
}
}
private void SetRowData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
//TextBox TextBoxName = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtName");
TextBox TextBoxTag = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[1].FindControl("txtTag");
DropDownList DrpPen = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("ddlPenE");
TextBox TextBoxWeight = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtWeight");
RadioButtonList RBLGender = (RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
TextBox TextBoxIden = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("txtIden");
DropDownList DrpBread = (DropDownList)grvStudentDetails.Rows[rowIndex].Cells[6].FindControl("drpBread");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
//dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxName.Text;
dtCurrentTable.Rows[i - 1]["Col1"] = TextBoxTag.Text;
dtCurrentTable.Rows[i - 1]["Col2"] = DrpPen.SelectedValue.ToString();
dtCurrentTable.Rows[i - 1]["Col3"] = TextBoxWeight.Text;
dtCurrentTable.Rows[i - 1]["Col4"] = RBLGender.SelectedValue;
dtCurrentTable.Rows[i - 1]["Col5"] = TextBoxIden.Text;
dtCurrentTable.Rows[i - 1]["Col6"] = DrpBread.SelectedValue;
rowIndex++;
}
ViewState["CurrentTable"] = dtCurrentTable;
//grvStudentDetails.DataSource = dtCurrentTable;
//grvStudentDetails.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//SetPreviousData();
}
<asp:TemplateField HeaderText="Shed Name">
<ItemTemplate>
<asp:DropDownList ID="ddlPenE" runat="server" Text='<%# Eval("Name") %>' class="form-control" Width="150">
<asp:ListItem>Select</asp:ListItem>
</asp:DropDownList>
<asp:RequiredFieldValidator ID="RequiredFieldValidator6" runat="server" ControlToValidate="ddlPenE"
ErrorMessage="*" InitialValue="Select"></asp:RequiredFieldValidator>
</ItemTemplate>
</asp:TemplateField>