Hi akhter,
Check this example. Now please take its reference and correct your code.
HTML
<asp:GridView runat="server" ID="gvDynamic" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="SrNo" HeaderText="" />
<asp:TemplateField HeaderText="Column1" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column2" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn2" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column3" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn3" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column4" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn4" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column5" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn5" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column6" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn6" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column7" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn7" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column8" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn8" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column9" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn9" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Column10" ControlStyle-Width="60px">
<ItemTemplate>
<asp:TextBox ID="txtColumn10" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" OnClick="Save" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("SrNo"));
dt.Columns.Add(new DataColumn("Column1"));
dt.Columns.Add(new DataColumn("Column2"));
dt.Columns.Add(new DataColumn("Column3"));
dt.Columns.Add(new DataColumn("Column4"));
dt.Columns.Add(new DataColumn("Column5"));
dt.Columns.Add(new DataColumn("Column6"));
dt.Columns.Add(new DataColumn("Column7"));
dt.Columns.Add(new DataColumn("Column8"));
dt.Columns.Add(new DataColumn("Column9"));
dt.Columns.Add(new DataColumn("Column10"));
dt.Rows.Add("Row6");
dt.Rows.Add("Row5");
dt.Rows.Add("Row4");
dt.Rows.Add("Row3");
dt.Rows.Add("Row2");
dt.Rows.Add("Row1");
gvDynamic.DataSource = dt;
gvDynamic.DataBind();
}
}
protected void Save(object sender, EventArgs e)
{
foreach (GridViewRow row in gvDynamic.Rows)
{
string col1 = (row.FindControl("Column1") as TextBox).Text.Trim();
string col2 = (row.FindControl("Column2") as TextBox).Text.Trim();
string col3 = (row.FindControl("Column3") as TextBox).Text.Trim();
string col4 = (row.FindControl("Column4") as TextBox).Text.Trim();
string col5 = (row.FindControl("Column5") as TextBox).Text.Trim();
string col6 = (row.FindControl("Column6") as TextBox).Text.Trim();
string col7 = (row.FindControl("Column7") as TextBox).Text.Trim();
string col8 = (row.FindControl("Column8") as TextBox).Text.Trim();
string col9 = (row.FindControl("Column9") as TextBox).Text.Trim();
string col10 = (row.FindControl("Column10") as TextBox).Text.Trim();
// Code to save in datbase.
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
SqlCommand cmd = new SqlCommand();
using (SqlConnection con = new SqlConnection(conString))
{
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "InsertDynamic";
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dt As DataTable = New DataTable()
dt.Columns.Add(New DataColumn("SrNo"))
dt.Columns.Add(New DataColumn("Column1"))
dt.Columns.Add(New DataColumn("Column2"))
dt.Columns.Add(New DataColumn("Column3"))
dt.Columns.Add(New DataColumn("Column4"))
dt.Columns.Add(New DataColumn("Column5"))
dt.Columns.Add(New DataColumn("Column6"))
dt.Columns.Add(New DataColumn("Column7"))
dt.Columns.Add(New DataColumn("Column8"))
dt.Columns.Add(New DataColumn("Column9"))
dt.Columns.Add(New DataColumn("Column10"))
dt.Rows.Add("Row6")
dt.Rows.Add("Row5")
dt.Rows.Add("Row4")
dt.Rows.Add("Row3")
dt.Rows.Add("Row2")
dt.Rows.Add("Row1")
gvDynamic.DataSource = dt
gvDynamic.DataBind()
End If
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
For Each row As GridViewRow In gvDynamic.Rows
Dim col1 As String = (TryCast(row.FindControl("Column1"), TextBox)).Text.Trim()
Dim col2 As String = (TryCast(row.FindControl("Column2"), TextBox)).Text.Trim()
Dim col3 As String = (TryCast(row.FindControl("Column3"), TextBox)).Text.Trim()
Dim col4 As String = (TryCast(row.FindControl("Column4"), TextBox)).Text.Trim()
Dim col5 As String = (TryCast(row.FindControl("Column5"), TextBox)).Text.Trim()
Dim col6 As String = (TryCast(row.FindControl("Column6"), TextBox)).Text.Trim()
Dim col7 As String = (TryCast(row.FindControl("Column7"), TextBox)).Text.Trim()
Dim col8 As String = (TryCast(row.FindControl("Column8"), TextBox)).Text.Trim()
Dim col9 As String = (TryCast(row.FindControl("Column9"), TextBox)).Text.Trim()
Dim col10 As String = (TryCast(row.FindControl("Column10"), TextBox)).Text.Trim()
' Code to save in datbase.
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim cmd As SqlCommand = New SqlCommand()
Using con As SqlConnection = New SqlConnection(conString)
cmd.Connection = con
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "InsertDynamic"
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
Next
End Sub
Screenshot