Vinit Patel

Removing Columns in DataTable.

Posted on: October 1, 2009


private DataTable CreateDataTable()
    {
        DataTable dt = new DataTable();
        DataRow dr = null;

        //Create the Columns Definition
     dt.Columns.Add(new DataColumn("Column1", typeof(string)));
     dt.Columns.Add(new DataColumn("Column2", typeof(string)));
     dt.Columns.Add(new DataColumn("Column3", typeof(string)));

     //Add the first Row to each columns defined

        dr = dt.NewRow();

       //Add dummy values to each rows
        dr["Column1"] = "A";
        dr["Column2"] = string.Empty; 
          // but it will be converted to empty
        dr["Column3"] = "C";

        dt.Rows.Add(dr);

        //Add the second Row to each columns defined

        dr = dt.NewRow();
        dr["Column1"] = null;
        dr["Column2"] = null;// but it will be converted to empty
        dr["Column3"] = "F";

        dt.Rows.Add(dr);
        //You can continue adding rows here
        return dt;

    }

Create the method for checking the row values of a 
particular column in a DataTable. Here’s the 
code block below:

 private DataTable CheckDataTableColumn()
    {
        DataTable dt = CreateDataTable();
        bool flag = false;
        int counter = 0;

        EXIT:
      for (int i = counter; i < dt.Columns.Count; i++)
        {
        for (int x = 0; x < dt.Rows.Count; x++)
         {
         if (string.IsNullOrEmpty(dt.Rows[x][i].ToString()))
         {
           flag = true; //means there is an empty value
         }
         else
           {
//means if it found non null or empty in rows of a particular column
               flag = false;
               counter = i + 1;
               goto EXIT;
           }
        }
        if (flag == true)
          {
           dt.Columns.Remove(dt.Columns[i]);
           i--;
          }
         }
       return dt;
    }

now let’s try to test it. To test it then lets check 
if what columns are being deleted by looping through 
the columns in the DataTable. See the code block below:

 protected void Page_Load(object sender, EventArgs e)
    {
        //print the Columns in the DataTable to 
            see if what column(s) are deleted
        DataTable dtNew = CheckDataTableColumn();
        if (dtNew.Rows.Count > 0)
        {
            foreach (DataColumn dc in dtNew.Columns)
            {
                Response.Write(dc.ColumnName + "
");
            }
        }
        else
        {
  Response.Write("No data returned from the DataTable");
        }
    }
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Calendar

October 2009
M T W T F S S
« Sep   Dec »
 1234
567891011
12131415161718
19202122232425
262728293031  

Stats Of Blog

  • 6,824 hits

Cluster Map

%d bloggers like this: