How to Add Dynamic Text Box inside child gridview
?Below screen shot will give clear picture of my question.I am unable to add on second row which is shown in screen shot.
Screen shot
Thank you in advance
Adding Dynamic Rows in GridView with TextBoxes
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true"
AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Your Binding Code
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)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
Page Load Event Code
if (!Page.IsPostBack){
SetInitialRow();
}
Running the codes above will give us this output below:
method for generating the rows when clicking the Button
private void AddNewRowToGrid(){
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
drCurrentRow["Column3"] = box3.Text;
rowIndex++;
}
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
Setting up previous Data
private void SetPreviousData(){
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 1; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
Button click event Code
protected void ButtonAdd_Click(object sender, EventArgs e){
AddNewRowToGrid();
}
For Detail Tutorial Visit : geekswithblock
Related
The problem is initially I load the page with gridview having 1 row with each columns having a textbox. When I need to retrieve, I need the gridview to have the no of rows present same like DB such that it worked. But the gridview is not loaded with the datas...
The code I worked on....
int rowIndex = 0;
ViewState["CurrentTable"] = dt.Tables[1];
LoadGridViewfromDB();
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
if (dtCurrentTable.Rows.Count > 0)
{
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
System.Web.UI.WebControls.TextBox combobox = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[0].FindControl("combo");
System.Web.UI.WebControls.TextBox sizes = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[1].FindControl("sizes");
System.Web.UI.WebControls.TextBox barcodenumber = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[2].FindControl("barcodeno");
System.Web.UI.WebControls.TextBox ratio = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[3].FindControl("ratio");
System.Web.UI.WebControls.TextBox qty = (System.Web.UI.WebControls.TextBox)gvmonoprixmasters.Rows[rowIndex].Cells[4].FindControl("qty");
combobox.Text = dt.Tables[1].Rows[i - 1]["Combo1"].ToString();
sizes.Text = dt.Tables[1].Rows[i - 1]["Sizes"].ToString();
barcodenumber.Text = dt.Tables[1].Rows[i - 1]["Barcode_Number"].ToString();
ratio.Text = dt.Tables[1].Rows[i - 1]["Ratio"].ToString();
qty.Text = dt.Tables[1].Rows[i - 1]["Quantity"].ToString();
rowIndex++;
}
gvmonoprixmasters.DataSource = dtCurrentTable;
gvmonoprixmasters.DataBind();
}
}
protected void LoadGridViewfromDB()
{
try
{
DataTable dtCurrentTable =(DataTable)ViewState["CurrentTable"];
if (ViewState["CurrentTable"] != null)
{
DataTable dt = new DataTable();
DataRow dr = null;
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)));
for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
{
dr = dt.NewRow();
dr["Col1"] = dtCurrentTable.Rows[i - 1]["Combo1"].ToString();
dr["Col2"] = dtCurrentTable.Rows[i - 1]["Sizes"].ToString();
dr["Col3"] = dtCurrentTable.Rows[i - 1]["Barcode_Number"].ToString();
dr["Col4"] = dtCurrentTable.Rows[i - 1]["Ratio"].ToString();
dr["Col5"] = dtCurrentTable.Rows[i - 1]["Quantity"].ToString();
dt.Rows.Add(dr);
}
ViewState["CurrentTable"] = dt;
gvmonoprixmasters.DataSource = dt;
gvmonoprixmasters.DataBind();
}
else
{
Response.Write("ViewState is null");
}
}
catch (Exception ee) { }
}
In Aspx page
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField HeaderText="COl1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col1") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col2">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col2") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Col3">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("col3") %>'></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
In aspx.cs page use
GridView1.DataSource = dt.Tables[1];
GridView1.DataBind();
Im trying to add a new row in grid with dropdown as one column in it. Now im trying to bind data to the drop down when the new row is added but the data doesnt show up. Can anyone tell me whats the problem with my code?
<asp:GridView ID="grdJournal" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:TextBox ID="lblDate" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Account">
<ItemTemplate>
<asp:DropDownList ID="ddlAccounts" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description">
<ItemTemplate>
<asp:TextBox ID="lblDescription" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Debit">
<ItemTemplate>
<asp:TextBox ID="lblDebit" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Credit">
<ItemTemplate>
<asp:TextBox ID="lblCredit" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
and my codebehind is this
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("colDate", typeof(string)));
dt.Columns.Add(new DataColumn("colAccount", typeof(string)));
dt.Columns.Add(new DataColumn("colDescription", typeof(string)));
dt.Columns.Add(new DataColumn("colDebit", typeof(string)));
dt.Columns.Add(new DataColumn("colCredit", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["colDate"] = string.Empty;
dr["colAccount"] = string.Empty;
dr["colDescription"] = string.Empty;
dr["colDebit"] = string.Empty;
dr["colCredit"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
grdJournal.DataSource = dt;
grdJournal.DataBind();
}
private void AddNewRowToGrid()
{
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++)
{
//extract the TextBox values
TextBox lblDate = (TextBox)grdJournal.Rows[rowIndex].Cells[1].FindControl("lblDate");
DropDownList lblAccount = (DropDownList)grdJournal.Rows[rowIndex].Cells[2].FindControl("ddlAccounts");
TextBox lblDescription = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDescription");
TextBox lblDebit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDebit");
TextBox lblCredit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblCredit");
Classes.CRUD obj = new Classes.CRUD();
lblAccount.DataSource = obj.ShowAllAccounts();
lblAccount.DataTextField = "Key";
lblAccount.DataValueField = "Value";
lblAccount.DataBind();
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
//Setting Values to new Row
dtCurrentTable.Rows[i - 1]["colDate"] = lblDate.Text;
dtCurrentTable.Rows[i - 1]["colAccount"] = lblAccount.SelectedItem.Text;
dtCurrentTable.Rows[i - 1]["colDescription"] = lblDescription.Text;
dtCurrentTable.Rows[i - 1]["colDebit"] = lblDebit.Text;
dtCurrentTable.Rows[i - 1]["colCredit"] = lblCredit.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
grdJournal.DataSource = dtCurrentTable;
grdJournal.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 lblDate = (TextBox)grdJournal.Rows[rowIndex].Cells[1].FindControl("lblDate");
DropDownList lblAccount = (DropDownList)grdJournal.Rows[rowIndex].Cells[2].FindControl("ddlAccounts");
TextBox lblDescription = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDescription");
TextBox lblDebit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblDebit");
TextBox lblCredit = (TextBox)grdJournal.Rows[rowIndex].Cells[3].FindControl("lblCredit");
lblDate.Text = dt.Rows[i]["colDate"].ToString();
// lblAccount.Text = dt.Rows[i]["colAccount"].ToString();
lblDescription.Text = dt.Rows[i]["colDescription"].ToString();
lblDebit.Text = dt.Rows[i]["colDebit"].ToString();
lblCredit.Text = dt.Rows[i]["colCredit"].ToString();
rowIndex++;
}
}
}
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void Page_Load(object sender, EventArgs e)
{
Classes.CRUD obj = new Classes.CRUD();
#region Binding Events
if (!Page.IsPostBack)
{
SetInitialRow();
ddlType.DataTextField = "Value";
ddlType.DataValueField = "Key";
ddlType.DataSource = obj.ShowTypesOfTransactions();
ddlType.DataBind();
}
#endregion
}
Solved it.
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 TextBoxAge = (TextBox)grvStudentDetails.Rows[rowIndex].Cells[2].FindControl("txtAge");
TextBox TextBoxAddress =
(TextBox)grvStudentDetails.Rows[rowIndex].Cells[3].FindControl("txtAddress");
RadioButtonList RBLGender =
(RadioButtonList)grvStudentDetails.Rows[rowIndex].Cells[4].FindControl("RBLGender");
DropDownList DrpQualification =
(DropDownList)grvStudentDetails.Rows[rowIndex].Cells[5].FindControl("drpQualification");
//Added these lines
Classes.CRUD obj = new Classes.CRUD();
DrpQualification.DataSource = obj.ShowAllAccounts();
DrpQualification.DataBind();
//****************
TextBoxName.Text = dt.Rows[i]["Col1"].ToString();
TextBoxAge.Text = dt.Rows[i]["Col2"].ToString();
TextBoxAddress.Text = dt.Rows[i]["Col3"].ToString();
RBLGender.SelectedValue = dt.Rows[i]["Col4"].ToString();
DrpQualification.SelectedValue = dt.Rows[i]["Col5"].ToString();
rowIndex++;
}
}
}
}
I have a dynamic gridview ...and it has four columns..what I want is to access the textbox value in the gridview from the last column "amount paid" and display its total sum value in a label.Below is the code that I have tried. can someone let me know how to do this?
ASP.NET
<asp:gridview ID="Gridview2" runat="server" ShowFooter="true" CssClass="vutblrow"
TabIndex="3" HeaderStyle-CssClass="vutblhdr"
CellPadding="4" ForeColor="#333333" GridLines="None" Width="1%"
PagerStyle-Mode="NumericPages"
AutoGenerateColumns="false" onrowcreated="Gridview2_RowCreated" Height="16px">
<PagerStyle CssClass="pgr" Height="25px" BorderStyle="Solid" />
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Serial Number" />
<asp:TemplateField HeaderText="From Place">
<ItemTemplate>
<asp:TextBox ID="Textfrom" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="To Place">
<ItemTemplate>
<asp:TextBox ID="Textto" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Distance Travelled (kms)">
<ItemTemplate>
<asp:TextBox ID="TextBoxdist" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Amount Paid (per km)">
<ItemTemplate>
<asp:TextBox ID="TextBoxamt" runat="server" CssClass="txtBoxNormalmedium"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd1" runat="server" Text="Add New Row"
CssClass="btnNormalAdd" OnClick="add" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
CssClass="lnkbut" OnClick="LinkButton2_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
<asp:Label ID="lblTotal" runat="server" Text="Label"></asp:Label>
C# code:
namespace Test.Test
{
public partial class WebForm6 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetInitialRow1();
}
}
private void AddNewRowToGrid1()
{
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview2.Rows[rowIndex].Cells[1].FindControl("Textfrom");
TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("Textto");
TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxdist");
TextBox box4 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxamt");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
dtCurrentTable.Rows[i - 1]["Column4"] = box4.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview2.DataSource = dtCurrentTable;
Gridview2.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData1();
}
private void SetPreviousData1()
{
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)Gridview2.Rows[rowIndex].Cells[1].FindControl("Textfrom");
TextBox box2 = (TextBox)Gridview2.Rows[rowIndex].Cells[2].FindControl("Textto");
TextBox box3 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxdist");
TextBox box4 = (TextBox)Gridview2.Rows[rowIndex].Cells[3].FindControl("TextBoxamt");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
rowIndex++;
}
}
}
}
protected void add(object sender, EventArgs e)
{
AddNewRowToGrid1();
}
protected void Gridview2_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 LinkButton2_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex + 1;
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
dt.Rows.Remove(dt.Rows[rowID]);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData1();
}
private void SetInitialRow1()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dr["Column4"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview2.DataSource = dt;
Gridview2.DataBind();
}
}
}
GridViewRow row = (GridViewRow)((Button)sender).NamingContainer;
TextBox TextBox1 = row.FindControl("TextBox1") as TextBox;
//Access TextBox1 here.
string myString = TextBox1.Text;
Text box is the child control inside in gridview row so you can iterate above code for each grid view row.
Could you please isolate the data source?
This will clarify your mind and the question. As soon you have a Service/Repository to query the data source you can take advantage of linq and do something simple like:
var sum = datasource.Sum(p=>p.AmountPaid);
Ok,CLICK ME !
THis is what i have right now... but i want to add a delete functionality. I have added a delete command button column and on its click event i am deleting the particular row.
But my problem is that i am unable to retain the remaining row's values in the controls.
protected void Gridview1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
DataTable dtCurrentTable=new DataTable();
int RowIndex = e.RowIndex;
dtCurrentTable = (DataTable)ViewState["CurrentTable"];
dtCurrentTable.Rows.RemoveAt(e.RowIndex);
SetPreviousData1(dtCurrentTable);
}
private void SetPreviousData1(DataTable dtCurrentTable)
{
int rowIndex = 0;
if (dtCurrentTable != null)
{
DataTable dt = dtCurrentTable;
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
Please note i am not fetching anything from database.
UPDATE(rest of my code)
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)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid()
{
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++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
dtCurrentTable.Rows[i - 1]["Column1"] = box1.Text;
dtCurrentTable.Rows[i - 1]["Column2"] = box2.Text;
dtCurrentTable.Rows[i - 1]["Column3"] = box3.Text;
rowIndex++;
}
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
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[rowIndex].Cells[1].FindControl("TextBox1");
TextBox box2 = (TextBox)Gridview1.Rows[rowIndex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("TextBox3");
box1.Text = dt.Rows[i]["Column1"].ToString();
box2.Text = dt.Rows[i]["Column2"].ToString();
box3.Text = dt.Rows[i]["Column3"].ToString();
rowIndex++;
}
}
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
ASPX
<asp:gridview ID="Gridview1" runat="server" ShowFooter="True" AutoGenerateColumns="False" OnRowDeleting="Gridview1_RowDeleting">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:CommandField DeleteText="D" HeaderText="D" ShowDeleteButton="true" />
</Columns>
</asp:gridview>
Update you gridview to following:
<asp:gridview ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row"
onclick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:LinkButton runat="server" ID="lnkDel" OnCommand="DeleteRowHandler" Text="delete"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:gridview>
Now add following code to you .cs page.
protected void DeleteRowHandler(object sender, CommandEventArgs e)
{
GridViewRow row = ((GridViewRow) ((LinkButton)sender).Parent.Parent);
DataTable dt = = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("Column1", typeof(string)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
for (int i = 0; i < Gridview1.Rows.Count; i++)
{
dr = dt.NewRow();
dr[1] = ((TextBox)Gridview1.Rows[i].Cells[1].FindControl("TextBox1")).Text;
dr[2] = ((TextBox)Gridview1.Rows[i].Cells[2].FindControl("TextBox1")).Text;
dr[3] = ((TextBox)Gridview1.Rows[i].Cells[3].FindControl("TextBox1")).Text;
dt.Rows.Add(dr);
}
dt.Rows.RemoveAt(row.RowIndex);
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
You are missing the rebinding of the data to the gridview after deleting the row.
Inside the SetPreviousData1 function, add:
GridView1.DataSource=dtCurrentTable;
GridView1.DataBind();
I'm write the code which create new row in gridview when click button. The number of rows are created by value in textbox. Ex: When i enter value 2 in textbox, of course two rows will be added, but when i clicked button again, third rows still added. Please check my code here:
ASPX
<asp:TextBox ID="txtVisitor" runat="server"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="Header 1">
<ItemTemplate>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 2">
<ItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Header 3">
<ItemTemplate>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd" runat="server" Text="Add New Row" OnClick="ButtonAdd_Click" />
</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</ContentTemplate>
</asp:UpdatePanel>
Code_Behind
protected void ButtonAdd_Click(object sender, EventArgs e)
{
int visitors = Convert.ToInt32(txtVisitor.Text);
AddNewRowToGrid(visitors);
}
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)));
dt.Columns.Add(new DataColumn("Column2", typeof(string)));
dt.Columns.Add(new DataColumn("Column3", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = 1;
dr["Column1"] = string.Empty;
dr["Column2"] = string.Empty;
dr["Column3"] = string.Empty;
dt.Rows.Add(dr);
//dr = dt.NewRow();
//Store the DataTable in ViewState
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
private void AddNewRowToGrid(int visitors)
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow;
if (dtCurrentTable.Rows.Count > 0)
{
int rowindex = 0;
for (int i = 1; i < visitors; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowindex].Cells[1].FindControl("txtDate");
TextBox box2 = (TextBox)Gridview1.Rows[rowindex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowindex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
drCurrentRow["Column3"] = box3.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
drCurrentRow = null;
rowindex++;
}
//add new row to DataTable
//Store the current data to ViewState
ViewState["CurrentTable"] = dtCurrentTable;
//Rebind the Grid with the current data
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SetInitialRow();
}
}
Here is problem #1 with your code:
for (int i = dtCurrentTable.Rows.Count; i < visitors; i++)
{
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowindex].Cells[1].FindControl("txtDate");
TextBox box2 = (TextBox)Gridview1.Rows[rowindex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowindex].Cells[3].FindControl("TextBox3");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = i + 1;
drCurrentRow["Column1"] = box1.Text;
drCurrentRow["Column2"] = box2.Text;
drCurrentRow["Column3"] = box3.Text;
dtCurrentTable.Rows.Add(drCurrentRow);
drCurrentRow = null;
rowindex++;
}
you set i to 1
replace it to row count of gridview
tostart from it
Here is problem #1 with your code:
//extract the TextBox values
TextBox box1 = (TextBox)Gridview1.Rows[rowindex].Cells[1].FindControl("txtDate");
TextBox box2 = (TextBox)Gridview1.Rows[rowindex].Cells[2].FindControl("TextBox2");
TextBox box3 = (TextBox)Gridview1.Rows[rowindex].Cells[3].FindControl("TextBox3");
This logic is flawed and not even necessary, because when you bind the grid it will iterate through the collection you supply as the DataSource and use the TemplateFields you defined in the markup to create each row.
The rows are not actually added to the GridView until is bound to a data source, a DataTable in your case, like this:
Gridview1.DataSource = dt;
Gridview1.DataBind();
Step #1 is to remove the FindControl lines from your AddNewRowToGrid method.
I am not even sure what you are trying to accomplish in your code.