Retrieve currently selected Gridview Column value

I would like to retrieve a cell value in the currently selected row of a Gridview.  It seems pretty simple, but everything I've tried just gives me a blank value.  I've listed what I'm trying below, any direction on this would be greatly appreciated!  
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow row = GridView1.SelectedRow;
        CurrentlySelectedLabel.Text = "You selected " + row.Cells[3].Text + ".";
    }

Open in new window

delousedAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

MortenSlotKristensenCommented:
First of all, what columns do you have in the datagrid? Check that the cell at positon 3 (4) is actually there.
0
MortenSlotKristensenCommented:
Also, what is the value of: GridView1.SelectedRow.RowIndex  ?
0
delousedAuthor Commented:
Thanks for the reply.

How do I check if the cell is actually there?  

When I check the value of GridView1.SelectedRow.RowIndex  it just says 0, 1, 2, 3 etc. depending on the selected item.  
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

MortenSlotKristensenCommented:
Which columns have you added? Can I see the code for creating your gridview?
0
MortenSlotKristensenCommented:
About the cells.. You can check the count like this: GridView1.SelectedRow.Cells.Count
0
delousedAuthor Commented:
Here is what my gridview looks like:
<asp:GridView ID="GridView1" runat="server" 
                        AutoGenerateColumns="False" AllowPaging="True" AllowSorting="True" 
                        DataKeyNames="ID"
                        DataSourceID="ObjectDataSource1"                    
                        OnRowDataBound="GridView1_RowDataBound" 
                        OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                        <Columns>
                            <asp:TemplateField HeaderText="Id #" SortExpression="ID">
                                <ItemTemplate>
                                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Date" SortExpression="Date">
                                <ItemTemplate>
                                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Date") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                                <ItemTemplate>
                                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                            <asp:TemplateField HeaderText="Country" SortExpression="Country">
                                <ItemTemplate>
                                    <asp:Label ID="LocationLabel" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
                                </ItemTemplate>
                            </asp:TemplateField>
                        </Columns>
                    </asp:GridView>

Open in new window

0
MortenSlotKristensenCommented:
Okay. Doesn't any of these cells have a value?
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    GridViewRow row = GridView1.SelectedRow;
    CurrentlySelectedLabel.Text = row.Cells[0].Text + "; " +
                                  row.Cells[1].Text + "; " +
                                  row.Cells[2].Text + "; " +
                                  row.Cells[3].Text;
}

Open in new window

0
delousedAuthor Commented:
When I check GridView1.SelectedRow.Cells.Count it gives me a value of 4.  
0
MortenSlotKristensenCommented:
Then there should be values in the cells. Wuold you give me the output of the above code?
0
delousedAuthor Commented:
Nope, it just gives me something like this:  ; ; ;

I don't get it.  
0
MortenSlotKristensenCommented:
Just as a test.. What does the following give you?
CurrentlySelectedLabel.Text = "";
foreach(GridViewRow row in GridView1.Rows) {
   CurrentlySelectedLabel.Text += row.Cells[0].Text + "; " +
                                  row.Cells[1].Text + "; " +
                                  row.Cells[2].Text + "; " +
                                  row.Cells[3].Text + "<br/>";
}
 

Open in new window

0
delousedAuthor Commented:
I have a repeater that is bound to the gridview like this:
<SelectParameters>
                            <asp:ControlParameter ControlID="GridView" Name="ID" PropertyName="SelectedValue" />
                        </SelectParameters>

Would it be easier to retrieve the value from the repeater instead?
0
MortenSlotKristensenCommented:
I'm not actually sure but what did the above test code output?
0
delousedAuthor Commented:
It just output  ; ; ;
0
MortenSlotKristensenCommented:
So there is only a single row of data in it?
0
MortenSlotKristensenCommented:
Try this then:
protected void GridView1_SelectedIndexChanged(Object sender, EventArgs e) {
    GridViewRow row = GridView1.SelectedRow;
    CurrentlySelectedLabel.Text = "You selected: " +((Label)row.FindControl("Label1")).Text;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
delousedAuthor Commented:
That did it!  Thank you!
0
MortenSlotKristensenCommented:
I'm glad and thanks for the points!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.