Hi JonJacobs,
Please refere below sample.
Note: For this sample i have used temporary DataTable. For more details refer How to create Temporary Table in ASP.Net using C# and VB.Net.
HTML
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="Country" HeaderText="Country" />
</Columns>
</asp:GridView>
<hr />
<table>
<tr>
<th>Name:</th>
<th>Country:</th>
</tr>
<tr>
<td><asp:TextBox ID="txtName" runat="server" /></td>
<td><asp:TextBox ID="txtCountry" runat="server" /></td>
</tr>
<tr>
<td colspan="2" style="text-align: center">
<asp:Button Text="Insert Data" OnClick="Insert" runat="server" /></td>
</tr>
</table>
Namespaces
C#
using System.Data;
VB.Net
Imports System.Data
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = GetData();
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
}
private DataTable GetData()
{
DataTable dt = new DataTable();
dt.Columns.AddRange(new DataColumn[] {
new DataColumn("Name"),
new DataColumn("Country")
});
dt.Rows.Add("John Hammond", "United States");
dt.Rows.Add("Mudassar Khan", "India");
dt.Rows.Add("Suzanne Mathews", "France");
dt.Rows.Add("Robert Schidner", "Russia");
return dt;
}
//Clear GridView data and Inser new data.
protected void Insert(object sender, EventArgs e)
{
this.gvCustomers.DataSource = null;
DataTable dt = GetData();
dt.Clear();
dt.Rows.Add(this.txtName.Text,this.txtCountry.Text);
this.gvCustomers.DataSource = dt;
this.gvCustomers.DataBind();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
Dim dt As DataTable = GetData()
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End If
End Sub
Private Function GetData() As DataTable
Dim dt As DataTable = New DataTable()
dt.Columns.AddRange(New DataColumn() {New DataColumn("Name"), New DataColumn("Country")})
dt.Rows.Add("John Hammond", "United States")
dt.Rows.Add("Mudassar Khan", "India")
dt.Rows.Add("Suzanne Mathews", "France")
dt.Rows.Add("Robert Schidner", "Russia")
Return dt
End Function
Protected Sub Insert(ByVal sender As Object, ByVal e As EventArgs)
Me.gvCustomers.DataSource = Nothing
Dim dt As DataTable = GetData()
dt.Clear()
dt.Rows.Add(Me.txtName.Text, Me.txtCountry.Text)
Me.gvCustomers.DataSource = dt
Me.gvCustomers.DataBind()
End Sub
Screenshot