Click here to Skip to main content
15,887,027 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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:

C#
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);
                           }
                       }
                   }
               }
Posted
Updated 3-Nov-16 17:15pm
Comments
Wendelius 1-Nov-16 2:02am    
Few questions, why do you want to remove columns from the data table and what is the problem you're facing with your code?
Raja Ganapathy 1-Nov-16 2:12am    
This data set is excel file. i upload the excel and convert as dataset and sheets are into datatables.if the user not delete the empty columns after the data fields.some problems are occur. i have do some validation using columns count so the validation is affected for those empty columns or unwanted datas.

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-
C#
int indexOfMyCol = myDataTable.Columns["Lead Time (WEEKS)"].Ordinal;

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.
 
Share this answer
 
Comments
Raja Ganapathy 1-Nov-16 3:30am    
How to do in data set.
C#
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
 
Share this answer
 
try this

C#
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);
           }
       }
 
Share this answer
 
Comments
Raja Ganapathy 5-Nov-16 1:18am    
Thanks to all.
Karthik_Mahalingam 8-Nov-16 2:06am    
welcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900