获取数据表的选定行 [英] getting selected row of a datatable
问题描述
我有一个dataTable加载了数据,并设置为gridview控件的数据源.
当我单击该网格视图上的任何行时,我只需要获取该行.
I have a dataTable loaded with data, and set as a datasource for a gridview control.
When I click on any of the row on that gridview, I need to get that row only.
How is that possible?
推荐答案
是的,这很容易做到.
通过单击<code>
gridview中的一行,我想您将从该行中获得一些唯一键.一旦拥有一个或多个可以唯一标识该行的值,就可以对datatable
应用Select
过滤器以从datatable
获取整个行.它将返回您的DataRow集合.喜欢
Yes that is quite easily possible.
By clicking a row in a<code>
gridview I suppose that you will have some unique key from that row. Once you have one or multiple value that can identify that row uniquely you can applySelect
Filter todatatable
to fetch whole row fromdatatable
. It will return you collection of DataRow. Like
Dim dataRows() As DataRow = dtChattable.Select(String.Format("Column1 LIKE '{0}%' AND Column3 = '{1}'", _
TextBox1.Text, TextBox2.Text))
在GridView中添加选择"CommandField
列.
Add a ''Select''CommandField
Column to your GridView.
<asp:CommandField HeaderText="Select" ShowSelectButton="True" />
现在,当用户选择特定的行时,将触发GridView的Row_Command
事件.所以你要做这样的事情
Now when user selects a particular row, Row_Command
event of the GridView is fired. So you do something like this
Protected Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs)
If e.CommandName = "Select" Then
'get the row index
Dim rowIndex As Integer = Convert.ToInt32(e.CommandArgument) - 1 'in case there is no paging.
'Now get the value from GridView against this row.
'perform required operation
End If
End Sub
顺便说一句,您也可以尝试使用Google搜索解决方案: ^ ]
希望这可以帮助! :thumbsup:
By the way, you could have also tried Googling for a solution: select a row in gridview[^]
Hope this helps! :thumbsup:
要显示要形成的选择行或内容.
例如,如果唯一键中网格的第一列,则使用此
Do want to display the select row to form or what.
For example if the first column of the grid in the unique key then use this
CStr(datagridview1.Item(0, datagridview1.CurrentCell.RowIndex).Value())
这将获得选择行第一列的值
希望您使用vb.net
this will get the value of the first column of the select row
am hoping ur using vb.net
这篇关于获取数据表的选定行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!