0

I'm building a asp.net page in C#. I have a GridView that when I click on it I want the content in the cell. The problem is that nothing at all happens when I click on in. What is wrong?

In my aspx-file:

<asp:GridView ID="GridView1" runat="server" Height="224px" Width="589px" IsItemClickEnabled="True" SelectionChanged="GridView1_SelectedIndexChanged" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"></asp:GridView>

In my cs-file:

protected void Page_Load(object sender, EventArgs e)
{
    NpgsqlConnection conn = new NpgsqlConnection("Server=127.0.0.1;Port=5432;Database=ssys;User Id=postgres;Password=postgres;"); //Skapar connectionobjektet
    conn.Open();               
    NpgsqlCommand command = new NpgsqlCommand("SELECT * FROM times", conn);  
    NpgsqlDataReader dr = command.ExecuteReader();
    GridView1.DataSource = dr;
    GridView1.DataBind();
}


protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    string item = (string)(GridView1.Rows[0].Cells[0]).Text;
    MessageBox.Show(item);
}
user2939293
  • 793
  • 5
  • 16
  • 41
  • The SelectedIndexChanged event is raised when a row's Select button is clicked, but after the GridView control handles the select operation. This enables you to provide an event-handling method that performs a custom routine, such as updating a status label with the currently selected row, whenever this event occurs. Courtesy: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.onselectedindexchanged(v=vs.110).aspx – rach Apr 18 '14 at 13:21
  • As user1848739 says, make sure you have some form of button or select column enabled on the grid. If you have, post content of the page so we can see where the problem is. – Anthony Horne Apr 18 '14 at 13:23
  • I'm not sure what type MessageBox is in your code. But you're using it like it's a Windows Forms control. Are you trying to show a Windows Forms message box in an ASP.NET context? – mason Apr 18 '14 at 13:44

1 Answers1

2

Just clicking the row in the gridview does not trigger the SelectedIndexChanged event.

From MSDN:

Occurs when a row's Select button is clicked, but after the GridView control handles the select operation.

The MSDN page tells you have to use

autogenerateselectbutton="True"

To generate a select button. This will trigger the SelectedIndexChanged.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325