datacolumn

changing values of a column in datatable in vb.net

风格不统一 提交于 2019-12-06 08:53:27
I want to loop through a databale and change the values of a specific column in that datatable. eg: the database returns Request_ID , Desc , Date and Status_Ind . The Status_Ind column contains integer values. I dont want to show the integer values to the user. I want to convert that to a string value based on the integer values returned in that columns. if Status_Ind = 1 then 'the values changes to Published You could do it by creating another column: Dim dt As New DataTable dt.Columns.Add(New DataColumn("status_int", GetType(Int32))) dt.Columns.Add(New DataColumn("status_str", GetType(String

datagridview with datasource and combobox

你离开我真会死。 提交于 2019-12-05 22:04:16
I have a datagridview with a datasource attached to it made of a custom datatable: DataTable: 0 = Dictionary 1 = string 2 = string the datagridview is editable, however for column 0 I need to show a combobox instead of a text field. How do I go about achieving this? internal Dictionary<int, string> products_list = new Dictionary<int, string>(); products_list.Add(0, "Test Product 1"); products_list.Add(1, "Test Product 2"); lines.Columns.Add(new DataColumn("Product", products_list.GetType())); lines.Columns.Add(new DataColumn("QTY", typeof(int))); lines.Columns.Add(new DataColumn("Description",

Customize DataColumn.Expression handling in C#

大城市里の小女人 提交于 2019-12-04 12:21:32
I would like to change behavior of DataColumn.Expression so that when I write: DataColumn.Expression = "MyMethod(Price)" It will call MyMethod, pass value from Price column into it and display evaluated value. How can I acomplish this? Not possible. The expression parser behind the Expression property is simple and not extensible. Making arbitrary function calls is not one of its capabilities. There are several ways to work around this, especially ones that don't require an expensive reflection lookup. Consider the DataTable.RowChanged event for example. A way around this is to make your

Best way add a new column with sequential numbering in an existing data table

三世轮回 提交于 2019-12-03 09:39:22
I have a non empty datatable . What is the best way to add another column to it that has sequential numbering starting from 1. I tried the following code. But did not work. DataColumn dc = new DataColumn("Col1"); dc.AutoIncrement = true; dc.AutoIncrementSeed = 1; dc.AutoIncrementStep = 1; dc.DataType = typeof(Int32); dt.Columns.Add(dc); Will setting any expression help in this scenario ? Thanks in Advance I think you could achieve that by using a 2nd "helper" data table that would contain just an auto-increment field and then you populate/merge it with the existing data, something like this:

How can I get a sum for the column “pieces” in a datatable?

纵然是瞬间 提交于 2019-12-03 04:03:38
How can I get a sum for the column "pieces" in a datatable? Say I had the following table. How can I calculate the "total" pieces for article="milk" and artno="15"? Columns: article artno pieces Rows: 1 milk 15 1 2 water 12 1 3 apple 13 2 4 milk 15 1 5 milk 16 1 6 bread 11 2 7 milk 16 4 The Result of the my new DataTable should be this: Columns: article artno pieces Rows: 1 bread 11 2 2 water 12 1 3 apple 13 2 4 milk 15 2 5 milk 16 5 My Code: foreach (DataRow foundDataRow in foundRows1) { int i = 0; foreach (DataRow dataRow in foundRows2) { if (object.Equals(dataRow.ItemArray[0], foundDataRow

In WPF, how can I bind a datagrid column to a specific column of a datatable?

半城伤御伤魂 提交于 2019-12-03 00:21:34
I have a Datatable with many columns, and a datagrid in which I need to display only a few of those columns. I need a codesample of how to do it. I found a whole bunch of examples telling me to turn AutoGenerateColumns to true and set the table as DataContext to display all the columns in my DataTable. And I guess I could then put in a page of code to hide all the columns that I don't need, rearrange the leftover ones to proper order and size, but surely there must be a more elegant way. In designer I have build the collection of columns I want to display, I got the datable, how do I bind an

How to access an column with special characters using DataTable.Select()?

别来无恙 提交于 2019-12-02 11:02:18
I have a DataTable with column such as # of Students and would like to sort by this in descending order. Here is my code: ... dt.Columns.Add(new DataColumn("# of Students", typeof(string))); // do some stuff... add records etc. // A runtime error occurs here: "Cannot find column '# of Students'" var rows = dt.Select("","'# of Students' desc"); // this is just fine. rows = dt.Select("","# of Students"); How can I access this column if has special characters in its name? You should use [] brackets, like this : var rows = dt.Select("","[# of Students] desc"); You can use both [] or `` syntax.

Create ADO.NET DataView showing only selected Columns

99封情书 提交于 2019-11-30 19:13:58
In C# & .NET, can one create a DataView that includes only a proper subset of the DataColumn s of a given DataTable ? In terms of relational algebra, one assigns a RowFilter in order to perform a "selection" operation (σ). How would one perform a "projection" operation (π)? You can't do that, but you can create a copy of the table with only the columns you want : DataView view = new DataView(table); DataTable table2 = view.ToTable("FirstColumn", "SecondColumn", "ThirdColumn"); Optionally you can return rows that have distinct values for the selected columns : DataView view = new DataView(table

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

爷,独闯天下 提交于 2019-11-30 17:22:33
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 access the native SQL data types (varchar, nvarchar, bigint...) instead of the C# types? I have tried

DataColumn Name from DataRow (not DataTable)

*爱你&永不变心* 提交于 2019-11-30 16:47:32
I need to iterate the columnname and column datatype from a specific row. All of the examples I have seen have iterated an entire datatable. I want to pass a single row to a function to do a bunch of conditional processing. I want to separate the conditional processing for ease of readability. This is what I have: private void doMore(DataRow dr) { foreach (DataColumn c in dr.ItemArray) //loop through the columns. { MessageBox.Show(c.ColumnName.ToString()); } } The error returned is System.InvalidCastException: Unable to cast object of type 'System.String' to type 'System.Data.DataColumn'. How