How to pass parameter from Datagrid selected row to a text box

E

Ed Dror

Hi there,

I'm using Visual studio 2005 pro and I want to pass parameter from datagrid selected cell to textbox

I have Select button in my datagrid

I wrote this

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
Try
If e.Row.RowType = DataControlRowType.DataRow Then
Me.txtSubTypeID.Text = e.Row.Cells(1).Text
Me.txtBSP.Text = e.Row.Cells(8).Text
Me.txtALC_ID.Text = e.Row.Cells(3).Text
End If
Catch ex As Exception
ErrorMessage.Text = ex.Message.ToString
End Try
End Sub

But it always it show the last record nothing change when I'm using the SELECT button?

I tryied all other methods but e.Row working only with RowDataBound

How do I do that?

Thanks,

Ed Dror

(e-mail address removed)
 
W

Walter Wang [MSFT]

Hi Ed,

The RowDataBound event is fired for every row when it's bound to the data,
the last time it's called is when the last row is bound, that's why you
always see the last row's data.

Instead, you need to use the SelectedIndexChanged event to get the selected
row's data. Here's a demo:


<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub grid1_SelectedIndexChanged(ByVal sender As Object, ByVal
e As System.EventArgs)
Response.Write(grid1.SelectedItem.Cells(1).Text)
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<asp:DataGrid ID="grid1" runat="server" DataSourceID="SqlDataSource1"
CellPadding="4" ForeColor="#333333" GridLines="None"
OnSelectedIndexChanged="grid1_SelectedIndexChanged">
<Columns>
<asp:ButtonColumn Text="Select"
CommandName="Select"></asp:ButtonColumn>
</Columns>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
/>
<EditItemStyle BackColor="#2461BF" />
<SelectedItemStyle BackColor="#D1DDF1" Font-Bold="True"
ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White"
HorizontalAlign="Center" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#EFF3FB" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White"
/>
</asp:DataGrid>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:pubsConnectionString %>"
SelectCommand="SELECT [au_id], [au_lname], [au_fname], [phone],
[address] FROM [authors]">
</asp:SqlDataSource>
</form>
</body>
</html>


Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 
E

Ed Dror

Hi there,

when I'm run this page I'm geting error that says:

'SelectedItem' is not a member of 'System.Web.UI.WebControls.GridView'

there is no SelectedItem for Gridview

Thanks,
Ed Dror
 
W

Walter Wang [MSFT]

Hi Ed,

Since your post title mentioned "Datagrid", I thought you're using DataGrid
instead of GridView. (DataGrid is a server control existed in ASP.NET 1.1
and can be used in 2.0 too)

For GridView, you need to use GridView.SelectedRow.Cells.

Hope this helps.


Regards,
Walter Wang ([email protected], remove 'online.')
Microsoft Online Community Support

==================================================
When responding to posts, please "Reply to Group" via your newsreader so
that others may learn and benefit from your issue.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top