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

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

Advertizement 1
Advertizement 2
Advertizement 3
Advertizement 4
Advertizement 5