Saturday, July 10, 2021

Add new Row to existing DataTable using C# and VB.Net

 Add new Row to existing DataTable using C# and VB.Net

        In this article I will explain with an example, how to add new Row to existing DataTable using C# and VB.Net.

HTML Markup

The following HTML Markup consists of an ASP.Net GridView along with some TextBoxes and a Button in order to insert data in GridView control.

<asp:GridView ID="GridView1" runat="server" CssClass="Grid" AutoGenerateColumns="false"

    EmptyDataText="No records has been added.">

    <Columns>

        <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="120" />

        <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="120" />

    </Columns>

</asp:GridView>

<br />

<table border="0" cellpadding="0" cellspacing="0" style="border-collapse: collapse">

<tr>

    <td style="padding-bottom: 10px">

        Name:<br />

        <asp:TextBox ID="txtName" runat="server" />

    </td>

</tr>

<tr>

    <td style="padding-bottom: 10px">

        Country:<br />

        <asp:TextBox ID="txtCountry" runat="server" />

    </td>

</tr>

<tr>

    <td style="width: 100px">

        <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="Insert" />

    </td>

</tr>

</table>

 

Namespaces

You will need to import the following namespace.

C#

using System.Data;

 

VB.Net

Imports System.Data

 

Binding the GridView using an Empty DataTable

Initially a DataTable has to be created and is saved in the ViewState variable and then the BindGrid method is executed which populates the GridView from the DataTable saved in ViewState variable.

Here the primary reason to use ViewState variable is to preserve the GridView data across PostBacks.

    Note: You can also make use of Session variable for saving the DataTable.

C#

protected void Page_Load(object sender, EventArgs e)

{

    if (!this.IsPostBack)

    {

        DataTable dt = new DataTable();

        dt.Columns.AddRange(new DataColumn[2] {new DataColumn("Name"), new DataColumn("Country") });

        ViewState["Customers"] = dt;

        this.BindGrid();

    }

}

 

protected void BindGrid()

{

    GridView1.DataSource = (DataTable)ViewState["Customers"];

    GridView1.DataBind();

}

 

VB.Net

Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load

    If Not Me.IsPostBack Then

        Dim dt As New DataTable()

        dt.Columns.AddRange(New DataColumn(1) {New DataColumn("Name"), New DataColumn("Country")})

        ViewState("Customers") = dt

        Me.BindGrid()

    End If

End Sub

 

Protected Sub BindGrid()

    GridView1.DataSource = DirectCast(ViewState("Customers"), DataTable)

    GridView1.DataBind()

End Sub


Adding new Row to existing DataTable using C# and VB.Net

First the DataTable is fetched from the ViewState variable and then a new Row is added to the DataTable by making use of the data from the TextBoxes.

Finally the DataTable is saved back in ViewState variable and the BindGrid method is executed which updates the GridView data.


C#

protected void Insert(object sender, EventArgs e)

{

    DataTable dt = (DataTable)ViewState["Customers"];

    dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim());

    ViewState["Customers"] = dt;

    this.BindGrid();

    txtName.Text = string.Empty;

    txtCountry.Text = string.Empty;

}

 

VB.Net

Protected Sub Insert(sender As Object, e As EventArgs)

    Dim dt As DataTable = DirectCast(ViewState("Customers"), DataTable)

    dt.Rows.Add(txtName.Text.Trim(), txtCountry.Text.Trim())

    ViewState("Customers") = dt

    Me.BindGrid()

    txtName.Text = String.Empty

    txtCountry.Text = String.Empty

End Sub

No comments:

Post a Comment

DotNet Latest Technologies

  I'm not sure exactly what you are looking for or how "recent" the technologies that you might wan to look into are, but I...