Creating a DataTable programmatically in ASP.NET

This article will show how to create a DataTable object at runtime and binding it to a DataGrid web control

The various data bound controls available in ASP.NET allow you to bind themselves with objects that implements IList interface of System.Collections namespace. The famous DataList and DataGrid controls can be bound to more complex data sources like the DataSet and DataTable. Usually you fetch data from a database and fill these data containers from this data and then bind the DataGrid or DataList with these objects.

Some times it becomes necessary that the data not come from a data base but comes from some other source, like from some where in your program you are reading the file system and want to display that data. In such scenarios you can populate the DataSet or more commonly a DataTable in your ASP.NET web page and then bind the web control with this data object.

Here I’ve used a very simple method to dynamically create a DataTable and populated it in the function with some rows and then bind this to a datagrid named dgOne in the ASP.NET page.

Here is the sample web page


public DataTable GetCustomMadeDataTable()
   {
       //Create a new DataTable object
       DataTable objDataTable = new DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Column 1", typeof(string));
       objDataTable.Columns.Add("Column 2", typeof(string));
       objDataTable.Columns.Add("Column 3", typeof(string));
       //Adding some data in the rows of this DataTable
       objDataTable.Rows.Add(new string[] { "Row1 - Column1", "Row1 - Column2", "Row1 - Column3" });
       objDataTable.Rows.Add(new string[] { "Row2 - Column1", "Row2 - Column2", "Row2 - Column3" });
       objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" });
       return objDataTable;

   }
   

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
   <title></title>
</head>
<body>
   <form id="form1" runat="server">
   <div>
       <asp:DataGrid ID="dgOne" runat="server" AutoGenerateColumns="true"/>
   </div>
   </form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

public partial class _Default : System.Web.UI.Page
{
   protected void Page_Load(object sender, EventArgs e)
   {
       DataTable dtObject = GetCustomMadeDataTable() as DataTable;
       dgOne.DataSource = dtObject;
       dgOne.DataBind();

   }
   public DataTable GetCustomMadeDataTable()
   {
       //Create a new DataTable object
       DataTable objDataTable = new DataTable();
       //Create three columns with string as their type
       objDataTable.Columns.Add("Column 1", typeof(string));
       objDataTable.Columns.Add("Column 2", typeof(string));
       objDataTable.Columns.Add("Column 3", typeof(string));
       //Adding some data in the rows of this DataTable
       objDataTable.Rows.Add(new string[] { "Row1 - Column1", "Row1 - Column2", "Row1 - Column3" });
       objDataTable.Rows.Add(new string[] { "Row2 - Column1", "Row2 - Column2", "Row2 - Column3" });
       objDataTable.Rows.Add(new string[] { "Row3 - Column1", "Row3 - Column2", "Row3 - Column3" });
       return objDataTable;

   }




}
Reactions

Post a Comment

1 Comments

  1. thanks a lot it give me better solution

    ReplyDelete

Please do not post any spam link in the comment box😊

Emoji
(y)
:)
:(
hihi
:-)
:D
=D
:-d
;(
;-(
@-)
:P
:o
:>)
(o)
:p
(p)
:-s
(m)
8-)
:-t
:-b
b-(
:-#
=p~
x-)
(k)

Close Menu