5

I know this question had been asked a hundred times, but I have difficulties implementing different solutions.I need to retrieve a selected row from a grid view in asp.net web application C#.I've done the databinding.I don't want to use edit/update buttons or checkboxe/radio button, just to select it by clicking on the row.Please help, I'm a little stuck, and I would like not to implement javascript based solutions.Thanks.

if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("OnMouseOver", "this.style.cursor='pointer';this.style.textDecoration='underline';");
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.ToolTip = "Click on select row";
            e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(this.SingleSelectGrid, "Select$" + e.Row.RowIndex);


            LinkButton selectbutton = new LinkButton()
            {
                CommandName = "Select",
                Text = e.Row.Cells[0].Text
            };
            e.Row.Cells[0].Controls.Add(selectbutton);
            e.Row.Attributes["OnClick"] = Page.ClientScript.GetPostBackClientHyperlink(selectbutton, "");


        }
1
  • Please don't use tags in the title. Commented Jun 22, 2012 at 22:26

2 Answers 2

8

if i get it right, this should do what you want:

.aspx:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"    
  DataKeyNames="id" onselectedindexchanged="GridView1_SelectedIndexChanged">

code behind:

 protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    int index = Convert.ToInt16(GridView1.SelectedDataKey.Value);

}

this should do what you want..index will give you the selected row ID, provided by the DataKeyNames attribute in your .aspx page. This however does require the "Enable Selection" to be checked. (Go to your .aspx page, designer, click your gridview, you should see the "Enable selection" attribute).

Sign up to request clarification or add additional context in comments.

13 Comments

I'm not finding the Enable Selection attribute, you can tell me the .aspx code to enable it...I'm using visual studio 2010
the bad news is that i'm binding my data manually from an arraylist...in my code...I think that's why I don't see the 3 enables..so I see it unbound untill my application starts...
ah....and I mentioned that I wanted not to use buttons...such as the linkbutton that appears when enable selection is activated
first I had some error related to Invalid postback or callback argument. Event validation is enabled using in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
and, another issue, is that, I don't know exaclty how to use that to retrieve the selected row...can u help me?:)) giving me an example ?
|
3

If you are adding a DataSource from code behind file then you have to set property called "AutoGenerateSelectButton " to True . This will enable you to select a row.

Comments

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.