Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do you get DataGrid.SelectedItem.Text value

Status
Not open for further replies.

ZmrAbdulla

Technical User
Apr 22, 2003
4,364
AE
I have a datagrid shows list of employees.

SL/No EmpName Extension
..... ....... ..........


I want to click on the "EmpName" to redirect the page to the details of selected employee. The redirecting part is OK for me. But I don't know how to get the selected name value.
What I will use for it?
[tt]
<asp:ButtonColumn>
or
<asp:HyperLinkColumn>[/tt]

I have tried it with <asp:ButtonColumn> and

On SelectedIdex_Change
Code:
Dim EmpName As String = CStr(Me.DataGrid1.DataKeys(DataGrid1.SelectedItem.Cells(1).Text)).ToString
and
Code:
Dim EmpName As String = CStr(Me.DataGrid1.SelectedItem.Cells(1).ToString)
Both not worked...

any help?


________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
There's a few methods you could use. One of these would be to use a CommandField and set the ButtonType property to Link. You can then use the RowCommand method to get the details. e.g.
Code:
<asp:CommandField ButtonType="Link" HeaderText="Download" SelectText="Select" ShowSelectButton="true" />
Code:
    Protected Sub MyGridView_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles MyGridView.RowCommand
        Select Case e.CommandName
            Case "Select"
                ' Get the relevant parameter that the user selected
                Dim row As GridViewRow = MyGridView.Rows(e.CommandArgument)
                Dim myString As String = MyGridView.DataKeys(row.RowIndex).Value
                
        End Select

    End Sub
Also, remember to set the DataKeyNames property of the GridView to the relevant field that you need to retrieve.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Oops, sorry I just saw you said DataGrid rather than GridView so you are probably using an older version of the framework. If the above doesn't work, there are alternative methods we can show you.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
It is DataGrid and working on framework 2
vwd2005 express.
It(datagrid) was fitting correctly to my requirement.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
If you are using version 2.0 of the framework, use the GridView control instead of the DataGrid. It offers the same functionality but is a much improved version of it and then you'll be able to use the method above.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Thanks, I will come back if stuck anywhere.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
I have done it like
Code:
  Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim EmpName As String = GridView1.SelectedRow.Cells(1).Text
    End Sub
working fine..

But I am stuck some other part. datagrid had a column for serial number
Code:
 <asp:TemplateColumn HeaderText="S/N">
    <ItemTemplate>
    	<span>
    	<%# Container.ItemIndex+1 %>
     	</span>
     </ItemTemplate>
    	<ItemStyle HorizontalAlign="Center" />
     	 <HeaderStyle Width="30px" />
  </asp:TemplateColumn>
that is not supported now. It says code blocks are not allowed inside gridview..
any other way?

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
There's one of two methods I'd usually use.

1) Return the incrementing number as part of the SQL Statement
2) Use the RowDataBound event.

Method #1 should be fairly straight-forward, and #2 would just use an incrementing variable. The value of this variable could then just be assigned to a Label control that you place inside the ItemTemplate.

Let us know if you need an example of either method.


____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.

 
Just logged in...
I think No:1 will not work in my case because I am using MS Access at the back end. Let me know if it is possible..

I would apreaciate if you could give an example of the second one.

I also will do a search on the net in the meantime.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
got an inspiration from
now the code look like
Code:
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        rowNum = 0
    End Sub
Code:
    Public rowNum As Integer = 0
    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim gv As GridView = sender
 
            Dim lblRowNum As Label = e.Row.FindControl("RowNum")
            rowNum = rowNum + 1            
            lblRowNum.Text = rowNum
        End If
    End Sub

Code:
   <asp:TemplateField HeaderText="S/N">
    <ItemTemplate>
      <asp:Label ID="RowNum" runat="server" />
    </ItemTemplate>
   </asp:TemplateField>
.

It was not working in "GridView1_RowDataBound"

I am not using pages
Thanks again

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Strange, I'm not sure why the RowDataBound event didn't work as it should in theory. Anyway, glad you got it working.

For the SQL query, I don't really use Access if I can help it so I'm not sure if there is an easy way of returning row numbers or not. Google suggests there isn't, but I did come across this article:



____________________________________________________________

Need help finding an answer?

Try the Search Facility or read FAQ222-2244 on how to get better results.
 
People do rownumbering in Access SQL (complicated). I prefer to keep my SQL very straight.

________________________________________________________
Zameer Abdulla
Help to find Missing people
Sharp acids corrode their own containers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top