5 Replies - 3220 Views - Last Post: 13 March 2011 - 08:58 AM Rate Topic: -----

#1 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

GridView Selection

Posted 12 March 2011 - 09:33 AM

I have a GridView that is populated with a database, what i want is that when the user clicks "more" it opens up with the full amount of information from the database.

I imagine, it will mean putting the "Name" in the selected row, into a session, then using the contents of the new session in the new page, to get and display the full information using an SQL SELECT. The SQL is the easy part, my current problem is how i get the "name" in the selected row.

Not sure what code will help here, but here is the HTML code for that part

<asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataSourceID="CoffeeDataSource" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:ImageField DataImageUrlField="Image" HeaderText="Image">
            </asp:ImageField>
            <asp:BoundField DataField="CoffeeName" HeaderText="CoffeeName" 
                SortExpression="CoffeeName" />
            <asp:BoundField DataField="CoffeeStrength" HeaderText="CoffeeStrength" 
                SortExpression="CoffeeStrength" />
            <asp:BoundField DataField="Price" HeaderText="Price" SortExpression="Price" />
            <asp:CommandField ShowSelectButton="True" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="CoffeeDataSource" runat="server" 
        ConnectionString="<%$ ConnectionStrings:CoffeeDatabaseConnectionString %>" 
        
        SelectCommand="SELECT [CoffeeName], [CoffeeStrength], [Price], [Image] FROM [CoffeeInfo]">
    </asp:SqlDataSource>



I can only assume it involves this event

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }



Thanks for the help

~Jack

Is This A Good Question/Topic? 0
  • +

Replies To: GridView Selection

#2 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: GridView Selection

Posted 12 March 2011 - 02:37 PM

Oh uh, seems this thread got lost in the depths of the internets ;) Dont hate me for bumping this thread, but i really need help with this, googling it doesn't seem to help me...
Was This Post Helpful? 0
  • +
  • -

#3 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: GridView Selection

Posted 12 March 2011 - 03:49 PM

A span of 4 or 5 hours I don't think constitutes "lost in the depths of the internet", especially when everyone here that helps out just does so on a volunteer basis when they have the time. Anyway, let me try to get some clarification. You have a gridview with the select button enabled, is this the "more" button you are referring to? It looks like the gridview is already displaying the name, strength and price as well as the select button.
When they click the select button you are wanting it to open a new page? Or maybe a detailsview with more information or something like that?
Was This Post Helpful? 0
  • +
  • -

#4 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: GridView Selection

Posted 13 March 2011 - 03:38 AM

Sorry, Yeah it is a grid view with the select button enable, and i want to have it so when you click the select button, it goes to a new page with more information on it. I assume, in order to get the new information it will have to get something to indentify the Row in the database that is wanted from the selected row

> User Click's "more"
> Program, puts the contents of the "name" field into a session
> A new window opens, and an SQL query is run to find the correct row in the database, based on the contents of the session

Thats how i see it working, i can do the 3rd part (getting the info from a session) but what i am having issues with is getting the contents of the name field selected, from the row, in the gridview
Was This Post Helpful? 0
  • +
  • -

#5 Nakor  Icon User is offline

  • Professional Lurker
  • member icon

Reputation: 441
  • View blog
  • Posts: 1,488
  • Joined: 28-April 09

Re: GridView Selection

Posted 13 March 2011 - 06:35 AM

alright, during the selectedindexchanged event you should be able to use the SelectedRow property of the gridview to get the currently selected GridViewRow.

GridViewRow row = GridView1.SelectedRow;



The GridViewRow has a property named "Cells". Just pull the text from the correct table cell and put it into a Session variable, then redirect the user to the page you want. See if you can get that to work, if you have any problems/errors just post you're code and I'll see if I can help you figure out what's going on. Here's a few links that should help you out as well.

SelectedRow Reference

Response.Redirect Reference

Session State Reference
Was This Post Helpful? 0
  • +
  • -

#6 Atomika3000  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 88
  • Joined: 15-October 10

Re: GridView Selection

Posted 13 March 2011 - 08:58 AM

I did as you suggested, my "SelectedIndexChange" event now looks like this

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow row = GridView1.SelectedRow;
            Session["SelectedRow"] = row.Cells[1];
            Response.Redirect("Information.aspx");
        }


My "Page_Load" event, on the new page looks like

protected void Page_Load(object sender, EventArgs e)
        {
            SelectedItem.Text = (string)Session["SelectedRow"];
        }


but i get this error

Quote

Server Error in '/' Application.
Unable to cast object of type 'System.Web.UI.WebControls.DataControlFieldCell' to type 'System.String'.


Ignore above post, just realised i missed the ".Text" on the end of the session
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1