网上搜了好多,没找到能用的,自己写一个,有发现错误的给我留言。

  private void RemoveNULLColumns(ref DataTable data)//删除空列
        {
            try
            {
                DataTable newData = new DataTable();

                for (int n = 0; n < data.Rows.Count-1; n++)//添加行
                {
                    newData.Rows.Add();
                }
                for (int i = 0; i < data.Columns.Count; i++)//遍历列
                {
                    for (int r = 0; r < data.Rows.Count; r++)//查询行内的数据
                    {
                        if (data.Rows[r][i].ToString() != "")//不是空的话
                        {
                            string columnName = data.Columns[i].ColumnName;
                            newData.Columns.Add(columnName);
                            for (int n = 0; n < data.Rows.Count; n++)
                            {
                                newData.Rows[n][newData.Columns.Count - 1] = data.Rows[n][i];
                            }
                            break;
                        }
                    }
                }
                data = newData;
            }
            catch { }
        }

 

相关文章:

  • 2022-01-12
  • 2021-07-28
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-08-05
  • 2021-06-18
  • 2022-01-04
  • 2022-12-23
  • 2022-12-23
  • 2021-09-26
  • 2022-12-23
相关资源
相似解决方案