happysmile
happysmile

Reputation: 7777

check for an particular column name in datatable

foreach (DataColumn dc in dtNewTable.Columns)
{     
    if(dtNewTable.ColumnName[18]="MONTH")
    {
        dc.DataType = typeof(string);
    }
}

here i need tio check for an particular columnname if that columnname is "MONTH" then change its datatype to string

can anyone tell me the syntax for it.

Upvotes: 0

Views: 24891

Answers (7)

Luke Sigler
Luke Sigler

Reputation: 1

We cast the DataColumnCollection to an IEnumerable, filter out only the columns that match your criteria, and then mass change the resulting column's datatype. While this maybe over kill when trying to find a single instance, I'm a sucker for LINQ.

 dtNewTable.Columns.Cast<DataColumn>()
            .Where(x => x.ColumnName.ToLower() == "month")
            .Select(x => { x.DataType = typeof(string); return x; }).ToList();

Upvotes: 0

TxRegex
TxRegex

Reputation: 2425

    /// <summary>
    /// Returns true if the given DataTable has the given column name
    /// </summary>
    /// <param name="data">The DataTable you are checking</param>
    /// <param name="columnName">the column name you want</param>
    /// <returns></returns>
    public static bool HasColumn(DataTable data, string columnName)
    {
        if(data == null || string.IsNullOrEmpty(columnName))
        {
            return false;
        }

        foreach(DataColumn column in data.Columns) 
             if (columnName.Equals(column.ColumnName, StringComparison.OrdinalIgnoreCase)) return true;
        return false;
    }

Upvotes: 0

Michael Buen
Michael Buen

Reputation: 39393

Try this

if (dt.Columns.Contains("MONTH"))
    dt.Columns["MONTH"].DataType = yourDesiredDataTypeHere;

Upvotes: 1

Tom Brothers
Tom Brothers

Reputation: 6007

If you have data in the DataTable you will need to copy the data to new column that has the expected data type.

using System;
using System.Data;

namespace WindowsFormsApplication1
{
    static class Program
    {
        [STAThread]
        static void Main()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Month", typeof(int));
            dt.Rows.Add(1);

            if (dt.Columns.Contains("Month"))
            {
                DataColumn originalDataColumn = dt.Columns["Month"];
                DataColumn newDataColumn = dt.Columns.Add("NewMonth", typeof(string));

                foreach (DataRow dr in dt.Rows)
                {
                    dr[newDataColumn] = dr[originalDataColumn].ToString();
                }

                dt.Columns.Remove(originalDataColumn);
                newDataColumn.ColumnName = "Month";
            }
        }
    }
}

Upvotes: 0

Dunc
Dunc

Reputation: 8038

Try

if(dc.ColumnName =="MONTH")
{
  dc.DataType = typeof(String);
}

Upvotes: 1

Aaron McIver
Aaron McIver

Reputation: 24713

You were on the right track...

foreach (DataColumn dc in dtNewTable.Columns) 
{
      if(dc.ColumnName == "MONTH")
      {
           dc.DataType = typeof(string);
      }
}

Upvotes: 6

decyclone
decyclone

Reputation: 30830

foreach (DataColumn dc in dtNewTable.Columns)
{
    if (dc.ColumnName == "MONTH")
    {
        dc.DataType = typeof(String);
    }
}

Upvotes: 2

Related Questions