本文英文原版:
http://aspnet.4guysfromrolla.com/articles/042402-1.2.aspx
考察DataGrid控件: Part 3 (下)
导言:
在上半部分,我们概述了如何通过使用ButtonColumn标签为DataGrid控件添加按钮.此外,我们创建了相关的事件处理器以因对用户点击按钮的情况.在本节,我们探讨如何确定点击的按钮以及怎样采取对应的行为.
确定点击的是哪个按钮
记得前面那个应对“点击按钮”的事件处理器是这样定义的:
Sub eventHandlerName(sender as Object, e as DataGridCommandEventArgs)
...
End Sub
其中DataGridCommandEventArgs class类包含了一个Item属性,其返回的item包含了触发该事件的对象源(the source of the object).实际上该item就是TableRow class类的一个实例,对应于DataGrid控件里被点击的那个行记录.我们可以通过Cells属性来访问TableRow class类的列,获取其值.也就是说,假设我们有一个DataGrid,它的Columns collection是这样定义的:
<asp:DataGrid runat="server" ... >
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="FAQ Details" CommandName="details" />
<asp:BoundColumn DataField="FAQID" HeaderText="FAQ ID" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>
然后,在事件处理器里,我们可以引用那些被点击的行的值,像下面这样:
Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
Dim buttonColumn as TableCell = e.Item.Cells(0)
Dim FAQIDColumn as TableCell = e.Item.Cells(1)
Dim DescColumn as TableCell = e.Item.Cells(2)
Dim buttonColText as String = buttonColumn.Text
Dim FAQIDColText as String = FAQIDColumn.Text
Dim DescColText as String = DescColumn.Text
End Sub
演示页面:http://aspnet.4guysfromrolla.com/demos/dgExample7.aspx
记得前面我们假设过这种情况,某电子商务公司希望在显示货运信息时只列出部分信息,但同时允许顾客查看某笔交易的全部信息.但是到目前为止,我们的演示页面只列出了存储过程sp_Popularity返回的部分列,假设我们只想列出Description列,并提供一个名为“Details”的按钮供用户查看剩余的信息.
虽然我们不想将FAQID列显示在DataGrid里,但我们仍然需要找到一个途径为detailsClicked事件处理器传递FAQID,因为FAQID是表的主键,即每个FAQ的唯一标识.为达此目的,我们需要在DataGrid标签里做小小的改动.找到与FAQID列对应的BoundColumn 标签,我们只需要添加代码:Visible="False",这将隐藏该列,但通过e.Item.Cells(1).Text的形式,detailsClicked事件处理器仍然可以访问到适当的FAQID值.
我们需要做的是更新detailsClicked事件处理器,以获取并展示用户希望得到某条FAQ的详细信息,页面看起来应该像下面这个样子:
<script language="vb" runat="server">
Sub Page_Load(sender as Object, e as EventArgs)
If Not Page.IsPostBack then
BindData() 'Only bind the data on the first page load
End If
End Sub
Sub BindData()
'Make a connection to the database
'Databind the DataReader results to the gPopularFAQs DataGrid.
End Sub
Sub detailsClicked(sender as Object, e As DataGridCommandEventArgs)
'Get detailed information about the selected FAQ and bind
'the database results to the dgFAQDetails DataGrid
End Sub
</script>
<form runat="server">
<asp:DataGrid runat="server" id="dgFAQDetails" ... >
...
</asp:datagrid>
<asp:DataGrid runat="server" id="dgPopularFAQs" ... >
<Columns>
<asp:ButtonColumn Text="Details" HeaderText="FAQ Details"
ButtonType="PushButton" />
<asp:BoundColumn DataField="FAQID" Visible="False" />
<asp:BoundColumn DataField="Description" HeaderText="FAQ Description" />
</Columns>
</asp:datagrid>
</form>
演示页面:http://aspnet.4guysfromrolla.com/demos/dgExample8.aspx
要注意的第一件事情是页面包含2个DataGrid控件,第一个名为dgFAQDetails,用于显示某个特定FAQ的详细信息;第二个名为dgPopularFAQs,用于显示最常见的10个问题.我们注意到在dgPopularFAQs里,我们对FAQID BoundColumn添加了:Visible="False",结果就是其不会显示在dgPopularFAQs的界面里.
结语:
本节我们考察了为每行添加一个按钮,并创建对应的事件处理器,在以后的部分我们还将探讨排序、分页、编辑数据等功能。
祝编程快乐!
分享到:
相关推荐
vb中datagrid控件的详细使用方法,拿出来给大家看看!!!!是学习datagrid控件的好助手
DataGrid控件是操作表格、数据库的重要控件,这里是其用法详解
Windows窗体DataGrid控件是NET 1.0/1.1推出的控件,在VS2005中亦可以继续使用,虽然它在功效方面不如NET 2.0新增的控件DataGridView,但它仍有用武之处,特别是对于已完成的项目中使用了该组件时,没有必要更换它,...
DataGrid 控件是一种类似于电子数据表的绑定控件,可以显示一系列行和列来表示 Recordset 对象的记录和字段。可以使用 DataGrid 来创建一个允许最终用户阅读和写入到绝大多数数据库的应用程序。DataGrid 控件可以在...
解压后,点bat对datagrid 控件 进行注册
VC++中DataGrid控件的使用,讲解与数据库绑定并且操作,内部包括所有代码
在DataGrid控件中设置单元格的数据显示格式.
博图WinCC V16用ADODB、DataGrid控件VB脚本显示SQL Server数据
用于解决vb6精简版没有datagrid控件的情况 安装完成后,可能出现“该部件的许可证信息没有找到,在设计环境中,没有合适的许可证使用该功能”提示 请运行vb6cli.exe,该软件我已上传至csdn。并且微软官网提供下载。
对MFC中的DataGrid控件的用法进行了一些总结
关于DataGrid 控件的样式和模板,内容很实用
asp.net用datagrid控件连接数据库的两个实例.
WPF DataGrid 控件实例源码 WPF DataGrid 控件实例源码 来自网上
带有上下翻页跳转的DataGrid控件,可以自动翻页的DataGrid控件
.net下功能強大的Datagrid控件 .net下功能強大的Datagrid控件
运用ASP.NET开发Web应用程序过程中,DataGrid是一个非常重要的控件,几乎任何和数据相关的表现都要用到该控件。所以熟练掌握DataGrid控件的应用技巧是每个Web开发人员所必备的基本能力。
VC使用ADO Data和DataGrid控件实现遍历、修改、删除、添加 所有数据库都大同小异。
C# WPF DataGrid控件实现三级联动 博客原文:http://blog.csdn.net/weixinhum/article/details/49449973
felx和springHibernate整合传递二进制数据到表中并在flex的dataGrid控件中显示的实践