• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1335
  • Last Modified:

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

0
deloused
Asked:
deloused
  • 11
  • 7
1 Solution
 
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
delousedAuthor Commented:
That did it!  Thank you!
0
 
MortenSlotKristensenCommented:
I'm glad and thanks for the points!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 11
  • 7
Tackle projects and never again get stuck behind a technical roadblock.
Join Now