csharp: DataTable Rename ColumnName and remove Column

原创 2015年07月09日 20:39:16
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);
         }
 
     }

测试:

DataTable dt = setData();
DataTable ds=new DataTable();
//店铺代码  店铺名称    姓名  身份证号    职位  基本出勤时数  计薪时数    迟到早退
//1
List<int> primes = new List<int>(new int[] {0, 2, 3});
DataTableRemoveIndex(primes, dt);
 
//2.
//List<string> clmnames = new List<string>(new string[] { "店铺代码", "基本出勤时数", "职位" });
//foreach (string i in clmnames)
//{
//  DataTableRemoveColumn(i, dt);
//}
this.dataGridView1.DataSource = dt;


版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

csharp: DataTable Rename ColumnName and remove Column

enum ChangeNume { /// /// 简体 /// gbk=1, //...

Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable

/// /// 添加 /// /// /// private void button1_Click(object...

Csharp:The .dat File using BinaryReader and BinaryWriter Convert to DataTable

/// /// 添加 /// /// /// private void button1_Click(o...

Csharp: Listview convert Datatable and ListView.Group count

/// /// 計算ListView各類子項ListViewGroup的數量 /// /// private DataTa...

unix环境高级编程-4.15-link,unlink,remove和rename函数

如上节所述,任何一个文件可以有多个目录项指向其i节点。创建一个指向现有文件的链接的方法是使用link函数。 #include int link(const char* existingpath,...

Csharp 讀取大文本文件數據到DataTable中,大批量插入到數據庫中

文本文件中的內容格式為:(其中問題,誰能否解決大於15M的文本文件能快速讀到DataTable中) 俚    rustic 俚语    slang 哩    mile 哩  &#...

Jquery Datatable 数据填充报错:requested unknown parameter 'XXX' for row xx, column xx 解决方法

Jquery Datatable 数据填充报错:requested unknown parameter 'XXX' for row xx, column xx 解决方法

Datatable删除行的Delete和Remove方法

AcceptChanges 在 Update 后 !     1、       DataRow row = dt.NewRow();       //...

CSharp - Memory leaks problem detection and solution

CSharp - Memory leaks problem detection and solution

Csharp: Create Excel Workbook or word from Template File using aspose.Word 14.5 and aspose.Cell 8.1

winform: /// /// /// /// /// private void btnGenWord_Cl...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)