Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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?
Posted
Updated 11-Jan-11 21:35pm
v3
Comments
Dalek Dave 12-Jan-11 3:35am    
Edited for Readability.

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 apply Select Filter to datatable to fetch whole row from datatable. It will return you collection of DataRow. Like

VB
Dim dataRows() As DataRow = dtChattable.Select(String.Format("Column1 LIKE '{0}%' AND Column3 = '{1}'", _
TextBox1.Text, TextBox2.Text))
 
Share this answer
 
Comments
Ankur\m/ 11-Jan-11 1:25am    
[moved from answer]
OP wrote:
my table doesn't have a primary key. more over it is loding into atext box any work arounf?
Hiren solanki 11-Jan-11 1:29am    
If not a primary key but atleast it should have multiple value from that row that can find row uniquely.
Dalek Dave 12-Jan-11 3:35am    
Good Answer.
Add a 'Select' CommandField Column to your GridView.
XML
<asp:CommandField HeaderText="Select" ShowSelectButton="True" />


Now when user selects a particular row, Row_Command event of the GridView is fired. So you do something like this

VB
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



By the way, you could have also tried Googling for a solution: select a row in gridview[^]

Hope this helps! :thumbsup:
 
Share this answer
 
Comments
Dalek Dave 12-Jan-11 3:35am    
Excellent.
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())


this will get the value of the first column of the select row

am hoping ur using vb.net
 
Share this answer
 
v2
Comments
Dalek Dave 12-Jan-11 3:36am    
Edited for Code Block.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900