aspx Tutorial

NET Articles,jQuery demo, asp.net with jQuery, online tutorial,Jquery, SilverLight, Javascript, asp.net,JSON, MVC,.NET Articles,demo, Web Services,
Advertise Here

Toolbar

Get our toolbar!

Advertize



Posted by Aamir Hasan   on Friday, May 13, 2011 Total Views:  

In this example you will see how to create dynamically a DataTable and fill the records and then bind to GridView control. I have also shows you how to update DataTable records if you don’t know the index of a row.

Here’s an example


<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title>DataTable</title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <p>

            <h2>

                Before Update</h2>

            <asp:GridView ID="GridView1" runat="server">

            </asp:GridView>

        </p>

        <p>

            <h2>

                After Update Record</h2>

            <asp:GridView ID="GridView2" runat="server">

            </asp:GridView>

        </p>

        <p>

            <h2>

                Muilple Update records</h2>

            <asp:GridView ID="GridView3" runat="server">

            </asp:GridView>

        </p>

    </div>

    </form>

</body>

</html>

 

Code Behind

   protected void Page_Load(object sender, EventArgs e)

    {

        DataTable __DS = new DataTable();

        __DS.TableName = "Patient";

 

        DataColumn DataColumn1 = new DataColumn();

        DataColumn1.ColumnName = "ID";

        DataColumn1.DataType = typeof(int);

        DataColumn1.AllowDBNull = false;

        DataColumn1.Unique = true;

 

        DataColumn DataColumn2 = new DataColumn();

        DataColumn2.ColumnName = "FullName";

        DataColumn2.DataType = typeof(String);

 

        DataColumn DataColumn3 = new DataColumn();

        DataColumn3.ColumnName = "Gender";

        DataColumn3.DataType = typeof(Char);

 

        DataColumn DataColumn4 = new DataColumn();

        DataColumn4.ColumnName = "Birthday";

        DataColumn4.DataType = typeof(String);

 

 

        DataColumn DataColumn5 = new DataColumn();

        DataColumn5.ColumnName = "Status";

        DataColumn5.DataType = typeof(bool);

 

 

        __DS.Columns.AddRange(new DataColumn[]
                      { DataColumn1, DataColumn2, DataColumn3, DataColumn4, DataColumn5 });

         __DS.Rows.Add(new object[] { 1, "John", 'M', "1995-10-02", false });

        __DS.Rows.Add(new object[] { 2, "Mahwish ", 'F', "2001-12-12", false });

        __DS.Rows.Add(new object[] { 3, "Shilpa", 'F', "2010-11-01" });

        __DS.Rows.Add(new object[] { 4, "Fasil Hameed", 'M', "1991-11-11" });

        __DS.Rows.Add(new object[] { 5, "Khamran Khan", 'M', "1996-3-03" });

 

        GridView1.DataSource = __DS;

        GridView1.DataBind();

 

        DataRow[] PatientRow = __DS.Select("ID = 2");

        PatientRow[0]["FullName"] = "Mahwish Khan";

 

        GridView2.DataSource = __DS;

        GridView2.DataBind();

 

 

 

        /*

        Edit multiple rows

         */

        DataRow[] Rows = __DS.Select();


        for (int i = 0; i < Rows.Length; i++)

        {

            __DS.Rows[i]["Status"] = true;

            __DS.Rows[i]["FullName"] = __DS.Rows[i]["FullName"] + " - " + i.ToString();

        }

 

        GridView3.DataSource = __DS;

        GridView3.DataBind();

 

    }


Output

Download

Creating-Editing-DataTable.rar (1.28 kb)

 

See live demo

Protected by Copyscape Online Plagiarism Tool

Comments (4) -

Dorothea Peninger
Dorothea Peninger United States
6/28/2015 4:38:07 PM #

You can certainly see your skills within the article you write. The sector hopes for more passionate writers like you who are not afraid to say how they believe. Always go after your heart.|

Joseph Deuman
Joseph Deuman United States
6/28/2015 4:57:31 PM #

You need to take part in a contest for one of the best websites on the web. I will recommend this site!|

Margot Pevez
Margot Pevez United States
6/28/2015 5:01:21 PM #

Wonderful goods from you, man. I've understand your stuff previous to and you are just too excellent. I really like what you've acquired here, really like what you're saying and the way in which you say it. You make it entertaining and you still care for to keep it sensible. I can't wait to read far more from you. This is actually a wonderful site.|

Galen Pandey
Galen Pandey United States
7/13/2015 7:33:11 AM #

Great work! This is the kind of information that are supposed to be shared around the internet. Disgrace on Google for now not positioning this post higher! Come on over and discuss with my website . Thank you =)|

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5