datacolumn

How to get the SqlType of a column in a DataTable?

浪尽此生 提交于 2019-11-30 14:37:14
问题 I have a DataTable obtained from a SQL DataBase, like this: using (SqlCommand cmd = new SqlCommand(query, _sqlserverDB)) { using (SqlDataAdapter adapter = new SqlDataAdapter(cmd)) { DataSet dataSet = new DataSet(); adapter.Fill(dataSet); result = (dataSet != null && dataSet.Tables != null && dataSet.Tables.Count > 0) ? dataSet.Tables[0] : null; } } When I try to get the DataType of each column through dataColumn.DataType , I get the C# types (Int32, Int64, String, etc). QUESTION: How can I

Why DataColumn.Caption doesn't work?

断了今生、忘了曾经 提交于 2019-11-29 05:52:50
I am trying to create a DataTable and bind it to a DataGridView . It works, but I can't set columns headers via the Caption property. It displays headers using the ColumnName ("City") instead. MSDN says that "You can use the Caption property to display a descriptive or friendly name for a DataColumn." Here is my code: DataColumn dc = new DataColumn("City", typeof(string)); dc.Caption = "Город"; DataTable dt = new DataTable(); dt.Columns.Add(dc); DataRow row = dt.NewRow(); row["City"] = "Moscow"; dt.Rows.Add(row); datagridview.DataSource = dt; Well, MSDN is right. That is what the Caption

How do I get column names to print in this C# program?

﹥>﹥吖頭↗ 提交于 2019-11-28 18:31:53
I've cobbled together a C# program that takes a .csv file and writes it to a DataTable . Using this program, I can loop through each row of the DataTable and print out the information contained in the row. The console output looks like this: --- Row --- Item: 1 Item: 545 Item: 507 Item: 484 Item: 501 I'd like to print the column name beside each value, as well, so that it looks like this: --- Row --- Item: 1 Hour Item: 545 Day1 KW Item: 507 Day2 KW Item: 484 Day3 KW Item: 501 Day4 KW Can someone look at my code and tell me what I can add so that the column names will print? I am very new to C#

Why DataColumn.Caption doesn't work?

假如想象 提交于 2019-11-27 05:57:51
问题 I am trying to create a DataTable and bind it to a DataGridView . It works, but I can't set columns headers via the Caption property. It displays headers using the ColumnName ("City") instead. MSDN says that "You can use the Caption property to display a descriptive or friendly name for a DataColumn." Here is my code: DataColumn dc = new DataColumn("City", typeof(string)); dc.Caption = "Город"; DataTable dt = new DataTable(); dt.Columns.Add(dc); DataRow row = dt.NewRow(); row["City"] =

How To Change DataType of a DataColumn in a DataTable?

喜你入骨 提交于 2019-11-26 10:14:41
I have: DataTable Table = new DataTable; SqlConnection = new System.Data.SqlClient.SqlConnection("Data Source=" + ServerName + ";Initial Catalog=" + DatabaseName + ";Integrated Security=SSPI; Connect Timeout=120"); SqlDataAdapter adapter = new SqlDataAdapter("Select * from " + TableName, Connection); adapter.FillSchema(Table, SchemaType.Source); adapter.Fill(Table); DataColumn column = DataTable.Columns[0]; What I want to do is: Assume currently column.DataType.Name is "Double" . I want it to become "Int32" . How do I achieve this? You cannot change the DataType after the Datatable is filled