datatable

C# DataTable.Select() 筛选数据

时光怂恿深爱的人放手 提交于 2020-02-04 05:11:03
有时候我们需要对数据表进行筛选,微软为我们封装了一个公共方法, DataTable.Select(),其用法如下: Select() Select(string filterExpression) Select(string filterExpression, string sort) Select(string filterExpression,string sort, DataViewRowState record States) 1) Select()——获取所有 System.Data.DataRow 对象的数组; 2) Select(string filterExpression)——按照主键顺序(如果没有主键,则按照添加顺序)获取与筛选条件相匹配的所有 System.Data.DataRow 对象的数组; 3) Select(string filterExpression, string sort)——获取按照指定的排序顺序且与筛选条件相匹配的所有System.Data.DataRow 对象的数组; 4) Select(string filterExpression, string sort, DataViewRowState recordStates)——获取与排序顺序中的筛选器以及指定的状态相匹配的所有。 举例说明: 有一个用户表,名称为 dtUsers,有id

Bind complex object to cell of DataTable in C#

送分小仙女□ 提交于 2020-02-03 08:01:19
问题 I have a DataTable with complex objects. For example, class ComplexDataWrapper { public string Name{ get; set; } public ComplexData Data{ get; set; } public ComplexDataWrapper(ComplexData data) { this.Data = data; this.Name = "Something"; } public override string ToString() { return Name; } } And now I want to bind cells from DataTable to objects of ComplexDataWrapper So, I try something like this : ... var column = new DataColumn() { ColumnName = columnName, DataType = typeof

Bind complex object to cell of DataTable in C#

独自空忆成欢 提交于 2020-02-03 07:56:20
问题 I have a DataTable with complex objects. For example, class ComplexDataWrapper { public string Name{ get; set; } public ComplexData Data{ get; set; } public ComplexDataWrapper(ComplexData data) { this.Data = data; this.Name = "Something"; } public override string ToString() { return Name; } } And now I want to bind cells from DataTable to objects of ComplexDataWrapper So, I try something like this : ... var column = new DataColumn() { ColumnName = columnName, DataType = typeof

ADO.NET实用经验大总结

血红的双手。 提交于 2020-02-03 00:12:38
ADO.NET实用经验大总结 刷新DataSet中的数据 如果想用服务器上的更新值刷新DataSet中的值,就使用DataAdapter.Fill。如果有在DataTable上定义的主 键,DataAdapter.Fill会根据主键进行新行匹配,并且当更改到现有行时应用服务器上的值。即使刷新之前修改了这些数据,刷新行的 RowState仍被设置为Unchanged。注意,如果没有为DataTable定义主键,DataAdapter.Fill就用可能重复的主键值添加新行。 如果想用来自服务器的当前值刷新表,并同时保留对表中的行所做的任何更改,必须首先用DataAdapter.Fill填充表,并填充一个新的 DataTable,然后用preserveChanges值true将DataTable合并到DataSet之中。 在DataSet中搜索数据 在DataSet中查询与特定条件相匹配的行时,可以利用基于索引的查找提高搜索性能。当将PrimaryKey值赋给DataTable时,会创 建一个索引。当给DataTable创建DataView时,也会创建一个索引。下面是一些利用基于索引进行查找的技巧。 1) 如果对组成DataTable的PrimaryKey的列进行查询,要使用DataTable.Rows.Find而不是 DataTable.Select。 2) 对于涉及到非主键列的查询

django : create table form when selecting multiple rows

放肆的年华 提交于 2020-02-02 14:36:05
问题 I need to create a pretty straight forward form with Django but seems to be unable to find the proper tool for it, maybe because of the lack of vocabulary on what I want : I have a table of n rows (n varies), each row represents a database object. I want to put a checkbox in front left of each row to be able to select multiple rows and apply an action placed in a multiplechoice widget at the top. I thought about "serialize" a deleteview with formset but anyway I don't know how to add extra

Displaying datatable column values in dollars in Shiny r

独自空忆成欢 提交于 2020-02-02 10:14:27
问题 I have a datatable where one of the columns should be expressed in dollars and some as percentages. I've been looking around and I'm still not sure how to do it - seems like it would be easy? The trickier part is I have another data table where only certain entries need to be expressed as dollars (i.e. not whole rows or whole columns) - is there a way to handle this? 回答1: Imagine your datatable (myData) is 2 columns by 10 rows. You want the second row to be in dollars: myData[,2]<-sapply

DataGridView 索引,添加列

家住魔仙堡 提交于 2020-02-02 00:49:18
问题1:DataGridView绑定DataTable之后,在界面排序,选择若干行,此时选中的行的index与DataTable中的index不一致,这样若想要对其对应的行进行修改操作,将会造成错误。 Sol: 首先将选中的DataGridView行的DataBoundItem属性转换为DataRowView, 然后通过对应的DataTable中的取出索引。 1 DataRowView drv = dataGridView.SelectedRows[0].DataBoundItem as DataRowView; 2 int index = dataTable.Rows.IndexOf(drv.Row); 这是因为DataRowView对象中,有一个Row属性,获取 正被查看的 DataRow ,那么就可以在DataRow所在的表中找到它的索引啦。 问题2:为什么foreach遍历DataTable或者DataGridView对行进行增删修改的操作,会造成索引错误的问题? Sol: 这是因为foreach循环是从第一位开始遍历,如果对某一行进行删除操作,那么该行后面的DataRow(DataGridViewRow)的索引会提前一位,也即是说,DataRow(DataGridViewRow)的index是会实时变化的,因此会出错。比如索引为3被删除,那么删除后4的索引就变成3了

C#中把List转换为DataTable的方法

此生再无相见时 提交于 2020-02-01 03:12:13
private static DataTable ToDataTable<T>(IEnumerable<T> collection) { var props = typeof(T).GetProperties(); var dt = new DataTable(); dt.Columns.AddRange(props.Select(p => new DataColumn(p.Name, p.PropertyType)).ToArray()); if (collection.Count() > 0) { for (int i = 0; i < collection.Count(); i++) { ArrayList tempList = new ArrayList(); foreach (PropertyInfo pi in props) { object obj = pi.GetValue(collection.ElementAt(i), null); tempList.Add(obj); } object[] array = tempList.ToArray(); dt.LoadDataRow(array, true); } } return dt; } 来源: CSDN 作者: 知或谓之慧 链接: https://blog.csdn.net/destiny_98/article

How I can export from a DataTable to a Content control in C# using Openxml?

流过昼夜 提交于 2020-01-30 11:49:54
问题 So I have this code, but i want to know how possible it is to convert from a result gotten from an SQL DataTable to print on a Microsoft word content control My code looks something like this public bool PrintTableonContentControl(string connectionString, string DbQuery,string filePath, string placeholderText) { try{ DataTable dt = new DataTable(); using(SqlConnection con = new SqlConnection(connectionString)){ con.Open(); using(SqlCommand cmd = new SqlCommand(DbQuery,con)){ using

Allowing VB.NET app to convert Excel Files to Datatable

别说谁变了你拦得住时间么 提交于 2020-01-30 06:56:08
问题 My VB.NET app currently allows me to convert CSV files to a datatable thanks to the code provided by David in this question I posted: Previous Question Now I am trying to allow .XLSX files to be imported to a datatable as well. Currently the code looks like this: Private Function ConvertCSVToDataTable(ByVal path As String) As DataTable Dim dt As DataTable = New DataTable() Using con As OleDb.OleDbConnection = New OleDb.OleDbConnection() Try If System.IO.Path.GetExtension(path) = ".csv" Then