datacolumn

Get sum from a DataColumn values in C#

那年仲夏 提交于 2021-02-10 14:23:56
问题 I have a table in a dataadapter . I want to get the count and sum of a specific column of it. How is that possible? This is the code for reach to the column, what after that? DataColumn buy_count = myDataSet.Tables["all_saled"].Columns["how_much_buy"] I know that we have sum, count,... in SQL, but how can I do it in C#? 回答1: You can use LINQ to DataSets var sales = myDataSet.Tables["all_saled"].AsEnumerable(); var buy_total = sales.Sum(datarow => datarow.Field<int>("how_much_buy")); Check the

DataColumn set a default value

时光毁灭记忆、已成空白 提交于 2021-01-27 07:39:18
问题 I am trying to set a default value for a DataColumn. How do you set a default value for DataColumn (column3) for the below code DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("Column1", typeof(String)), new DataColumn("Column2", typeof(String)), new DataColumn("Column3", typeof(String)), }); string csvData = File.ReadAllText(csvPath); foreach (string row in csvData.Split('\n')) { if (!string.IsNullOrEmpty(row)) { dt.Rows.Add(); int i = 0; foreach (string

DataColumn set a default value

旧城冷巷雨未停 提交于 2021-01-27 07:34:17
问题 I am trying to set a default value for a DataColumn. How do you set a default value for DataColumn (column3) for the below code DataTable dt = new DataTable(); dt.Columns.AddRange(new DataColumn[] { new DataColumn("Column1", typeof(String)), new DataColumn("Column2", typeof(String)), new DataColumn("Column3", typeof(String)), }); string csvData = File.ReadAllText(csvPath); foreach (string row in csvData.Split('\n')) { if (!string.IsNullOrEmpty(row)) { dt.Rows.Add(); int i = 0; foreach (string

How to remove DataColumn from DataTable programmatically

拥有回忆 提交于 2020-02-15 05:42:08
问题 I have a code foreach (DataColumn dataTableCol in this.dataTable.Columns) { bool columnFound = false; foreach (GRTColumnView uiColumn in descriptor.UIColumns) { if (dataTableCol.ColumnName.Equals(uiColumn.Name)) { columnFound = true; break; } } if (!columnFound) { if (this.dataTable.Columns.Contains(dataTableCol.ColumnName)) this.dataTable.Columns.Remove(dataTableCol.ColumnName); } } I want to remove some "things" from collection if they aren't found in another collection. When I run the

How to remove DataColumn from DataTable programmatically

喜你入骨 提交于 2020-02-15 05:40:12
问题 I have a code foreach (DataColumn dataTableCol in this.dataTable.Columns) { bool columnFound = false; foreach (GRTColumnView uiColumn in descriptor.UIColumns) { if (dataTableCol.ColumnName.Equals(uiColumn.Name)) { columnFound = true; break; } } if (!columnFound) { if (this.dataTable.Columns.Contains(dataTableCol.ColumnName)) this.dataTable.Columns.Remove(dataTableCol.ColumnName); } } I want to remove some "things" from collection if they aren't found in another collection. When I run the

Customize DataColumn.Expression handling in C#

喜夏-厌秋 提交于 2020-01-23 02:32:47
问题 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? 回答1: 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

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

微笑、不失礼 提交于 2020-01-01 00:46:11
问题 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

How to dynamically add (or) insert values to data column in c#?

Deadly 提交于 2019-12-24 19:06:05
问题 I am iterating an excel file cell by cell. Each file has its own number of columns. Based on excel cell count, we are dynamically creating the columns to the data table. This part is working fine. I will have to insert each cell value to the data column. How to dynamically add (or) insert values to data column in c#? In an assumption, excel file has 2 rows and 3 colums FirstName LastName Location --------------------------- Albert B Miami Jackson C Tampa I will have to populate the data table

Ignore optional columns on save

自古美人都是妖i 提交于 2019-12-23 15:09:25
问题 When I update a table from a Form through Autogenerated EF, if I remove some data-columns from the view form because I don't want to be editable, that columns are updated with null value, how can avoid this behavior? I read here: Entity Framework: Ignore Columns removing it from the model, but not always I want to ignore these datacolumns. thank! 回答1: asp.net MVC provide you with UpdateModel method, look on the overload protected internal void UpdateModel<TModel>( TModel model, string prefix,

How do I use IF/ELSE or CASE In DataColumn.Expression?

青春壹個敷衍的年華 提交于 2019-12-23 09:14:47
问题 I have a table with 1 column: 'Status' I want to add in another column named 'Action', its value will be as follow: if Status = 'Yes' Then Action = 'Go', otherwise, Action = 'Stop'. I used this following code to add in column 'Action' but it didn't work: myDataTable.Columns.Add("Action", typeof(string), "IF [Status] = 'Yes' THEN 'Go' ELSE 'Stop' END"); 回答1: The expression you are looking for is: IIF( [Status] = 'Yes', 'Go', 'Stop' ) DataTables do not support standard SQL CASE statements, nor