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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Select a record with a gridview

Status
Not open for further replies.

pinkpoppy

Programmer
Jul 6, 2011
75
US
Hi
I am having a hard time selecting a row in a grid view to work. I've search just about everywhere for this solution.

I have a page(1) where records are displayed in a gridview. The autogenerateselectbutton is set to false so when users select a specific row, the row is highlighted. When the row is clicked, it is suppose to bring the user to the next page(2) which data is displayed in a listview.

The issue is when users click on a row in the gridview on first page. I can't seem to find the right solution to where it directs them to the page(2) so they can view the data.

The data are called from a web service. Each record that is displayed in the gridview has a unique id which is passed back. The unqiue id is not displayed, the only information displayed to users are the names, numbers, and email address in the gridview.

Any suggestions and help is very much appreciated!
 
You'll probably want to identify the unique id as a datakey for the grid view, its a client side property of the gridview;
Code:
DataKeyNames="UNIQUE_ID_NAME"

On the row command you can the retrieve the datakey for the row selected through the GridViewCommandEventArgs parameter;
Code:
protected void MyGridViewRowCommand(Object sender, GridViewCommandEventArgs e)
{
  using (GridView gridView = e.CommandSource as GridView)
  {
    if (string.CompareOrdinal(e.CommandName.ToUpper(), "SELECT") == 0)
    {
      int uniqueId = Convert.ToInt32(gridView.DataKeys[Convert.ToInt32(e.CommandArgument)][0]);
    }
  }
}

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Hi, thank you for your suggestion.

I have this for the girdview:
<asp:GridView ID="grdSearchResult" runat="server"
AutoGenerateColumns="False" DataKeyNames="Unique_ID">
<Columns>

<asp:BoundField DataField="Unique_ID" Visible="false" />

<asp:BoundField DataField="Name" HeaderText="Namer" />
<asp:BoundField DataField="Number" HeaderText="Client's Number" />
<asp:BoundField DataField="Email " HeaderText="Email Address" />

</Columns>

</asp:GridView>


Then I put your suggested code in page 1.aspx.cs

I ran the code and my gridview is not showing at all.

 
I don't know what type of object you are binding to, I would assume a DataTable. Replace the 'Unique_ID' portion of DataKeyNames="Unique_ID" with the name of the unique id field in your data source. For example, if the name of the unique id field was 'MyID' you would use DataKeyNames="MyID".

Does that make sense?

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Yes, it makes sense. The name of the unique id is unique id. I am trying to find why it is not executing. The gridview does not show up.

Thanks.
 
Can you debug and make sure your data is getting bound properly and there is data to display?

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
I debug; and excluded the DataKeyNames="Unique_ID" and it ran fine.

protected void grdSearchResult_RowCommand1(object sender, GridViewCommandEventArgs e)
{
using (GridView grdSearchResult = e.CommandSource as GridView)

if (string.CompareOrdinal(e.CommandName.ToUpper(), "SELECT") == 0)
{
int Unique_ID = Convert.ToInt32(grdSearchResult.DataKeys[Convert.ToInt32(e.CommandArgument)][0]);
}

}



<asp:GridView ID="grdSearchResult" runat="server" onrowcommand="grdSearchResult_RowCommand1"
AutoGenerateColumns="False"
>
<Columns>

<asp:BoundField DataField="Unique_ID" Visible="false" />

<asp:BoundField DataField="Name" HeaderText="Namer" />
<asp:BoundField DataField="Number" HeaderText="Client's Number" />
<asp:BoundField DataField="Email " HeaderText="Email Address" />

</Columns>

</asp:GridView>
 
What is the type of data in the Unique_ID field? I would guess it is, or is being interpreted, as a string...

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Is it recognised as an integer, and what are you binding to, an array, a simple collection, a complex collection?

Depending how your target page to display detail level information is set up you could always use a HyperLinkField and pass the Unique_ID in the QueryString. The below may not be exactly right as its pretty much off the top of my head;
Code:
<asp:HyperLinkField DataTextField="Name" HeaderText="Name" Target="_blank" DataNavigateUrlFields="Unique_ID" DataNavigateUrlFormatString="~/TargetPage.aspx?Unique_ID={0}" />

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
Best approach would probably be to figure the very basics of your detail display page out. I.E., create one if you haven't already and get one field, or even perhaps a FormView on there to display at least one element of your object detail.

If use of a QueryString parameter is acceptable, great, have a bit of a Google on using the HyperLinkField of a GridView and play around a bit. If its not and the issue with DataKeyNames persists you can probably get the value of the hidden field bound to the Unique_ID field in your code behind.

I would also strongly recommend playing around with this yourself as you will learn it a lot better. You can always work on optimising your solution afterwards once you're up and running.

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
No worries, have a good weekend :)

Rhys

"Technological progress is like an axe in the hands of a pathological criminal"
"Two things are infinite: the universe and human stupidity; and I'm not sure about the the universe"
Albert Einstein
 
I enable the select and code as below:
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles GridView1.SelectedIndexChanged
Dim row As GridViewRow = GridView1.SelectedRow
Try
strRcdID = row.Cells(1).Text
Session.Add("strRcdID", strRcdID)
Response.Redirect("(Your Page2).aspx")
Catch EX As Exception
End Try
End Sub
Remember that the gride starts with cell(0). When you click on the select on the row you want pass the record ID, you can move it to a sting and pass it to the next webpage. Very easy!

Good Luck
Cathy
 
Thanks for the help Cathy! I got the issue solved 2 days ago.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top