Hitesh Kasar
Hitesh Kasar

Reputation: 123

How to find datakey value of gridview on selected index changed property?

My gridview is like this but I am getting error when I select view button to find primary key value column on selected index changed. Please help me to solve the issue.

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <Columns >
                <asp:TemplateField >
                    <ItemTemplate >
                        <asp:Button ID="btnViewComments" Text ="View Comments" runat ="server" CommandName ="select" />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField ="forumId" Visible ="false" />
                <%--<asp:CommandField ButtonType ="Button" ShowSelectButton ="true" SelectText ="View Comments"/>--%>
                <asp:TemplateField HeaderText ="Question">
                    <ItemTemplate >
                        <asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" Height="100" Width ="350"></asp:TextBox>
                       <%-- <%#Eval("question")%>--%>
                    </ItemTemplate>
                    <%--<EditItemTemplate >
                        <asp:TextBox ID ="txtQuestion" Text ='<%#Eval("question")%>' runat ="server" TextMode ="MultiLine" ></asp:TextBox>
                    </EditItemTemplate>--%>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Poster Name">
                    <ItemTemplate >
                        <%#Eval("posterName") %>
                    </ItemTemplate>
                    <EditItemTemplate >
                        <asp:Label ID ="lblPosterName" Text ='<%#Eval("posterName") %>' runat ="server" ></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="Date">
                    <ItemTemplate >
                        <%#Eval("dateTim") %>
                    </ItemTemplate>
                    <EditItemTemplate >
                        <asp:Label ID ="lblDateTime" Text ='<%#Eval("dateTim") %>' runat ="server" ></asp:Label>
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:GridView>

my code is.....

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        try
        {
            Int64 forumId = (Int64)GridView1.SelectedValue;
            Session["forumId"] = forumId;
            Response.Redirect("Thread.aspx");
        }
        catch (Exception)
        {

            throw;
        }
    }

Upvotes: 11

Views: 61355

Answers (6)

Hema Shetty
Hema Shetty

Reputation: 109

If you are not using gridview select event in your page.cs code then you just remove OnSelectedIndexChanged="GridView1_SelectedIndexChanged" from the aspx code of page of gridview.

Upvotes: 0

AlphaRomeo69
AlphaRomeo69

Reputation: 59

You would need to specify the unique column name in the gridview to be set under the Datakey tab.

From there, you would need to invoke the _selectedIndexChanged method on the behind page code.

Upvotes: 0

Raghubar
Raghubar

Reputation: 2788

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        Int64 forumId = Convert.ToInt64(GridView1.SelectedRow.Cells[1].Text);
        Session["forumId"] = forumId;
        Response.Redirect("Thread.aspx");
    }
    catch (Exception)
    {

        throw;
    }
}

Upvotes: 3

Damith
Damith

Reputation: 63065

you can set DataKeyNames as forumId like below

<asp:GridView ID="GridView1" runat="server" DataKeyNames = "forumId" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">

Since you haven't give any data key names in current solution GridView1.SelectedValue will not contain the value you expected

Upvotes: 0

Soner G&#246;n&#252;l
Soner G&#246;n&#252;l

Reputation: 98740

Looks like you just need to set DataKeyNames property to forumId like;

<asp:GridView DataKeyNames = "forumId" ...

Upvotes: 0

Maruti
Maruti

Reputation: 400

First you have to define field name in grid view declaration that which field you want to make datakey. for example if you want "forumId" datakey.than

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
DataKeyNames="forumId">

and than you can access in this way

int intforumid = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Values[0]);

Upvotes: 10

Related Questions