开发者

check for an particular column name in datatable

开发者 https://www.devze.com 2023-02-02 08:12 出处:网络
foreach (DataColumn dc in dtNewTable.Columns) { if(dtNewTable.ColumnName[18]=\"MONTH\") { dc.DataType = typeof(str开发者_JAVA技巧ing);
foreach (DataColumn dc in dtNewTable.Columns)
{     
    if(dtNewTable.ColumnName[18]="MONTH")
    {
        dc.DataType = typeof(str开发者_JAVA技巧ing);
    }
}

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.


You were on the right track...

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


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


Try

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


Try this

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


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";
            }
        }
    }
}


    /// <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;
    }


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();
0

精彩评论

暂无评论...
验证码 换一张
取 消