Hi akhter,
Refer below sample.
When you finding control be make sure you are giving correct id of controls, your issues were not giving correct id in code.
HTML
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
OnRowCreated="Gridview1_RowCreated" OnRowDataBound="Gridview1_RowDataBound">
<Columns>
<asp:BoundField DataField="RowNumber" HeaderText="Row Number" />
<asp:TemplateField HeaderText="OrderID">
<ItemTemplate>
<asp:TextBox ID="txtIDGV" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Country">
<ItemTemplate>
<asp:Label ID="lblitemcode" runat="server" Text='<%# Eval("Descriptionitem") %>'
Visible="false" />
<asp:DropDownList ID="ddlCountry" runat="server">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Order_Qty">
<ItemTemplate>
<asp:TextBox ID="txtqty" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Weight">
<ItemTemplate>
<asp:TextBox ID="txtweigth" runat="server"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Bale_Size">
<ItemTemplate>
<asp:TextBox ID="txtbs" 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>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Style="height: 26px"
Text="Save" />
<br />
<asp:GridView ID="gvOrderTest" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="OrderNo" HeaderText="OrderNo" />
<asp:BoundField DataField="Country" HeaderText="CodItem" />
<asp:BoundField DataField="orderqty" HeaderText="orderqty" />
<asp:BoundField DataField="Weight" HeaderText="Weight" />
<asp:BoundField DataField="Size" HeaderText="Size" />
</Columns>
</asp:GridView>
Namespaces
C#
using System.Data.SqlClient;
using System.Data;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Imports System.Data
Code
C#
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("RowNumber", typeof(string)));
dt.Columns.Add(new DataColumn("OrderNo", typeof(string)));
dt.Columns.Add(new DataColumn("Descriptionitem", typeof(string)));
dt.Columns.Add(new DataColumn("orderqty", typeof(string)));
dt.Columns.Add(new DataColumn("Column4", typeof(string)));
dt.Columns.Add(new DataColumn("Column5", typeof(string)));
dr = dt.NewRow();
dr["RowNumber"] = (int)ViewState["RowNumber"];
dr["OrderNo"] = string.Empty;
dr["Descriptionitem"] = string.Empty;
dr["orderqty"] = string.Empty;
dr["Column4"] = string.Empty;
dr["Column5"] = string.Empty;
dt.Rows.Add(dr);
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++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtIDGV");
DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlCountry");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtqty");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtweigth");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtbs");
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["RowNumber"] = (int)ViewState["RowNumber"] + 1;
drCurrentRow["OrderNo"] = box1.Text;
drCurrentRow["Descriptionitem"] = box2.Text;
drCurrentRow["orderqty"] = box3.Text;
drCurrentRow["Column4"] = box4.Text;
drCurrentRow["Column5"] = box5.Text;
rowIndex++;
}
ViewState["RowNumber"] = (int)ViewState["RowNumber"] + 1;
dtCurrentTable.Rows.Add(drCurrentRow);
ViewState["CurrentTable"] = dtCurrentTable;
Gridview1.DataSource = dtCurrentTable;
Gridview1.DataBind();
}
}
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 = 1; i < dt.Rows.Count; i++)
{
TextBox box1 = (TextBox)Gridview1.Rows[rowIndex].Cells[1].FindControl("txtIDGV");
DropDownList box2 = (DropDownList)Gridview1.Rows[rowIndex].Cells[2].FindControl("ddlCountry");
TextBox box3 = (TextBox)Gridview1.Rows[rowIndex].Cells[3].FindControl("txtqty");
TextBox box4 = (TextBox)Gridview1.Rows[rowIndex].Cells[4].FindControl("txtweigth");
TextBox box5 = (TextBox)Gridview1.Rows[rowIndex].Cells[5].FindControl("txtbs");
box1.Text = dt.Rows[i]["OrderNo"].ToString();
box2.Text = dt.Rows[i]["Descriptionitem"].ToString();
box3.Text = dt.Rows[i]["orderqty"].ToString();
box4.Text = dt.Rows[i]["Column4"].ToString();
box5.Text = dt.Rows[i]["Column5"].ToString();
rowIndex++;
}
}
}
}
SqlConnection con = new SqlConnection();
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
ViewState["RowNumber"] = 1;
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
protected void LinkButton1_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)
{
dt.Rows.Remove(dt.Rows[rowID]);
}
}
ViewState["CurrentTable"] = dt;
Gridview1.DataSource = dt;
Gridview1.DataBind();
}
SetPreviousData();
}
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 Gridview1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
con = new SqlConnection(constr);
SqlCommand scmd = new SqlCommand("SELECT * FROM Customers", con);
SqlDataAdapter sda = new SqlDataAdapter(scmd);
SqlCommandBuilder builders = new SqlCommandBuilder(sda);
DataSet dss = new DataSet();
sda.Fill(dss);
DropDownList ddIC = (DropDownList)e.Row.FindControl("ddlCountry");
if (ddIC != null)
{
ddIC.DataSource = dss;
ddIC.DataValueField = "Country";
ddIC.DataTextField = "Country";
ddIC.DataBind();
con.Close();
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(constr);
SqlCommand cmd = null;
for (int i = 0; i < Gridview1.Rows.Count - 1; i++)
{
TextBox txtIDGV = Gridview1.Rows[i].FindControl("txtIDGV") as TextBox;
DropDownList ddlCountry = Gridview1.Rows[i].FindControl("ddlCountry") as DropDownList;
TextBox txtqty = Gridview1.Rows[i].FindControl("txtqty") as TextBox;
TextBox txtweigth = Gridview1.Rows[i].FindControl("txtqty") as TextBox;
TextBox txtbs = Gridview1.Rows[i].FindControl("txtqty") as TextBox;
cmd = new SqlCommand("INSERT INTO OrderTest(OrderNo, Country, orderqty,Weight ,Size) VALUES(@OrderNo, @Country, @orderqty,@Weight ,@Size)", con);
cmd.Parameters.AddWithValue("@OrderNo", txtIDGV.Text);
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedValue);
cmd.Parameters.AddWithValue("@orderqty", txtqty.Text);
cmd.Parameters.AddWithValue("@Weight", txtweigth.Text);
cmd.Parameters.AddWithValue("@Size", txtbs.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
cmd = new SqlCommand("SELECT OrderNo, Country, orderqty,Weight ,Size FROM OrderTest", con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
this.gvOrderTest.DataSource = dt;
this.gvOrderTest.DataBind();
}
VB.Net
Private constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Private Sub SetInitialRow()
Dim dt As DataTable = New DataTable()
Dim dr As DataRow = Nothing
dt.Columns.Add(New DataColumn("RowNumber", GetType(String)))
dt.Columns.Add(New DataColumn("OrderNo", GetType(String)))
dt.Columns.Add(New DataColumn("Descriptionitem", GetType(String)))
dt.Columns.Add(New DataColumn("orderqty", GetType(String)))
dt.Columns.Add(New DataColumn("Column4", GetType(String)))
dt.Columns.Add(New DataColumn("Column5", GetType(String)))
dr = dt.NewRow()
dr("RowNumber") = CInt(ViewState("RowNumber"))
dr("OrderNo") = String.Empty
dr("Descriptionitem") = String.Empty
dr("orderqty") = String.Empty
dr("Column4") = String.Empty
dr("Column5") = String.Empty
dt.Rows.Add(dr)
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End Sub
Private Sub AddNewRowToGrid()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dtCurrentTable As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dtCurrentTable.Rows.Count > 0 Then
For i As Integer = 1 To dtCurrentTable.Rows.Count
Dim box1 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("txtIDGV"), TextBox)
Dim box2 As DropDownList = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlCountry"), DropDownList)
Dim box3 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("txtqty"), TextBox)
Dim box4 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("txtweigth"), TextBox)
Dim box5 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(5).FindControl("txtbs"), TextBox)
drCurrentRow = dtCurrentTable.NewRow()
drCurrentRow("RowNumber") = CInt(ViewState("RowNumber")) + 1
drCurrentRow("OrderNo") = box1.Text
drCurrentRow("Descriptionitem") = box2.Text
drCurrentRow("orderqty") = box3.Text
drCurrentRow("Column4") = box4.Text
drCurrentRow("Column5") = box5.Text
rowIndex += 1
Next
ViewState("RowNumber") = CInt(ViewState("RowNumber")) + 1
dtCurrentTable.Rows.Add(drCurrentRow)
ViewState("CurrentTable") = dtCurrentTable
Gridview1.DataSource = dtCurrentTable
Gridview1.DataBind()
End If
Else
Response.Write("ViewState is null")
End If
SetPreviousData()
End Sub
Private Sub SetPreviousData()
Dim rowIndex As Integer = 0
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 0 Then
For i As Integer = 1 To dt.Rows.Count - 1
Dim box1 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(1).FindControl("txtIDGV"), TextBox)
Dim box2 As DropDownList = CType(Gridview1.Rows(rowIndex).Cells(2).FindControl("ddlCountry"), DropDownList)
Dim box3 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(3).FindControl("txtqty"), TextBox)
Dim box4 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(4).FindControl("txtweigth"), TextBox)
Dim box5 As TextBox = CType(Gridview1.Rows(rowIndex).Cells(5).FindControl("txtbs"), TextBox)
box1.Text = dt.Rows(i)("OrderNo").ToString()
box2.Text = dt.Rows(i)("Descriptionitem").ToString()
box3.Text = dt.Rows(i)("orderqty").ToString()
box4.Text = dt.Rows(i)("Column4").ToString()
box5.Text = dt.Rows(i)("Column5").ToString()
rowIndex += 1
Next
End If
End If
End Sub
Private con As SqlConnection = New SqlConnection()
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
ViewState("RowNumber") = 1
SetInitialRow()
End If
End Sub
Protected Sub ButtonAdd_Click(ByVal sender As Object, ByVal e As EventArgs)
AddNewRowToGrid()
End Sub
Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As EventArgs)
Dim lb As LinkButton = CType(sender, LinkButton)
Dim gvRow As GridViewRow = CType(lb.NamingContainer, GridViewRow)
Dim rowID As Integer = gvRow.RowIndex + 1
If ViewState("CurrentTable") IsNot Nothing Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
If dt.Rows.Count > 1 Then
If gvRow.RowIndex < dt.Rows.Count - 1 Then
dt.Rows.Remove(dt.Rows(rowID))
End If
End If
ViewState("CurrentTable") = dt
Gridview1.DataSource = dt
Gridview1.DataBind()
End If
SetPreviousData()
End Sub
Protected Sub Gridview1_RowCreated(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim dt As DataTable = CType(ViewState("CurrentTable"), DataTable)
Dim lb As LinkButton = CType(e.Row.FindControl("LinkButton1"), LinkButton)
If lb IsNot Nothing Then
If dt.Rows.Count > 1 Then
If e.Row.RowIndex = dt.Rows.Count - 1 Then
lb.Visible = False
End If
Else
lb.Visible = False
End If
End If
End If
End Sub
Protected Sub Gridview1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
con = New SqlConnection(constr)
Dim scmd As SqlCommand = New SqlCommand("SELECT * FROM Customers", con)
Dim sda As SqlDataAdapter = New SqlDataAdapter(scmd)
Dim builders As SqlCommandBuilder = New SqlCommandBuilder(sda)
Dim dss As DataSet = New DataSet()
sda.Fill(dss)
Dim ddIC As DropDownList = CType(e.Row.FindControl("ddlCountry"), DropDownList)
If ddIC IsNot Nothing Then
ddIC.DataSource = dss
ddIC.DataValueField = "Country"
ddIC.DataTextField = "Country"
ddIC.DataBind()
con.Close()
End If
End If
End Sub
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs)
con = New SqlConnection(constr)
Dim cmd As SqlCommand = Nothing
For i As Integer = 0 To Gridview1.Rows.Count - 1 - 1
Dim txtIDGV As TextBox = TryCast(Gridview1.Rows(i).FindControl("txtIDGV"), TextBox)
Dim ddlCountry As DropDownList = TryCast(Gridview1.Rows(i).FindControl("ddlCountry"), DropDownList)
Dim txtqty As TextBox = TryCast(Gridview1.Rows(i).FindControl("txtqty"), TextBox)
Dim txtweigth As TextBox = TryCast(Gridview1.Rows(i).FindControl("txtqty"), TextBox)
Dim txtbs As TextBox = TryCast(Gridview1.Rows(i).FindControl("txtqty"), TextBox)
cmd = New SqlCommand("INSERT INTO OrderTest(OrderNo, Country, orderqty,Weight ,Size) VALUES(@OrderNo, @Country, @orderqty,@Weight ,@Size)", con)
cmd.Parameters.AddWithValue("@OrderNo", txtIDGV.Text)
cmd.Parameters.AddWithValue("@Country", ddlCountry.SelectedValue)
cmd.Parameters.AddWithValue("@orderqty", txtqty.Text)
cmd.Parameters.AddWithValue("@Weight", txtweigth.Text)
cmd.Parameters.AddWithValue("@Size", txtbs.Text)
con.Open()
cmd.ExecuteNonQuery()
con.Close()
Next
cmd = New SqlCommand("SELECT OrderNo, Country, orderqty,Weight ,Size FROM OrderTest", con)
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
Dim dt As DataTable = New DataTable()
da.Fill(dt)
Me.gvOrderTest.DataSource = dt
Me.gvOrderTest.DataBind()
End Sub
Screenshot