Click here to Skip to main content
15,889,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
https://i.stack.imgur.com/7RBox.jpg[^]

Above image shows what i want, i wish to remove prefix part all columns names like ,WK1-600,WK1-700,WK1-800....WK2-600,WK2-700,WK2-800....WK3-600,WK3-700,WK4-800....WK4-600,WK4-700,WK4-800 etc then remove 600, 700, 800 alternately.

I am trying below code but i dont know how to fix it correclty in interop excel... i am very jr in coding please help me....big thanks in advance

What I have tried:

if i wish to remove all numbers in column names, which placing after ('-'), then what will i do?...anyone please help



C#
foreach (DataColumn column in ds.Tables[0].Columns)
  {
      string colname = column.ColumnName;
      if (colname == "Retailer" || colname == "Brand") ;
      else
      {
          ds.Tables[0].Columns[colname].ColumnName = "WK" + colname.split('-')[0];
      }
  }
Posted
Updated 26-Jun-19 10:16am
v3
Comments
BillWoodruff 25-Jun-19 21:17pm    
You really want all the column names to be "WK" ? That's what your description, and code, imply.

Not sure what you want to achieve, because the headers of columns haven't changed.
Assuming that you want to add rows based on column names, you can use something like that:

C#
DataTable dt = ds.Tables[0]; 
dt.Columns.AddRange(names.Select(x=>new DataColumn(x)).ToArray());

var row2 =  dt.Columns.Cast<DataColumn>()
		.Select(c=>c.ColumnName.Split('-')[0])
		.ToArray();
var row1 =  dt.Columns.Cast<DataColumn>()
		.Select(c=>c.ColumnName.Split('-')[1])
		.ToArray();
dt.Rows.Add(row1);		
dt.Rows.Add(row2);


Result:
WK1-600 WK1-700 WK1-800 WK2-600 WK2-700 WK2-800 WK3-600 WK3-700 WK3-800 WK4-600 WK4-700 WK4-800
600     700     800     600     700     800     600     700     800     600     700     800 
WK1     WK1     WK1     WK2     WK2     WK2     WK3     WK3     WK3     WK4     WK4     WK4 
 
Share this answer
 
v2
Just remove the "old" row and insert a new one with the values you want.

How to add new row to excel file in C# - Stack Overflow[^]
 
Share this answer
 
Comments
BillWoodruff 25-Jun-19 21:54pm    
My vote of #1: the question is about changing Column headers, has nothing to do with Rows.

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