As you said In GridView Contains the Temporary data and you want to save this Data to another GridView. So here i have created a sample for the Ref
Adding Dynamic Rows in ASP.NET GridView Control with TextBoxes and with Delete functionality
In this sample one Submit button is there which insert the Gridview1 temporary Data to Sql Table. On Insert function i am calling a PopulatingGridView2 function which is used for getting all Inserted from Database.
Download the project from the link and modify the following:
HTML:
<form id="form1" runat="server">
<div>
<asp:GridView ID="Gridview1" runat="server" ShowFooter="true" AutoGenerateColumns="false"
OnRowCreated="Gridview1_RowCreated">
<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>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" OnClick="LinkButton1_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Save" OnClick="Button1_Click" />
<br />
Main GridView
<asp:GridView ID="gvmainGridView" runat="server" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Column1" HeaderText="Column1" />
<asp:BoundField DataField="Column2" HeaderText="Column2" />
<asp:BoundField DataField="Column3" HeaderText="Column3" />
</Columns>
<EmptyDataTemplate>
<table cellspacing="0" rules="all" border="1" id="gvmainGridView" style="border-collapse: collapse;">
<tr>
<th scope="col">
Column1
</th>
<th scope="col">
Column2
</th>
<th scope="col">
Column3
</th>
</tr>
<tr>
<td colspan="3">
No Records Found
</td>
</tr>
</table>
</EmptyDataTemplate>
</asp:GridView>
</div>
</form>
Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
SetInitialRow();
this.PopulatemainGridView();
}
}
InsertRecord function:
private void InsertRecords(StringCollection sc)
{
SqlConnection conn = new SqlConnection(GetConnectionString());
StringBuilder sb = new StringBuilder(string.Empty);
string[] splitItems = null;
foreach (string item in sc)
{
const string sqlStatement = "INSERT INTO SampleTable (Column1,Column2,Column3) VALUES";
if (item.Contains(","))
{
splitItems = item.Split(",".ToCharArray());
sb.AppendFormat("{0}('{1}','{2}','{3}'); ", sqlStatement, splitItems[0], splitItems[1], splitItems[2]);
}
}
try
{
conn.Open();
SqlCommand cmd = new SqlCommand(sb.ToString(), conn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
this.PopulatemainGridView();
SetInitialRow();
Page.ClientScript.RegisterClientScriptBlock(typeof(Page), "Script", "alert('Records Successfuly Saved!');", true);
}
catch (System.Data.SqlClient.SqlException ex)
{
string msg = "Insert Error:";
msg += ex.Message;
throw new Exception(msg);
}
finally
{
conn.Close();
}
}
Add this function in code:
private void PopulatemainGridView()
{
string constr = ConfigurationManager.ConnectionStrings["DBConnection"].ConnectionString;
string sqlStatment = "SELECT * FROM SampleTable";
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(sqlStatment, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
DataSet ds = new DataSet();
da.Fill(ds);
this.gvmainGridView.DataSource = ds;
this.gvmainGridView.DataBind();
}
}
}
}
i have call this function SetInitialRow() at the end of InsertRecords function this will clear the Temporary data of GridView1