csharp: DataTable Rename ColumnName and remove Column

简介: enum ChangeNume { /// <summary> /// 简体 /// </summary> gbk=1, /// <summary> /// 英文 /// </summary>
enum ChangeNume
      {
          /// <summary>
          /// 简体
          /// </summary>
          gbk=1,
          /// <summary>
          /// 英文
          /// </summary>
          en=2,
          /// <summary>
          /// 繁体
          /// </summary>
          big=3,
          ko=4,
          jp=5,
      }
 
      /// <summary>
      ///
      /// </summary>
      /// <returns></returns>
      DataTable setData()
      {
          DataTable dt = new DataTable();
          dt.Columns.Add("id", typeof(int));
          dt.Columns.Add("name", typeof(string));
          dt.Columns.Add("patner", typeof(int));
          dt.Columns.Add("time", typeof(DateTime));
          dt.Rows.Add(1, "dgeo", 0, "2015-01-02");
          dt.Rows.Add(2, "geovindu", 1, "2015-03-03");
          dt.Rows.Add(3, "gess", 1, "2015-03-23");
          dt.Rows.Add(4, "dfadfd", 2, "2015-04-03");
          return dt;
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="dv"></param>
      /// <param name="lname"></param>
      private DataTable setGridViewName(DataTable dv, ChangeNume lname)
      {
          DataTable dt = dv;
          switch (lname)
          {
              case ChangeNume.gbk:
                  dt.Columns[0].ColumnName = "序号";
                  dt.Columns[1].ColumnName = "名称";
                  dt.Columns[2].ColumnName = "父节点";
                  dt.Columns[3].ColumnName = "日期";
                  break;
              case ChangeNume.big:
                  dt.Columns[0].ColumnName = "序號";
                  dt.Columns[1].ColumnName = "名稱";
                  dt.Columns[2].ColumnName = "父節點";
                  dt.Columns[3].ColumnName = "日期";
                  break;
              case ChangeNume.en:
                  dt.Columns[0].ColumnName = "id";
                  dt.Columns[1].ColumnName = "name";
                  dt.Columns[2].ColumnName = "patner";
                  dt.Columns[3].ColumnName = "time";
                  break;
              default:
                  dt.Columns[0].ColumnName = "序号";
                  dt.Columns[1].ColumnName = "名称";
                  dt.Columns[2].ColumnName = "父节点";
                  dt.Columns[3].ColumnName = "日期";
                  break;
          }
          return dt;
      }
 
      /// <summary>
      ///
      /// </summary>
      public Form2()
      {
          InitializeComponent();
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void Form2_Load(object sender, EventArgs e)
      {
          this.dataGridView1.DataSource = setGridViewName(setData(), ChangeNume.en);
 
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="sender"></param>
      /// <param name="e"></param>
      private void button1_Click(object sender, EventArgs e)
      {
          this.dataGridView2.DataSource = setGridViewName(setData(), ChangeNume.big);
      }


/// <summary>
     /// DataTable列名移除
      /// </summary>
      /// <param name="dcName"></param>
      /// <param name="datatable"></param>
      private void DataTableRemoveColumn(string dcName,DataTable datatable)
      {
         // List<string> clmnames = new List<string>(new string[] { "clm6", "clm7", "clm20" });
            DataColumnCollection dcCollection = datatable.Columns;
            if (dcCollection.Contains(dcName))
            {
                dcCollection.Remove(dcName);
            }
      }
     /// <summary>
      /// DataTable列索引移除
     /// </summary>
     /// <param name="index"></param>
     /// <param name="datatable"></param>
     private void DataTableRemoveIndex(List<int> index, DataTable datatable)
     {
         //List<int> index = new List<int>(new int[] { 2, 3, 5 });
         List<string> primes = new List<string>();
         foreach (int i in index)
         {
             primes.Add(datatable.Columns[i].ColumnName);
         }
 
         foreach (string iname in primes)
         {
             DataTableRemoveColumn(iname, datatable);
         }
 
     }


目录
相关文章
when click one item in table Select at least one column to perform the search
when click one item in table Select at least one column to perform the search
114 0
when click one item in table Select at least one column to perform the search
How to add extension field to report
How to add extension field to report
127 0
使用代码获得table whered use list
使用代码获得table whered use list
105 0
change datagridview column.headtext
动态change datagridview 某个列的值,最快的方法: this.FTContent.HeaderText = currentNodeText + ",议题列表"; 直接使用DataGridViewTextBoxColu...
797 0
|
SQL C#
csharp: using OleDb Getting the identity of the most recently added record
/// &lt;summary&gt; /// 执行SQL语句,返回影响的记录数 /// &lt;/summary&gt; /// &lt;param name="SQLString"&gt;SQL语句&lt;/param&gt; /// &lt;returns&gt;影响的记录数&lt;/returns&gt;
1160 0
|
SQL C# Go
sql and csharp: Split Function
T-SQL:   declare @int int,@prov int,@city int,@str nvarchar(500) set @str='天河麗特青春:中國廣東省廣州市天河區天河路623號天河娛樂廣場麗特青春百貨一樓,塗聚文' select @int=charindex(':',@str) select @prov=charindex('省',@str) sel
1035 0