3

Hey I have a gridView and I would like to extract the PhotoPath (ftppath + filename (ftp://192.168.1.2/Jelly.jpg))

its in asp.net and I'm not sure how to retrieve the data ive set the gridView up for selection and I like to "upon" select store the PhotoPath in a string.

source

<asp:GridView ID="GridView1" runat="server" AllowSorting="True" 
        AutoGenerateColumns="False" DataKeyNames="StudentID" 
        DataSourceID="SqlDataSource1" Height="263px" Width="915px" 
        AllowPaging="True" CellPadding="4" ForeColor="#333333" GridLines="None" 
        onselectedindexchanged="GridView1_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" 
                ShowSelectButton="True" />
            <asp:BoundField DataField="No" HeaderText="MatricNo" 
                SortExpression="MatricNo" />
            <asp:BoundField DataField="FirstName" HeaderText="FirstName" 
                SortExpression="FirstName" />
            <asp:BoundField DataField="LastName" HeaderText="LastName" 
                SortExpression="LastName" />
            <asp:BoundField DataField="Current" HeaderText="CurrentModules" 
                SortExpression="CurrentModules" />
            <asp:BoundField DataField="PhotoPath" HeaderText="PhotoPath" 
                SortExpression="PhotoPath" />
        </Columns>
        <EditRowStyle BackColor="#2461BF" />
        <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
        <RowStyle BackColor="#EFF3FB" BorderColor="Blue" BorderStyle="Solid" 
            Font-Size="Small" />
        <SelectedRowStyle BackColor="#D1DDF1" BorderStyle="Solid" Font-Bold="True" 
            Font-Size="Small" ForeColor="#333333" HorizontalAlign="Center" />
        <SortedAscendingCellStyle BackColor="#F5F7FB" />
        <SortedAscendingHeaderStyle BackColor="#6D95E1" />
        <SortedDescendingCellStyle BackColor="#E9EBEF" />
        <SortedDescendingHeaderStyle BackColor="#4870BE" />
    </asp:GridView>

I tryed this:

        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        string PhotoPath;
        GridView1.CurrentRow.Cells[0].Value.ToString();
    }

But current row isn't part of the definition.

1 Answer 1

8

Try with

protected void GridView1_SelectedIndexChanging(object sender, EventArgs e) {
    string PhotoPath = "";
    // Get the currently selected row. Because the SelectedIndexChanging event
    // occurs before the select operation in the GridView control, the
    // SelectedRow property cannot be used. Instead, use the Rows collection
    // and the NewSelectedIndex property of the e argument passed to this 
    // event handler.
    GridViewRow row = GridView1.Rows[e.NewSelectedIndex];

    PhotoPath = row.Cells[5].Text;
}

or

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e) {
    string PhotoPath = "";
    GridViewRow row = GridView1.SelectedRow;

    PhotoPath = row.Cells[5].Text;
}
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks alot :) getting a small error with NewSelectedIndex tho Error 1 'System.EventArgs' does not contain a definition for 'NewSelectedIndex' and no extension method 'NewSelectedIndex' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)
To get the index of the newly-selected row, you need to handle the SelectedIndexChanging event instead. Then e should have the NewSelectedIndex property.
oops! You're right! Sorry for that. I have updated my answer accordingly
yup its ok got it :) maybe you could take a look at my newest post, very disturbing lol

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.