Pages

Men

rh

1/08/2013

How to create a DataTable in C#

Please find the Example below.

 protected void GetYears()
        {
            DataTable dt = new DataTable();
            DataColumn column;
            DataRow row;
           
            column = new DataColumn();
            column.ColumnName = "YearID";
            dt.Columns.Add(column);

            column = new DataColumn();
            column.ColumnName = "YearNo";
            dt.Columns.Add(column);
           
            row = dt.NewRow();
            row["YearID"] = "1";
            row["YearNo"] ="2012";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["YearID"] = "2";
            row["YearNo"] = "2011";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["YearID"] = "3";
            row["YearNo"] = "2010";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["YearID"] = "4";
            row["YearNo"] = "2009";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["YearID"] = "5";
            row["YearNo"] = "2008";
            dt.Rows.Add(row);

            row = dt.NewRow();
            row["YearID"] = "6";
            row["YearNo"] = "2007";
            dt.Rows.Add(row);

          
        }

No comments :

Post a Comment