For example in header3 drodpwonlist i select item1 then after i cilck add new row in new row also it will remian same value item1in header3 i want previous value to come in new added row
Accroding to your description,I don't understand your requirment clearly.I have created a test and when I click the "add row" button,it will add a new dropdownlist with "select".Just like this:
Could you post your codes and sample to us? It will help us to solve your problems.
Best regards,
Yijing Sun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Use gridview databound event to get last value, then using findcontrol get the ddl from the headerrow. Select the item you want
Or use session variabel or viewstate variable, set the value while inserting data. Then like above way set selected ddl from the variable in databound event
Programming to simplify, don't look for difficult way
Suwandi - Non Graduate Programmer
private ArrayList GetDummyData()
{
ArrayList arr = new ArrayList();
arr.Add(new ListItem("Item1", "1"));
arr.Add(new ListItem("Item2", "2"));
arr.Add(new ListItem("Item3", "3"));
arr.Add(new ListItem("Item4", "4"));
arr.Add(new ListItem("Item5", "5"));
return arr;
}
private void FillDropDownList(DropDownList ddl)
{
ArrayList arr = GetDummyData();
foreach (ListItem item in arr)
{
ddl.Items.Add(item);
}
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));//for TextBox value
dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for TextBox value
dt.Columns.Add(new DataColumn("Column3", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("Column4", typeof(string)));//for DropDownList selected item
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Bind the Gridview
Gridview1.DataSource = dt;
Gridview1.DataBind();
//After binding the gridview, we can then extract and fill the DropDownList with Data
DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[4].FindControl("DropDownList2");
FillDropDownList(ddl1);
FillDropDownList(ddl2);
}
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 TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
dtCurrentTable.Rows[i]["Column1"] = box1.Text;
dtCurrentTable.Rows[i]["Column2"] = box2.Text;
//extract the DropDownList Selected Items
DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[4].FindControl("DropDownList2");
dtCurrentTable.Rows[i]["Column3"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows[i]["Column4"] = ddl2.SelectedItem.Text;
// Update the DataRow with the DDL Selected Items
}
//Rebind the Grid with the current data to reflect changes
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
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 box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2");
DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList1");
DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("DropDownList2");
//Fill the DropDownList with Data
FillDropDownList(ddl1);
FillDropDownList(ddl2);
if (i < dt.Rows.Count - 1)
{
//Assign the value from DataTable to the TextBox
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
//Set the Previous Selected Items on Each DropDownList on Postbacks
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true;
ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows[i]["Column4"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
if (lb != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
lb.Visible = false;
}
}
else
{
lb.Visible = false;
}
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data and reset row number
dt.Rows.Remove(dt.Rows[rowID]);
ResetRowID(dt);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void ResetRowID(DataTable dt)
{
int rowNumber = 1;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
row[0] = rowNumber;
rowNumber++;
}
}
}
protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var dropdownList = e.Row.FindControl("DropDownList1") as DropDownList;
if (e.Row.RowIndex != 0)
{
DataTable dtAgents = (DataTable)ViewState["CurrentTable"];
for (int i = 0; i < dtAgents.Rows.Count; i++)
{
if (i == 0)
{
var predropdownList = dtAgents.Rows[i]["Column3"].ToString();
dropdownList.SelectedItem.Text = predropdownList;
}
else
{
var predropdownList = dtAgents.Rows[i - 1]["Column3"].ToString();
dropdownList.SelectedItem.Text = predropdownList;
}
}
}
}
}
Result:
Best regards,
Yijing Sun
.NET forums are moving to a new home on Microsoft Q&A, we encourage you to go to Microsoft Q&A for .NET for posting new questions and get involved today.
Member
289 Points
664 Posts
Retain last value of add new row item
Dec 27, 2020 07:31 PM|Gopi.MCA|LINK
Hello
I used this in my page
https://www.c-sharpcorner.com/UploadFile/8c19e8/dynamically-adding-and-deleting-rows-in-gridview-and-saving/
For example in header3 drodpwonlist i select item1 then after i cilck add new row in new row also it will remian same value item1in header3 i want previous value to come in new added row
thanking you
Contributor
3360 Points
1279 Posts
Re: Retain last value of add new row item
Dec 28, 2020 02:45 AM|yij sun|LINK
Hi Gopi.MCA,
Accroding to your description,I don't understand your requirment clearly.I have created a test and when I click the "add row" button,it will add a new dropdownlist with "select".Just like this:
Could you post your codes and sample to us? It will help us to solve your problems.
Best regards,
Yijing Sun
Member
289 Points
664 Posts
Re: Retain last value of add new row item
Dec 28, 2020 05:19 AM|Gopi.MCA|LINK
Hello in hearer3 rownumber 1 you have selected item1 on add new row also I want to show item1 only not select..
Whenever add new row button click then last value to be store in header3 column drop-down
All-Star
52503 Points
15665 Posts
Re: Retain last value of add new row item
Dec 28, 2020 01:46 PM|oned_gk|LINK
Use gridview databound event to get last value, then using findcontrol get the ddl from the headerrow. Select the item you want
Or use session variabel or viewstate variable, set the value while inserting data. Then like above way set selected ddl from the variable in databound event
Suwandi - Non Graduate Programmer
Contributor
3360 Points
1279 Posts
Re: Retain last value of add new row item
Dec 29, 2020 03:55 AM|yij sun|LINK
Hi Gopi.MCA,
Accroding to your description,as far as I think,you could viewstate datatable value in rowdatabound event.
Just like this:
Code-behind:
private ArrayList GetDummyData() { ArrayList arr = new ArrayList(); arr.Add(new ListItem("Item1", "1")); arr.Add(new ListItem("Item2", "2")); arr.Add(new ListItem("Item3", "3")); arr.Add(new ListItem("Item4", "4")); arr.Add(new ListItem("Item5", "5")); return arr; } private void FillDropDownList(DropDownList ddl) { ArrayList arr = GetDummyData(); foreach (ListItem item in arr) { ddl.Items.Add(item); } } private void SetInitialRow() { DataTable dt = new DataTable(); DataRow dr = null; dt.Columns.Add(new DataColumn("RowNumber", typeof(string))); dt.Columns.Add(new DataColumn("Column1", typeof(string)));//for TextBox value dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for TextBox value dt.Columns.Add(new DataColumn("Column3", typeof(string)));//for DropDownList selected item dt.Columns.Add(new DataColumn("Column4", typeof(string)));//for DropDownList selected item dr = dt.NewRow(); dr["RowNumber"] = 1; dr["Column1"] = string.Empty; dr["Column2"] = string.Empty; dt.Rows.Add(dr); //Store the DataTable in ViewState for future reference ViewState["CurrentTable"] = dt; //Bind the Gridview Gridview1.DataSource = dt; Gridview1.DataBind(); //After binding the gridview, we can then extract and fill the DropDownList with Data DropDownList ddl1 = (DropDownList)Gridview1.Rows[0].Cells[3].FindControl("DropDownList1"); DropDownList ddl2 = (DropDownList)Gridview1.Rows[0].Cells[4].FindControl("DropDownList2"); FillDropDownList(ddl1); FillDropDownList(ddl2); } 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 TextBox values TextBox box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1"); TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2"); dtCurrentTable.Rows[i]["Column1"] = box1.Text; dtCurrentTable.Rows[i]["Column2"] = box2.Text; //extract the DropDownList Selected Items DropDownList ddl1 = (DropDownList)Gridview1.Rows[i].Cells[3].FindControl("DropDownList1"); DropDownList ddl2 = (DropDownList)Gridview1.Rows[i].Cells[4].FindControl("DropDownList2"); dtCurrentTable.Rows[i]["Column3"] = ddl1.SelectedItem.Text; dtCurrentTable.Rows[i]["Column4"] = ddl2.SelectedItem.Text; // Update the DataRow with the DDL Selected Items } //Rebind the Grid with the current data to reflect changes Gridview1.DataSource = dtCurrentTable; Gridview1.DataBind(); } } else { Response.Write("ViewState is null"); } //Set Previous Data on Postbacks 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 box1 = (TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1"); TextBox box2 = (TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox2"); DropDownList ddl1 = (DropDownList)Gridview1.Rows[rowIndex].Cells[3].FindControl("DropDownList1"); DropDownList ddl2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[4].FindControl("DropDownList2"); //Fill the DropDownList with Data FillDropDownList(ddl1); FillDropDownList(ddl2); if (i < dt.Rows.Count - 1) { //Assign the value from DataTable to the TextBox box1.Text = dt.Rows[i]["Column1"].ToString(); box2.Text = dt.Rows[i]["Column2"].ToString(); //Set the Previous Selected Items on Each DropDownList on Postbacks ddl1.ClearSelection(); ddl1.Items.FindByText(dt.Rows[i]["Column3"].ToString()).Selected = true; ddl2.ClearSelection(); ddl2.Items.FindByText(dt.Rows[i]["Column4"].ToString()).Selected = true; } rowIndex++; } } } } protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { SetInitialRow(); } } protected void ButtonAdd_Click(object sender, EventArgs e) { AddNewRowToGrid(); } protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DataTable dt = (DataTable)ViewState["CurrentTable"]; LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1"); if (lb != null) { if (dt.Rows.Count > 1) { if (e.Row.RowIndex == dt.Rows.Count - 1) { lb.Visible = false; } } else { lb.Visible = false; } } } } protected void LinkButton1_Click(object sender, EventArgs e) { LinkButton lb = (LinkButton)sender; GridViewRow gvRow = (GridViewRow)lb.NamingContainer; int rowID = gvRow.RowIndex; if (ViewState["CurrentTable"] != null) { DataTable dt = (DataTable)ViewState["CurrentTable"]; if (dt.Rows.Count > 1) { if (gvRow.RowIndex < dt.Rows.Count - 1) { //Remove the Selected Row data and reset row number dt.Rows.Remove(dt.Rows[rowID]); ResetRowID(dt); } } //Store the current data in ViewState for future reference ViewState["CurrentTable"] = dt; //Re bind the GridView for the updated data Gridview1.DataSource = dt; Gridview1.DataBind(); } //Set Previous Data on Postbacks SetPreviousData(); } private void ResetRowID(DataTable dt) { int rowNumber = 1; if (dt.Rows.Count > 0) { foreach (DataRow row in dt.Rows) { row[0] = rowNumber; rowNumber++; } } } protected void Gridview1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { var dropdownList = e.Row.FindControl("DropDownList1") as DropDownList; if (e.Row.RowIndex != 0) { DataTable dtAgents = (DataTable)ViewState["CurrentTable"]; for (int i = 0; i < dtAgents.Rows.Count; i++) { if (i == 0) { var predropdownList = dtAgents.Rows[i]["Column3"].ToString(); dropdownList.SelectedItem.Text = predropdownList; } else { var predropdownList = dtAgents.Rows[i - 1]["Column3"].ToString(); dropdownList.SelectedItem.Text = predropdownList; } } } } }
Result:
Best regards,
Yijing Sun