在
Pocket PC
应用程序中使用
DataGrid
控件
Christian Forsberg
businessanyplace.net
September 2003
Applies to:
Microsoft® .NET Compact Framework 1.0
Microsoft Visual Studio®.NET 2003
摘要:
学习如何在你的
Pocket PC
应用程序中如何有效地使用
DataGrid
控件进行数据查看和更新。这篇文章将解释,为什么这是重要的,并向你演示如何做。
Download datagrid_control.exe
Contents
Table Data
The DataGrid Control
DataGrid Sample
Code Walkthrough
Conclusion
Table Data
在实现企业级
Pocket PC
应用程序时,几乎总是需要以表格的方式浏览数据。大多数通用的方案是你输入许多查找标准,得到作为一个表的查找结果集,并从中搜索或选择。一个表格是一个非常直接的显示数据的途径,可以被你直接使用与屏幕空间中。
The DataGrid Control
包括在
Microsoft® Visual Studio® .NET 2003
中的
DataGrid
控件是一个强大的控件,使你能够以多种方式查看数据。这个控件可以被绑定到一个
DataTable,DataView,ArrayList
或者任何其他支持
IlistSource
或者
Ilist
接口的对象中。
让我们来看
DataGrid
控件是如何被用于一个
Pocket PC
应用程序实例的。
DataGrid Sample
这个
Pocket PC
的应用程序实例使用
Visual Studio .NET 2003, C#,
和
Microsoft .NET Compact Framework
创建。它将演示如何使用
DataGrid
控件查看并更新数据。这个程序由一个
form
组成:
Figure 1. DataGrid sample
这个实例开始时载入订单表(一个来自
Northwind sample database
的原始订单表的子集)到一个
grid
中,然后使用多种
column style
来创建一个好看的界面。点击
column header
可以对每一个
column
进行排序,再点击排序后的
column header
可以在升序和降序间切换。
一个非常好的功能是
grid
中的每一个
cell
都是可编辑的。当一个
cell
被点击并获得输入焦点时,
cell
中的值可以被更新(见图一)。当其他
cell
被选择时,被编辑过的
cell
被更新,新的
cell
变为可编辑。注意,当一个
cell
得到输入焦点,软键盘(
SIP
)是被自动激活(弹出)。一个可能的增进是例子可以拖动被选择的
cell
到可见,当它被
SIP
覆盖的时候。
现在,让我们看一下代码是如何实现的。
Code Walkthrough
在实例代码中,
DataGrid
控件被命名为
grdOrders
。将订单表(被存成
XML
文件)加载到
DataGrid
控件,你只需要下面的代码:
DataSet ds = new DataSet();
ds.ReadXml(@"/Program Files/DGrid/orders.xml");
grdOrders.DataSource = ds.Tables[0];
事实上,如果你只做了这些,那么将得到一个非常简单的
grid
,每个
column header
上将会显示表字段的名字。为了让
grid
看上去更好,使用
table styles
。一个
table style
可以包含多种
table styles
——你想在
grid
中查看每个
column
。在这个例子中,
columns
包括订购信息和收货人姓名。下面的代码设置状态:
DataGridTableStyle ts = new DataGridTableStyle();
ts.MappingName = "Order";
// Order date column style
DataGridColumnStyle orderDate = new DataGridTextBoxColumn();
orderDate.MappingName = "OrderDate";
orderDate.HeaderText = "Date";
ts.GridColumnStyles.Add(orderDate);
// Shipping name column style
DataGridColumnStyle shipName = new DataGridTextBoxColumn();
shipName.MappingName = "ShipName";
shipName.HeaderText = "Customer";
shipName.Width = this.Width - orderDate.Width - 37;
ts.GridColumnStyles.Add(shipName);
grdOrders.TableStyles.Add(ts);
每个
column style(DataGridColumnStyle)
设置一个表字段名称映射到
(MappingName
property)
,
header text
用于显示
(HeaderText
property)
,并自定义
column
宽度
(Width
property)
。两个
column styles
被加入到
table style
中,最后将
table style
加入到
DataGrid
控件
(grdOrders)
中。在这个例子里
styles
是被代码创建的,但是
styles
同样可以在
forms designer
中被创建。
当这个
grid
看上去很好时,有许多
common features
在
grid
中可以被支持。编辑就是其中之一。但编辑在
DataGrid
控件中不是被自动支持的,这就需要手工实现。一种方法就是使用一个隐藏的
TextBox
控件,当一个
cell
被选中时显示,当编辑结束时隐藏。这就需要你声明一些变量:
private DataGridCell editCell;
private bool inEditMode = false;
private bool inUpdateMode = false;
我们需要获取哪个
cell
被编辑激活
(editCell)
,这个
cell
是否在编辑状态
(inEditMode)
,和我们是否正在更新一个
cell(inUpdateMode).
控制编辑代码如下:
private void grdOrders_CurrentCellChanged(object sender,
System.EventArgs e)
{
if (!inUpdateMode)
{
if (inEditMode && !grdOrders.CurrentCell.Equals(editCell))
{
// Update edited cell
inUpdateMode = true;
grdOrders.Visible = false;
DataGridCell currentCell = grdOrders.CurrentCell;
grdOrders[editCell.RowNumber, editCell.ColumnNumber] =
txtEdit.Text;
grdOrders.CurrentCell = currentCell;
grdOrders.Visible = true;
inUpdateMode = false;
txtEdit.Visible = false;
inEditMode = false;
}
// Enter edit mode
editCell = grdOrders.CurrentCell;
txtEdit.Text = (string)grdOrders[editCell.RowNumber,
editCell.ColumnNumber];
Rectangle cellPos = grdOrders.GetCellBounds(editCell.RowNumber,
editCell.ColumnNumber);
txtEdit.Left = cellPos.Left - 1;
txtEdit.Top = cellPos.Top + grdOrders.Top - 1;
txtEdit.Width = cellPos.Width + 2;
txtEdit.Height = cellPos.Height + 2;
txtEdit.Visible = true;
inEditMode = true;
}
}
当一个
cell
被点击(
inEditMode
和
inUpdate
被设置为
false
)时,当前的
cell
被保存
(editCell)
。然后,隐藏的
TextBox(txtEdit)
得到当前
cell
的内容,重新设置到当前
cell
的位置,并使其可见
(
在当前的
cell
上边
)
。当编辑完成、其他
cell
被选择时,事件代码不可用
(inUpdateMode
设置成
true),
当被编辑的
cell
从
TextBox
控件得到后,将新的
cell
保存
(currentCell)
。在
update
过程中,
grid
没有被
update(Visible
设置成
false)
,当
update
完成
TextBox
被重新隐藏。
其他的
common grid feature
是通过点击
column header
进行排序。代码如下:
private void grdOrders_MouseUp(object sender,
System.Windows.Forms.MouseEventArgs e)
{
DataGrid.HitTestInfo hitTest = grdOrders.HitTest(e.X, e.Y);
if (hitTest.Type == DataGrid.HitTestType.ColumnHeader)
{
// Exit edit mode
txtEdit.Visible = false;
inEditMode = false;
// Sort tapped column
DataTable dataTable = (DataTable)grdOrders.DataSource;
DataView dataView = dataTable.DefaultView;
string columnName = dataTable.Columns[hitTest.Column].ColumnName;
if (dataView.Sort == columnName)
dataView.Sort = columnName + " DESC";
else
dataView.Sort = columnName;
}
}
如果
header
被点击,第一件事是确保任何当前
cell
的编辑被取消。然后一个
DataView
被创建并通过点击的
column
排序。如果一个
column
再次得到点击事件而排序顺序将被改成递减。后来的点击将使排序在递增和递减间切换,直到其他的
column
被点击。
Conclusion
DataGrid
控件是一个查看(然后是编辑)数据的强大工具,用于你的
Pocket PC
应用程序。这个控件提供多样性的
features
,而且这篇文章包含一些有趣的特性:数据绑定,
grid
内编辑,排序。你的下一步是在这个例子上做一些深层次的开发,探索这个控件中更多更
cool
的窍门。
分享到:
相关推荐
vb中datagrid控件的详细使用方法,拿出来给大家看看!!!!是学习datagrid控件的好助手
可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在设计时快速进行配置,只需少量代码或无需代码。当在设计时设置了DataGrid 控件的 DataSource 属性后,就会用...
运用ASP.NET开发Web应用程序过程中,DataGrid是一个非常重要的控件,几乎任何和数据相关的表现都要用到该控件。所以熟练掌握DataGrid控件的应用技巧是每个Web开发人员所必备的基本能力。
VC++中DataGrid控件的使用,讲解与数据库绑定并且操作,内部包括所有代码
在DataGrid控件中设置单元格的数据显示格式.
Windows窗体DataGrid控件是NET 1.0/1.1推出的控件,在VS2005中亦可以继续使用,虽然它在功效方面不如NET 2.0新增的控件DataGridView,但它仍有用武之处,特别是对于已完成的项目中使用了该组件时,没有必要更换它,...
DataGrid控件是操作表格、数据库的重要控件,这里是其用法详解
对MFC中的DataGrid控件的用法进行了一些总结
大家伙,我在这里给大家分享一下最近我在VB中使用的DataGrid控件的部分用法,希望大家下载分享!有更好的还望大家给我推荐,谢谢!
解压后,点bat对datagrid 控件 进行注册
ASP_NET中DataGrid控件的使用技巧.pdfASP_NET中DataGrid控件的使用技巧.pdfASP_NET中DataGrid控件的使用技巧.pdfASP_NET中DataGrid控件的使用技巧.pdfASP_NET中DataGrid控件的使用技巧.pdf
DataGrid控件高级使用 DataGrid控件高级使用
博图WinCC V16用ADODB、DataGrid控件VB脚本显示SQL Server数据
ASPNET程序中DataGrid控件的内置分页机理及效率提高的研究.pdf
felx和springHibernate整合传递二进制数据到表中并在flex的dataGrid控件中显示的实践
MSDATGRD.ocx exe安装检测无毒 Vista以后...安装完成后,可能出现“该部件的许可证信息没有找到,在设计环境中,没有合适的许可证使用该功能”提示 请运行vb6cli.exe,该软件我已上传至csdn。并且微软官网提供下载。
VC使用ADO Data和DataGrid控件实现遍历、修改、删除、添加 所有数据库都大同小异。
asp.net用datagrid控件连接数据库的两个实例.
有关VB中DataGrid空间的详细使用情况