Changing Caption in dt
-
I have hundred of DataTables and many of the caption names within do not match the column name. Is there a way I can change all captions to match column name.he follows code I tried.
foreach (DataTable dt in dataSet.Tables) { foreach (DataColumn dc in dt.Columns) { if (dc.DataType == typeof(string)) { dc.Caption = dc.ColumnName.ToString(); } } }
' However this is not permanent and I don't see results. Is there a way to make this permanent. I appreciate any help and thanks in advance. Michael
-
I have hundred of DataTables and many of the caption names within do not match the column name. Is there a way I can change all captions to match column name.he follows code I tried.
foreach (DataTable dt in dataSet.Tables) { foreach (DataColumn dc in dt.Columns) { if (dc.DataType == typeof(string)) { dc.Caption = dc.ColumnName.ToString(); } } }
' However this is not permanent and I don't see results. Is there a way to make this permanent. I appreciate any help and thanks in advance. Michael
The Caption property isn't used to set the headers of a DataGridView (for example) when you use it as a DataSource - it probably should have been, but it definitely wasn't! If you want a "friendly name" for your displayed columns, you need to manually set them on the display control via the "HeaderText" property:
myDataTable = new DataTable(); myDataTable.Columns.Add("C1"); myDataTable.Columns.Add("C2"); myDataTable.Columns\[0\].Caption = "A new caption"; myDataGridView.Columns.Clear(); myDataGridView.DataSource = myDataTable; for (int i = 0; i < myDataTable.Columns.Count; i++) { myDataGridView.Columns\[i\].HeaderText = myDataTable.Columns\[i\].Caption; }
"I have no idea what I did, but I'm taking full credit for it." - ThisOldTony "Common sense is so rare these days, it should be classified as a super power" - Random T-shirt AntiTwitter: @DalekDave is now a follower!