Click here to Skip to main content
16,010,553 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to check is there any space in the column names in a data table. if there is any space need to be replace that column name without space
Posted
Updated 20-Aug-13 18:47pm
v2

DataTable has a Columns property which you can iterate through : http://msdn.microsoft.com/en-us/library/system.data.datatable.columns.aspx[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 21-Aug-13 1:30am    
Sure, 5ed.
—SA
Mehdi Gholam 21-Aug-13 1:34am    
Thanks Sergey!
Maciej Los 21-Aug-13 2:05am    
+5
Mehdi Gholam 21-Aug-13 2:11am    
Thanks Maciej!
C#
using System;
using System.Collections.Generic;
using System.Text;

using System.Data.Common;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable tbl = new DataTable();
            tbl.Columns.Add("Column A");
            tbl.Columns.Add("Column B");
            tbl.Columns.Add("Column C");
            tbl.Columns.Add("Column D");
            tbl.Columns.Add("Column E");

            Console.WriteLine("Before Change...");
            PrintColumnsName(tbl);

            foreach (DataColumn dcl in tbl.Columns)
            {
                if (dcl.ColumnName.Contains(" "))
                {
                    dcl.ColumnName = dcl.ColumnName.Replace(" ", "");
                }
            }

            tbl.AcceptChanges();

            Console.WriteLine("After Change...");
            PrintColumnsName(tbl);

            Console.WriteLine("Press a key to quit...");
            Console.ReadKey();
        }

        static void PrintColumnsName(DataTable t)
        {
            foreach (DataColumn dcl in t.Columns)
            {
                Console.WriteLine(dcl.ColumnName);
            }
        }
    }
}
 
Share this answer
 
Comments
Mehdi Gholam 21-Aug-13 1:35am    
5'ed
Maciej Los 21-Aug-13 2:05am    
+5!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900