Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > How to add duplicate datacolumn to datatable
 

How to add duplicate datacolumn to datatable

Hi All,

How can we add a duplicate column to datatable. I already had a column named "Action" in my datatable, and i want a duplicate column to "Action".

Note:
Actually the datatable is being filled from dataset. I cannot change the Sql Query because those are predefined by analysts.

Thanks in advance.
Sukanya  Thursday, December 28, 2006 3:06 PM

Just use a foreach loop:

dc = new DataColumn("Action2",typeof(string));

ds.Tables[0].Columns.Add(dc);

foreach (DataRow dr in ds.Tables[0].Rows)

{

dr["Action2",]= dr["Action1"];

}

You can not have 2 columns named action in your data table. You canadd a datacolumnthat uses an expression tocopy the data to the new column.


DataColumn dc = new DataColumn("Action2");
dc.Expression =
"Action";
dt.Columns.Add(dc);


Ken Tucker  Thursday, December 28, 2006 6:54 PM
Hi Ken,

Thank you so much. This was very helpful. But there is one more query in it.

I will change the value of action field when certain key is pressed. But that should not be reflected in 'action2'. Because I want to compare original action and changed action.

Whenver I change the 'action' and click on some other cell, the value of that field is being changed in 'action2' also. That should not happen . Can you please help with this.

Sukanya  Wednesday, January 03, 2007 12:50 PM

Just use a foreach loop:

dc = new DataColumn("Action2",typeof(string));

ds.Tables[0].Columns.Add(dc);

foreach (DataRow dr in ds.Tables[0].Rows)

{

dr["Action2",]= dr["Action1"];

}

Thank you Shin Ye. It worked.

Sukanya  Thursday, January 18, 2007 2:05 PM

You can use google to search for other answers

Custom Search

More Threads

• Host Controls in Windows Forms DataGridView Cells using VB.net
• Specified argument was out of the range of valid values.Parameter name: '0'is not a valid value for 'index'
• DataGridView and C#
• Sorting ListView Data
• datagridview combobox
• vb.net DataGridView edit mode without adding a row
• Inline controls in a DataGrid or ListView
• How do I hide the location bar in a pop up window using window.showModalDialog?
• DataGrid Word Wrap
• how can i change the cell value like ("A"&1)...