How to create datatable programmatically from a string using C#?

Muhilan
Posted by in ASP.NET category on for Beginner level | Views : 36778 red flag
Rating: 4 out of 5  
 2 vote(s)

In this article explain you how to create datatable programmatically from a string using C#.

Introduction

ASP.Net Gridview or other data bound controls are to filled by Dataset or DataTable. Sometimes data's not get from a datasource or data base but we need to take from other source like file system , text file , string etc..  and need to display that data into data bound controls.

 

In that scenarios need to create datatable programmatically or dynamically and filled with rows, columns and bind into the datagrid.

 

I have written simple way to create datatable programmatically based on a string.  


 

String

String looks like below , it have column product values and column Sub plan values



1. First to split the given into columns

The above screen the 4th element is Product column values and the 5th element is corresponding sub plan values. Means sub plan may have several values for a single product.

2. So again if we split into 4th element we can get 4 products like this

zero th element is product and their corresponding values will be get in another split function like this

 


In this first product have only one subplan but 2nd element have multiple suplans

DataTable Creation

declare the datatable, datarow

DataTable tmpTable = new DataTable();
        DataColumn column;
        DataRow row;

 

create datacolumn with columnname, datatype and few properties for that particular datacolumn and add to the datacolumn collections

column = new DataColumn();
column.ColumnName = "Product";
column.ReadOnly = true;
column.Unique = false ;
tmpTable.Columns.Add(column);


 In these examples created datatable with three columns

 

 column = new DataColumn();
            column.ColumnName = "Product";
            column.ReadOnly = true;
            column.Unique = false ;
                   tmpTable.Columns.Add(column);

 

            column = new DataColumn();
            column.ColumnName = "SubPlan";
            column.AutoIncrement = false;
            column.Caption = "Sub Plan";
            column.ReadOnly = false;
            column.Unique = false;
                       tmpTable.Columns.Add(column);


            column = new DataColumn();
            column.ColumnName = "NoofPolicy";
            column.AutoIncrement = false;
            column.Caption = "NoofPolicy";
            column.ReadOnly = false;
            column.Unique = false;
           tmpTable.Columns.Add(column);

Now split into string and assign values to the corresponding columns

 

  string[] tmpstr = str.Split(Convert.ToChar(254));
            string[] tmpstr2;// = new string[100];
            string[] tmpstr3;// = new string[100];

           tmpstr2 = tmpstr[3].Split(Convert.ToChar(253));//Product
           tmpstr3 = tmpstr[4].Split(Convert.ToChar(253));// subplan
            int CntMain=0,CntSub=0;
            string product = string.Empty;
            string subplan = string.Empty;

            while (CntMain < tmpstr2.Count())
            {
                product = tmpstr2[CntMain].ToString();
                subplan = tmpstr3[CntMain];
                string[] tmpsubPlan = subplan.Split(Convert.ToChar(252));
                CntSub = 0;
                while (CntSub < tmpsubPlan.Count())
                {
                        row = tmpTable.NewRow();
                        if (CntSub == 0)
                            row["Product"] = product;
                        else
                        row["Product"] = "";

                        row["SubPlan"] = tmpsubPlan[CntSub].ToString();
                        tmpTable.Rows.Add(row);
              
                    CntSub++;
                }
                CntMain++;

            }

 

Complete Code Listing

private DataTable CreateTable(string str)

{

DataTable tmpTable = new DataTable();

DataColumn column;

DataRow row;

try

{

column = new DataColumn();

column.ColumnName = "Product";

column.ReadOnly = true;

column.Unique = false ;

tmpTable.Columns.Add(column);

column = new DataColumn();

column.ColumnName = "SubPlan";

column.AutoIncrement = false;

column.Caption = "Sub Plan";

column.ReadOnly = false;

column.Unique = false;

tmpTable.Columns.Add(column);

column = new DataColumn();

column.ColumnName = "NoofPolicy";

column.AutoIncrement = false;

column.Caption = "NoofPolicy";

column.ReadOnly = false;

column.Unique = false;

tmpTable.Columns.Add(column);

string[] tmpstr = str.Split(Convert.ToChar(254));

string[] tmpstr2;// = new string[100];

string[] tmpstr3;// = new string[100];

tmpstr2 = tmpstr[3].Split(Convert.ToChar(253));//Product

tmpstr3 = tmpstr[4].Split(Convert.ToChar(253));// subplan

int CntMain=0,CntSub=0;

string product = string.Empty;

string subplan = string.Empty;

while (CntMain < tmpstr2.Count())

{

product = tmpstr2[CntMain].ToString();

subplan = tmpstr3[CntMain];

string[] tmpsubPlan = subplan.Split(Convert.ToChar(252));

CntSub = 0;

while (CntSub < tmpsubPlan.Count())

{

row = tmpTable.NewRow();

if (CntSub == 0)

row["Product"] = product;

else

row["Product"] = "";

row["SubPlan"] = tmpsubPlan[CntSub].ToString();

tmpTable.Rows.Add(row);

CntSub++;

}

CntMain++;

}

return tmpTable;

}

catch

{

// return null;

throw;

}

}

The above function return the datatable and assign to the datagrid

DataTable tmpDataTable= CreateTable(PTRN);

gvMenu.DataSource = tmpDataTable;

gvMenu.DataBind();

PTRN is a string which have the datatable values and should be


Page copy protected against web site content infringement by Copyscape

About the Author

Muhilan
Full Name: Muhil an
Member Level:
Member Status: Member
Member Since: 11/30/2009 2:46:25 AM
Country: India


working as an IT System Analyst,tmuhilan at gmail com

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)