var member = new Member(); int recordCount; var memberInfos = member.Paging(20, 1, "MemberId DESC", "", out recordCount); var memberIdSum = memberInfos.Sum(c => c.MemberId); Response.Write(memberIdSum.ToString()); Response.Write("<br/>——————————————————-<br />"); var dbHelper = DbFactory.AccessSqlServer; var dbPaging = DBPaging.Create(dbHelper); var ds = […]
View Details相对而言,LINQ TO DataSet是LINQ技术中最小的一块,虽然是DB中抽取出来的一个离线的操作模型,但毕竟对象也是个内存里面的object而已。所以和LINQ TO Object相比,大多数的操作都是一样的,不同只是要根据DataSet,DataTable的结构标明字段而已。下面简单的列出LINQ TO DataSet相比LINQ TO Object一些要注意的特色。 Query UnTyped DataSet 和一般的LINQ相比,query对象是untyped DataSet的时候,使用Field<T>和SetField<T>来读写不同的column字段,下面是一个简单的例子: DataTable orders = ds.Tables["Orders"]; DataTable orderDetails = ds.Tables["OrderDetails"]; var query = from o in orders.AsEnumerable() where o.Field<DateTime>( "OrderDate" ).Year >= 1998 orderby o.Field<DateTime>( "OrderDate" ) descending select o; 在这里大致要注意三点 1.因为untyped DataSet没有实现IEnumerable<T> 和 IQueryable<T>的interface,所以如果想把它作为一个可以查询的对象的话,要先用AsEnumerable() 或者AsQueryable()转换一下,将它转换成IEnumerable<T>或者IQueryable<T>对象才能用LINQ去查询。如:from o in orders.AsEnumerable() 2.一般是使用使用Field<T>(“Column A”)和SetField<T>(“Column A”)来读写不同的column字段对应的element,用它来访问相对于以前我们用ds.Tables["Orders"].Row[“RowA”][ “Column A”]的访问模式比起来,一个很大的好处就是可以避免null类型产生的exception。我们以前从DataSet里面取数据的时候,如果取的出来的是null,就会抛出exception,所以我们经常作类似if(ds.Tables["Orders"].Row[“RowA”][ “Column A”]!=null)的判断来包装我们进一步的逻辑处理,但是用Field<T>(“Column A”)就可以避免这种麻烦。因为Field<T>(“Column A”)是nullable的。这个特性的由来是<T>这个泛型的使用,比如你取int类型数据的时候,如果你觉得它可能是null,那你就可以用Field<int?>(“Column A”)去取,这样就可以避免了exception的抛出。 3 .Field<T>和SetField<T>是使用并不局限在LINQ 的query当中,在程序的其他地方也能使用,可以用它去替代以前的我们访问DataSet的方式,例如:
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">foreach( DataRow r in orderDetails.Rows ) {</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> if (r.Field<decimal>( "UnitPrice" ) < 10 ){</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> r.SetField<decimal>( "UnitPrice", 10 );</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> }</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">}</font></span> |
Query Typed DataSet 这就更加简单了。对于定义了类型的DataSet,我们可以象查询内存中一般的object那样去查询它。例如:
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">var query =</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> from o in ds.Orders</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> where o.OrderDate.Year >= 1998</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orderby o.OrderDate descending</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> select new { o.OrderID, o.OrderDate,</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> Amount = o.GetOrder_DetailsRows().Sum(</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> od => od.UnitPrice * od.Quantity ) };</font></span> |
还有一个与untyped DataSet不同的地方是在查询它的时候不需要使用AsEnumerable() 或者AsQueryable()那样的转换方法了。因为所有定义好的DataSet都是继承了TypedTableBase<T>这个基类,而这个基类已经实现了IEnumerable<T>的interface Query DataSet中的relation DataSet当中有时候也是有relation的,和DB一样,例如在下面的DataSet中加入relation:
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">DataTable orders = ds.Tables["Orders"];</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">DataTable orderDetails = ds.Tables["OrderDetails"];</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2">ds.Relations.Add( "OrderDetails",</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orders.Columns["OrderID"],</font></span> |
|
1 |
<span style="background-image: initial; background-attachment: initial; background-origin: initial; background-clip: initial; background-color: silver; "><font size="2"> orderDetails.Columns["OrderID"]);</font></span> |
如果我们想像在LINQ […]
View Details1.数据集( DataSet ) • DataSet 是更为广泛使用的ADO.NET 组件之一,它可以显式缓存不同数据源中的数据。 • 在表示层上DataSet 与GUI 控件紧密集成,以进行数据绑定。 • 在中间层上,它提供保留数据关系形状的缓存并包括快速简单查询和层次结构导航服务,从而可以减少对数据库的请求数。 2.查询数据集 • DataSet 虽然具有突出的优点,但其查询功能也存在限制。 • Select 方法可用于筛选和排序,GetChildRows和GetParentRow 方法可用于层次结构导航。 • 但对于更复杂的情况,开发人员必须编写自定义查询。这会使应用程序性能低下并且难以维护。 3.使用LINQ to DataSet • 使用LINQ to DataSet 可以更快更容易地查询在DataSet 对象中缓存的数据。 • 这些查询用编程语言本身表示,而不表示为嵌入在应用程序代码中的字符串。 • LINQ to DataSet 可使Visual Studio 开发人员的工作效率更高, 因为Visual Studio IDE 提供编译时语法检查、静态类型化和对 LINQ 的智能感知的支持。 • LINQ to DataSet 也可用于查询从一个或多个数据源合并的数据 这可以使许多需要灵活表示和处理数据的方案能够实现。 4.查询数据集 • 填充DataSet – XxxDataAdapter – LINQ to SQL •主要使用下面两个扩展类 – DataRowExtensions – DataTableExtensions • 查询支持 – 类型化数据集 – 非类型化数据集 5.启用LINQ to DataSet 功能 • 要求.NET Framework 3.5 • 引用System Data DataSetExtensions 程序集 […]
View DetailsLinQ家族五大成员:LinQ to Objects – 默认功能,用来实现对内存中集合对象的查询LinQ to SQL – 针对SQL Server的查询,它是一个带有可视化的操作界面的ORM工具LinQ to DataSet – 对强类型化或弱类型化的DataSet或独立的DataTable进行查询LinQ to Entity – 对实体框架中EDM定义的实体集合进行查询。LinQ to XML – 对XML文档进行查询创建等操作。 C#语法与LinQ相关的新增功能 1.隐式强类型变量在C#3.0中可以使用var关键字隐式定义强类型局部变量。 《图1》这里的var关键字定义变量与JavaScript定义变量看起来很像但二者有着本质的区别。JavaScript定义的变量是弱类型的变量,也可理解为是一种通用类型的变量,它可以容纳各种类型的值,还可以在运行过程中动态修改其中的内容类型。下面这种写法在JavaScript中是正确的:var obj = 3.14;obj = "hello world";C#中的var则是种强类型的变量,它在定义的时候会确定数据类型,分配内存空间。上面这两名代码在C#3.0中会报错,因为第一句已经把obj定义为double型的变量,第二句把字符串赋值给double是错误的操作。 隐式强类型并不能有效简化我的书写的代码,但当我们在用它来动态接收一些未知类型的数据的时候就显虽得很强大。在隐式强类型变量出现前,我们一般是使用object型变量来接收这种未知类型的数据的。 2.对象初始化这个功能可以有效简化类的getter和setter部份的代码,还可以只使用一行表达式语句实现对象的实例化与初始化操作。如定义实例类时可以使用如下代码,没有必要再把成员变量和属性分别定义了。public class LineItem{ public int OrderID { get; set; } public int ProductID { get; set; } public short Quantity { get; set; } public string QuantityPerUnit { get; set; } public decimal UnitPrice { get; set; } public float Discount { get; set; }} 实例化LineItem对象,并为它赋值var line3 = new LineItem { OrderID = 11000, ProductID = 61, Quantity = […]
View Detailsyield 关键字向编译器指示它所在的方法是迭代器块。 编译器生成一个类来实现迭代器块中表示的行为。 在迭代器块中,yield 关键字与 return 关键字结合使用,向枚举器对象提供值。 这是一个返回值,例如,在 foreach 语句的每一次循环中返回的值。 yield 关键字也可与 break 结合使用,表示迭代结束。 有关迭代器的更多信息,请参见迭代器(C# 编程指南)。 下面的示例演示两种形式的 yield 语句。 示例:
|
1 |
<ol class="dp-c"><li class="alt"><span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> List </span></span></li><li><span>{ </span></li><li class="alt"><span> <span class="comment">//using System.Collections;</span><span> </span></span></li><li><span> <span class="keyword">public</span><span> </span><span class="keyword">static</span><span> IEnumerable Power(</span><span class="keyword">int</span><span> number, </span><span class="keyword">int</span><span> exponent) </span></span></li><li class="alt"><span> { </span></li><li><span> <span class="keyword">int</span><span> counter = 0; </span></span></li><li class="alt"><span> <span class="keyword">int</span><span> result = 1; </span></span></li><li><span> <span class="keyword">while</span><span> (counter++ < exponent) </span></span></li><li class="alt"><span> { </span></li><li><span> result = result * number; </span></li><li class="alt"><span> yield <span class="keyword">return</span><span> result; </span></span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> <span class="keyword">static</span><span> </span><span class="keyword">void</span><span> Main() </span></span></li><li><span> { </span></li><li class="alt"><span> <span class="comment">// Display powers of 2 up to the exponent 8:</span><span> </span></span></li><li><span> <span class="keyword">foreach</span><span> (</span><span class="keyword">int</span><span> i </span><span class="keyword">in</span><span> Power(2, 8)) </span></span></li><li class="alt"><span> { </span></li><li><span> Console.Write(<span class="string">"{0} "</span><span>, i); </span></span></li><li class="alt"><span> } </span></li><li><span> } </span></li><li class="alt"><span>} </span></li><li><span><span class="comment">/*</span> </span></li><li class="alt"><span><span class="comment">Output:</span> </span></li><li><span><span class="comment">2 4 8 16 32 64 128 256 </span> </span></li><li class="alt"><span><span class="comment">*/</span><span> </span></span></li></ol> |
环境: windows server 2003 、 iis6.0 、 php5.3.2 、 fcgi_1.5_x86 在winodws server 2003 上利用iis6.0搭建web服务器,采用php-cgi模式运行php脚本,提示如下错误!错误提示: FastCGI ErrorThe FastCGI Handler was unable to process the request.——————————————————————————– Error Details: Error Number: 14001 (0x800736b1).Error Description: 由于应用程序配置不正确,应用程序未能启动。重新安装应用程序可能会纠正这个问题。HTTP Error 500 – Server Error.Internet Information Services (IIS) 错误可能原因: 一.没有安装.net 2.0 的框架 二.没有安装VC9运行库即VISUAL C++ 2008 解决方法:⒈安装2.0版本以上的.net Framework.net Framework下载地址:页面地址:http://www.microsoft.com/downloads/zh-cn/details.aspx?FamilyID=D0E5DEA7-AC26-4AD7-B68C-FE5076BBA986软件地址:http://download.microsoft.com/download/2/0/E/20E90413-712F-438C-988E-FDAA79A8AC3D/dotnetfx35.exe⒉安装VC9运行库即VISUAL C++ 2008,Microsoft Visual C++ 2008 Redistributable Package (x86)下载地址:页面地址:http://www.microsoft.com/downloads/details.aspx?FamilyID=9B2DA534-3E03-4391-8A4D-074B9F2BC1BF&displaylang=zh-cn软件地址:http://download.microsoft.com/download/7/5/0/7502f4e9-1f90-4895-9259-1bde67b8b9a1/vcredist_x86.exe ⒊安装完成后重新启动iis 转自:http://www.huqiang.name/2012/05/03/archives/1678.html
View DetailsFastCGI的IIS模块可以让流行的应用框架在IIS上支持FastCGI协议以提供高性能和可用性的访问服务,FastCGI面向现有的网络服务器提供高性能的CGI替代标准。具体PHP 安装方法我记录下来,其中包括出现的一个错误.环境: 操作系统:Windows 2003 Server PHP版本:PHP5.25 1.下载FastCGI For IIS6 http://www.microsoft.com/downloads/details.aspx?FamilyID=2d481579-9a7c-4632-b6e6-dee9097f9dc5&displaylang=en 下载之后,双击运行进行安装. 2.下载PHP5.25 Windows版 http://www.php.net 下载.zip格式的版本,下载后解压至D:\PHP目录,并给IIS启动帐户组或用户赋予读取和运行权限. 3.注册PHP到FastCGI 在命令行下运行 cscript fcgiconfig.js -add -section:"PHP" -extension:php -path:"D:\PHP\php-cgi.exe" fcgiconfig.js 默认存放目录是:C:\WINDOWS\system32\inetsrv 4.设置 将D:\PHP\php.ini-recommended重命名为为D:\PHP\php.ini 打开D:\PHP\php.ini,修改: extension_dir = "D:\PHP\ext" fastcgi.impersonate = 1 cgi.fix_pathinfo=1 cgi.force_redirect = 0 其它根据实际需要对php.ini进行设置修改 然后执行: cscript fcgiconfig.js -set -section:"PHP" -InstanceMaxRequests:500 cscript fcgiconfig.js -set -section:"PHP" -EnvironmentVars:PHP_FCGI_MAX_REQUESTS:1000 以上两项设置最大池和响应连接数,可以根据运行机器的硬件配置、使用情况修改 一切完毕之后重启IIS,赶紧写一个测试页试一下吧. 我第一次安装时,由于为了服务器的安全我删除了wsshell命令,导致在第3步时出现了如下错误: Microsoft JScript 运行时错误: Automation 服务器不能创建对象 后通过重新注册命令:regsvr32 WSHom.ocx 问题解决 重启IIS后,打开测试页出现如下提示: FastCGI Error The FastCGI Handler was unable to process the request. ——————————————————————————-- Error Details: Error Number: 5 (0x80070005). Error Description: 拒绝访问。 HTTP Error 500 – Server Error. Internet Information Services (IIS) 这个错误是由于在解压PHP之后,没有对IIS启动帐户赋予该目录的读取和运行权限.修改文件夹安全属性,问题解决. 转自:http://www.jb51.net/article/16953.htm
View Details取出richTextBox里面的内容第一种方法:将richTextBox的内容以字符串的形式取出string xw = System.Windows.Markup.XamlWriter.Save(richTextBox.Document);第二种方法:将richTextBox的类容以二进制数据的方法取出FlowDocument document = richTextBox.Document;System.IO.Stream s = new System.IO.MemoryStream(); System.Windows.Markup.XamlWriter.Save(document, s); byte[] data = new byte[s.Length];s.Position = 0;s.Read(data, 0, data.Length);s.Close(); 赋值给richTextBox 第一种方法:将字符串转换为数据流赋值给richTextBox中 System.IO.StringReader sr = new System.IO.StringReader(xw);System.Xml.XmlReader xr = System.Xml.XmlReader.Create(sr);richTextBox1.Document = (FlowDocument)System.Windows.Markup.XamlReader.Load(xr);第二种方法:将二进制数据赋值给richTextBoxSystem.IO.Stream ss = new System.IO.MemoryStream(data);FlowDocument doc = System.Windows.Markup.XamlReader.Load(ss) as FlowDocument;ss.Close();richTextBox1.Document = doc; 清空RichTextBox的方法 System.Windows.Documents.FlowDocument doc = richTextBox.Document;doc.Blocks.Clear(); 如何将一个String类型的字符串赋值给richTextBoxmyRTB.Document = new FlowDocument(new Paragraph(new Run(myString))); FlowDocument doc = new FlowDocument();Paragraph p = new Paragraph(); // Paragraph 类似于 html 的 P 标签Run r = new Run(myString); // Run 是一个 Inline 的标签p.Inlines.Add(r);doc.Blocks.Add(p);myRTB.Document = […]
View Details给大家讲到WPF,我们就不得不提到WinForm,51CTO之前也报导过《WPF与WinForm对比谈 多线程编程优化是关键》的技术文章,对于他们之间的优劣有了更深刻的认识。而今天我们要讲到的是WPF 4中的DataGrid空间。我们也诚挚向您推荐《让你的代码“炫”起来——WPF开发教程》。 提到DataGrid 不管是网页还是应用程序开发都会频繁使用。通过它我们可以灵活的在行与列间显示各种数据。本篇将详细介绍WPF 4 中DataGrid 的相关功能。 自定义列 默认情况下,当我们为DataGrid 控件设置ItemSource 属性后,DataGrid 会根据数据类型自动生成相应的列,下表列出DataGrid 支持的四种列及其数据类型。 在创建DataGrid 时可以通过AutoGenerateColumns 属性设置列是否自动生成,从而加入自定义列。如果DataGrid 中同时包含“自动生成列”与“用户自定义列”,则首先创建“用户自定义列”。下面代码实例中分别创建这四种列:
|
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">Window</span><span> </span><span class="attribute">x:Class</span><span>=</span><span class="attribute-value">"WPF4ControlTest.MainWindow"</span><span> </span></span></li><li><span> </span><span class="attribute">xmlns</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml/presentation"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:x</span><span>=</span><span class="attribute-value">"http://schemas.microsoft.com/winfx/2006/xaml"</span><span> </span></li><li><span> </span><span class="attribute">xmlns:local</span><span>=</span><span class="attribute-value">"clr-namespace:WPF4ControlTest"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">xmlns:assembly</span><span>=</span><span class="attribute-value">"clr-namespace:System;assembly=mscorlib"</span><span> </span></li><li><span> </span><span class="attribute">Title</span><span>=</span><span class="attribute-value">"MainWindow"</span><span> </span><span class="attribute">Height</span><span>=</span><span class="attribute-value">"200"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"500"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider</span><span> </span><span class="attribute">x:Key</span><span>=</span><span class="attribute-value">"sexEnum"</span><span> </span><span class="attribute">MethodName</span><span>=</span><span class="attribute-value">"GetValues"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">ObjectType</span><span>=</span><span class="attribute-value">"{x:Type assembly:Enum}"</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">x:Type</span><span> </span><span class="attribute">Type</span><span>=</span><span class="attribute-value">"local:SexOpt"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider.MethodParameters</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">ObjectDataProvider</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">Window.Resources</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Name"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Name}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridTextColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Age"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"50"</span><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Age}"</span><span class="tag">/></span><span> </span></li><li><span> </span><span class="tag"><</span><span class="tag-name">DataGridComboBoxColumn</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"80"</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Sex"</span><span> </span></li><li class="alt"><span> </span><span class="attribute">SelectedItemBinding</span><span>=</span><span class="attribute-value">"{Binding Sex}"</span><span> </span></li><li><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding Source={StaticResource sexEnum}}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridCheckBoxColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Pass Exam?"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"100"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Pass}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"><</span><span class="tag-name">DataGridHyperlinkColumn</span><span> </span><span class="attribute">Header</span><span>=</span><span class="attribute-value">"Email"</span><span> </span><span class="attribute">Width</span><span>=</span><span class="attribute-value">"150"</span><span> </span></li><li><span> </span><span class="attribute">Binding</span><span>=</span><span class="attribute-value">"{Binding Email}"</span><span class="tag">/></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">DataGrid.Columns</span><span class="tag">></span><span> </span></li><li><span> </span><span class="tag"></</span><span class="tag-name">DataGrid</span><span class="tag">></span><span> </span></li><li class="alt"><span> </span><span class="tag"></</span><span class="tag-name">Grid</span><span class="tag">></span><span> </span></li><li><span class="tag"></</span><span class="tag-name">Window</span><span class="tag">></span><span> </span></li></ol> |
其中每列都包含各自的数值类型,在C# 中创建Member 类及SexOpt 枚举,并将memberData 数据绑定到DataGrid:
|
1 |
<ol class="dp-c"><li class="alt"><span><span class="keyword">using</span><span> System; </span></span></li><li><span class="keyword">using</span><span> System.Windows; </span></li><li class="alt"><span class="keyword">using</span><span> System.Collections.ObjectModel; </span></li><li><span> </span></li><li class="alt"><span class="keyword">namespace</span><span> WPF4ControlTest </span></li><li><span>{ </span></li><li class="alt"><span> </span><span class="comment">/// <summary> </span><span> </span></li><li><span> </span><span class="comment">/// Interaction logic for MainWindow.xaml </span><span> </span></li><li class="alt"><span> </span><span class="comment">/// </summary> </span><span> </span></li><li><span> </span><span class="keyword">public</span><span> partial </span><span class="keyword">class</span><span> MainWindow : Window </span></li><li class="alt"><span> { </span></li><li><span> </span><span class="keyword">public</span><span> MainWindow() </span></li><li class="alt"><span> { </span></li><li><span> InitializeComponent(); </span></li><li><span>ObservableCollection<Member> memberData = </span><span class="keyword">new</span><span> ObservableCollection<Member>(); </span></li><li><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li class="alt"><span> { </span></li><li><span> Name = </span><span class="string">"Joe"</span><span>, Age = </span><span class="string">"23"</span><span>, Sex = SexOpt.Male, </span></li><li class="alt"><span> Pass = </span><span class="keyword">true</span><span>, Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Joe@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Mike"</span><span>, Age = </span><span class="string">"20"</span><span>, </span></li><li><span> Sex = SexOpt.Male, Pass = </span><span class="keyword">false</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Mike@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> memberData.Add(</span><span class="keyword">new</span><span> Member() </span></li><li><span> { </span></li><li class="alt"><span> Name = </span><span class="string">"Lucy"</span><span>, Age = </span><span class="string">"25"</span><span>, </span></li><li><span> Sex = SexOpt.Female, Pass = </span><span class="keyword">true</span><span>, </span></li><li class="alt"><span> Email = </span><span class="keyword">new</span><span> Uri(</span><span class="string">"mailto:Lucy@school.com"</span><span>) </span></li><li><span> }); </span></li><li class="alt"><span> dataGrid.DataContext = memberData; </span></li><li><span> } </span></li><li class="alt"><span> } </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">enum</span><span> SexOpt { Male, Female }; </span></li><li><span> </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">class</span><span> Member </span></li><li><span> { </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Name { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">string</span><span> Age { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> SexOpt Sex { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> </span><span class="keyword">public</span><span> </span><span class="keyword">bool</span><span> Pass { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li class="alt"><span> </span><span class="keyword">public</span><span> Uri Email { </span><span class="keyword">get</span><span>; </span><span class="keyword">set</span><span>; } </span></li><li><span> } </span></li><li class="alt"><span>} </span></li></ol> |
这样我们就可以通过自定义列的方式创建出DataGrid: 选择模式 默认情况下,DataGrid 的选择模式为“全行选择”,并且可以同时选择多行(如下图所示),我们可以通过SelectionMode 和SelectionUnit 属性来修改DataGrid 的选择模式。 SelectionUnit:包含Cell、FullRow 和CellOrRowHeader 三种单元选择模式。 ◆Cell:选择单元格; ◆FullRow:选择全行; ◆CellOrRowHeader:可选择单元格,也可以通过点击行首选择全行; SelectionMode:分为Extended 和Single 两种模式。 ◆Extended:选择多个单元(单元格或全行,由SelectionUnit 定义); ◆Single:选择唯一单元(单元格或全行,由SelectionUnit 定义);
|
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span class="tag">></span><span>… … </span></li></ol> |
设置SelectionUnit 和SelectionMode 后的实例效果: 编辑 默认情况下,我们可以直接在DataGrid 中编辑数据(下表为相关的编辑命令),当然也可以通过IsReadOnly 属性将DataGrid 设置为只读方式。
|
1 |
<ol class="dp-xml"><li class="alt"><span><span class="tag"><</span><span class="tag-name">DataGrid</span><span> </span><span class="attribute">x:Name</span><span>=</span><span class="attribute-value">"dataGrid"</span><span> </span><span class="attribute">ItemsSource</span><span>=</span><span class="attribute-value">"{Binding}"</span><span> </span><span class="attribute">AutoGenerateColumns</span><span>=</span><span class="attribute-value">"False"</span><span> </span></span></li><li><span> </span><span class="attribute">SelectionUnit</span><span>=</span><span class="attribute-value">"Cell"</span><span> </span><span class="attribute">SelectionMode</span><span>=</span><span class="attribute-value">"Extended"</span><span> </span><span class="attribute">IsReadOnly</span><span>=</span><span class="attribute-value">"True"</span><span class="tag">></span><span>… … </span></li></ol> |
原文标题:WPF 4 DataGrid 控件(基本功能篇) 链接:http://www.cnblogs.com/gnielee/archive/2010/04/16/wpf4-datagrid-control-basic.html
View Details1.获取所有数据库名: SELECT Name FROM Master..SysDatabases ORDER BY Name 2.获取所有表名: SELECT Name FROM DatabaseName..SysObjects Where XType=’U' ORDER BY Name XType=’U':表示所有用户表; XType=’S':表示所有系统表; 3.获取所有字段名: SELECT Name FROM SysColumns WHERE id=Object_Id('TableName') access : 如果表存在,如何得到这个表? SELECT * FROM MSysObjects where name=’ExamResultTime ' and type=1 and flags=0 //得到该数据库中所有的表名 SELECT name FROM MSysObjects where type=1 and flags=0 转自:http://www.soaspx.com/dotnet/sql/mssql/sql2000/sqlserver2000_20091215_2051.html
View Details