如何使用C#删除数据表中特定列之后的列? [英] How to remove columns after particular columns in datatable using C#?

查看:151
本文介绍了如何使用C#删除数据表中特定列之后的列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在特定列之后从数据表中删除列。我使用了Remove方法,但这是删除'(提前期(WEEKS)))'列'我想在'(提前期后)删除WEEKS)))'列。

如何操作

我使用下面的代码。



我尝试了什么:



I want to remove columns from data table after particular columns.I have used Remove method but this is delete the '("Lead Time (WEEKS)"))' columns i want to remove after the '("Lead Time (WEEKS)"))' columns.
How to do it
I have used below code.

What I have tried:

for (int i = 0; i < ds.Tables.Count; i++)
               {
                   for (int j = 0; j < ds.Tables[i].Columns.Count; j++)
                   {
                       if (ds.Tables[i].Rows[7][j].ToString().Trim().Contains("Lead Time (WEEKS)"))
                       {
                           int y = j;
                           if (ds.Tables[i].Rows[7][y + 1].ToString().Trim() == "" || ds.Tables[i].Rows[7][y + 1].ToString().Trim() != "")
                           {
                               int y1 = y + 1;
                               ds.Tables[i].Columns.RemoveAt(y1);
                           }
                       }
                   }
               }

推荐答案

你可以先得到索引之后要删除列的列。为此,您可以使用 DataColumn.Ordinal Property(System.Data) [ ^ ] 。



类似于 -

You can first get the index of the column afterwards which you want to delete the columns. For that you can use DataColumn.Ordinal Property (System.Data)[^].

Something like-
int indexOfMyCol = myDataTable.Columns["Lead Time (WEEKS)"].Ordinal;



现在你可以运行一个循环来删除从 indexOfMyCol + 1开始的列



希望,它有帮助:)

如果你的要求不是这个,请告诉我。


Now you can run a loop to remove columns starting from indexOfMyCol + 1

Hope, it helps :)
Please let me know if your requirement is something other than this.


Hi, 

I am not good at this. But try below for your scenario

int indexofLeadTimeWEEKS = 0;
for (int i = 0; i < ds.Tables.Count; i++)
{
	if (ds.Columns[i].ColumnName == "Lead Time (WEEKS)")	
        {
         	indexofLeadTimeWEEKS = i;
        }
	if(indexofLeadTimeWEEKS != 0)
	{
		ds.Columns.Remove(ds.Columns[i].ColumnName);
		i = i -1;
		indexofLeadTimeWEEKS = indexofLeadTimeWEEKS - 1;
	}
}


-Karthick Raju


试试这个



try this

foreach (DataTable dt in ds.Tables)
       {
           bool flag = false;
           foreach (DataColumn col in dt.Columns)
           {
               if (col.ColumnName == "Lead Time (WEEKS)")
                   flag = true;
               if (flag && col.ColumnName != "Lead Time (WEEKS)")
                   dt.Columns.Remove(col);
           }
       }


这篇关于如何使用C#删除数据表中特定列之后的列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆