adding duplicate type of column to a datatable
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));
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 ;)
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.
Old post, you can use binary null character:
string NullString= "\0";
table.Columns.Add("DC", typeof(string));
table.Columns.Add("DC" + NullString, typeof(string));
精彩评论