Click here to Skip to main content
15,899,475 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My gridviews's OnrowDataBounding, OnselectedIndexchanged events working ok. after selecting control is passing to another page as intended, but selectedindex datakey value is null even in calling page and in details.aspx page. I like to pass this selectedindex value to a string and thereby use in querystring in details page. please help me out with correct coding.

I have also tried as suggested at http://www.codeproject.com/Questions/103062/How-to-get-the-value-of-a-selected-row-in-gridview


My code is as follows
ASP.NET
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns = "False" AllowPaging="true"
    OnPageIndexChanging="gdv1_OnPaging" PageSize="5" DataKeyNames = "Material"
     OnRowDataBound = "gv1_OnRowDataBound" OnSelectedIndexChanged = "gv1_SelectedIndexChanged"
                    CSSClass= "~/Styles" EnableViewState="False" PagerSettings-Mode="Numeric" AutoGenerateSelectButton="true">

C#
 protected void gv1_OnRowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onclick"] =     Page.ClientScript.GetPostBackClientHyperlink(GridView1, "Select$" + e.Row.RowIndex);
            e.Row.ToolTip = "Click to select this row.";
        }
    }
protected void gv1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int selectedIndex = GridView1.SelectedIndex;
        string selectedMaterial = (GridView1.DataKeys[selectedIndex]).Value.ToString();
        Server.Transfer("Details.aspx");
    }
//code in details.aspx
 protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Page.PreviousPage != null)
        {
           GridView GridView1 =  GridView)this.Page.PreviousPage.FindControl("GridView1");
            int selectedIndex = GridView1.SelectedIndex;

            string selectedMaterial = (GridView1.DataKeys[selectedIndex]).ToString();
}


What I have tried:

C#
//int selectedIndex = Convert.ToInt32(GridView1.SelectedDataKey[1].ToString());
 int selectedIndex = GridView1.SelectedIndex;
 string customID = (GridView1.DataKeys[selectedIndex]["pkCityId"]).ToString();
Posted
Updated 25-Aug-16 16:14pm

1 solution

Hi abmkp,
You need to add the column pkCityId to the DataKeyNames property of the grid as per below;
ASP.NET
<asp:gridview id="GridView1" runat="server" autogeneratecolumns="False" allowpaging="true" OnPageIndexChanging="gdv1_OnPaging" PageSize="5" DataKeyNames = "Material,pkCityId" OnRowDataBound = "gv1_OnRowDataBound" OnSelectedIndexChanged = "gv1_SelectedIndexChanged" CSSClass= "~/Styles" EnableViewState="False" PagerSettings-Mode="Numeric" AutoGenerateSelectButton="true"</asp:gridview>


Kind Regards
 
Share this answer
 
v2
Comments
abmkp 28-Aug-16 10:16am    
Hi, my actual Detakeynames is "Material" only and i have added that one. "pkcityId" was referred as example only.
an0ther1 28-Aug-16 19:25pm    
To get a field value you need to use as below;
string customID = GridView1.DataKeys[selectedIndex]["mycolumnname"].ToString();
But the column name must be included in the DataKeyNames property of the grid definition.
Assume you have the fields MaterialId, Material, pkCityId and CityName as your Grid columns & you want to get the MaterialId & pkCityId values from your code behind you would add these to the DataKeyNames and access them using the code above.
Additionally, check the value of the GridView.SelectedIndex. Personally I use the following;
int intRow = this.GridView1.SelectedRow.RowIndex;

Kind Regards

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900