Reputation: 10815
How can i add same fields for columns in a datatable. I wan to make a datatable which repeate column name of same type multiple times, but dot net dont allow me to add that, but i want to do that hw can i do that?
table.Columns.Add("S.NO", typeof(int));
table.Columns.Add("DC", typeof(string));
table.Columns.Add("DC", typeof(string));
table.Columns.Add("DC", typeof(string));
table.Columns.Add("ST", typeof(string));
table.Columns.Add("ST", typeof(string));
table.Columns.Add("AD", typeof(string));
table.Columns.Add("AD", typeof(string));
Upvotes: 1
Views: 5613
Reputation: 644
Old post, you can use binary null character:
string NullString= "\0";
table.Columns.Add("DC", typeof(string));
table.Columns.Add("DC" + NullString, typeof(string));
Upvotes: 2
Reputation: 620
You do it like this
DataColumn col = new DataColumn();
col.Caption = "Your Caption";
col.ColumnName = "UniqueColumnName";
col.DataType = System.Type.GetType("System.String");
testDataTable.Columns.Add(col);
Just saying its bad practice is not helpful, we all know its bad practice, but for display purposes we sometimes need this ;)
Upvotes: 3
Reputation: 50998
This is exactly what relational database were created to avoid. You should not repeat the same column in a table. Instead, each instance of the column should become rows in a related table.
So a table like (SNo INTEGER, INFO STRING, ST STRING, ST STRING) would instead become two tables (SNo INTEGER, INFO STRING) and (SNo INTEGER, ST STRING). The original "record" would become one record in the first table and two matching records (matching using the SNo value) in the second table.
Upvotes: 1