Click here to Skip to main content
15,877,103 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I have 1 grid in ASP.NET and I am trying to fetch the data when I click any row to update or delete, but the event is not getting called when I click on grid row.

What I have tried:

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
           if (GridView1.SelectedRow != null)
               // Retrieve the data from the selected row
               int id =
               string selectedData = GridView1.SelectedRow.Cells[1].Text;

               // Store the id in the hidden field
               SelectedIdHiddenField.Value = id.ToString();

               // Display the selected data in the TextBox
               TextBox1.Text = selectedData;

This is my front end code:
 <asp:GridView ID="GridView1" 
runat="server" AutoGenerateColumns="false" 
CssClass="custom-grid fixed-header" 
OnSelectedIndexChanged="GridView1_SelectedIndexChanged" AutoPostBack="true">
                     <asp:BoundField DataField="Id" 
                     HeaderText="Id"  SortExpression="Id" />
                    <%--<asp:BoundField DataField="Sr_No" 
                    HeaderText="Sr_No" ItemStyle-Width="100px" ItemStyle- 
                    HeaderStyle-HorizontalAlign="Center" />--%>
                    <asp:BoundField DataField="hobbies" 
                    HeaderText="Hobbies"  ItemStyle-Width="1100px" 
                    SortExpression="hobbies" />
Updated 25-Oct-23 10:41am

Your code seems good but you can verify the following points:
1. If your GridView is placed inside an UpdatePanel, ensure that the GridView is configured as a PostBack trigger for that UpdatePanel. This is important for Ajax-enabled pages.

2. If there are any validation controls on your page, make sure that they are not blocking the postback. Validation errors can prevent postback events from firing.

3. Sometimes, issues like this can be browser-specific. Try testing your page in different web browsers to see if the issue is browser-related.

If you've gone through these steps and the event handler is still not being called, please provide more details about your code and environment, and I can offer further assistance based on that information.
Share this answer
You haven't provided any way for the user to select a row. Therefore, the "selected row changed" event is never going to fire!

Either set the AutoGenerateSelectButton[^] property to true, or add a CommandField[^] to the grid with the ShowSelectButton property set to true.
Share this answer
ashwini gaikwad 2021 16-Oct-23 9:29am    
if i add AutoGeneratedSelectButton=true it shows 1 button column in grid but i do not want like that i want like when i click on row that data should show in textbox and i can edit and update it please help
Richard Deeming 16-Oct-23 9:31am    
Did you even try searching for an answer?

Less than 30 seconds in Google produces: Select GridView Row By Clicking Anywhere In Row[^]
ashwini gaikwad 2021 16-Oct-23 12:51pm    
yes i have tried, i am beginner in web application that is why thought to take help from experienced developer

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