Printing out to a label using OnSelectChange in gridview

228 views Asked by At

I have a On Select change on my data grid using asp.net and C# in VS2012, but for some reason the selected row is not printing out in my label, I am no expert in C# although I am learning could somebody please tell me if I have made a mistake.

Thanks

C# Code

public void Latest_DVD()
{
    {
        using (OleDbDataAdapter dataquer = new OleDbDataAdapter("SELECT Title,Category,Director,Stock,Year FROM DVD ", conn))
        {
            dataquer.Fill(dt);
        }
    }
    DG_Latest.ShowHeader = true;
    DG_Latest.DataSource = dt;
    DG_Latest.DataBind();
    conn.Close();
    conn.Dispose();
}

void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
{
    GridViewRow row = DG_Latest.SelectedRow;

    MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}

asp.net

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3" GridLines="Vertical">
            <AlternatingRowStyle BackColor="#DCDCDC" />
            <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
            <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
            <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
            <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
            <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
            <SortedAscendingCellStyle BackColor="#F1F1F1" />
            <SortedAscendingHeaderStyle BackColor="#0000A9" />
            <SortedDescendingCellStyle BackColor="#CAC9C9" />
            <SortedDescendingHeaderStyle BackColor="#000065" />             
       </asp:GridView>
2

There are 2 answers

4
Sudhakar Tillapudi On BEST ANSWER

You need to wire up SelectedIndexChangedEvent properly

Try ThiS:

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True"   
BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" 
CellPadding="3" GridLines="Vertical" 
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
        <AlternatingRowStyle BackColor="#DCDCDC" />
        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />             
   </asp:GridView>  

Code Behind:

protected void Latest_DVD_SelectedIndexChanged(Object sender, EventArgs e)
{
GridViewRow row = DG_Latest.SelectedRow;
MessageLabel.Text = "You selected " + row.Cells[2].Text + ".";
}
2
afzalulh On

In the markup you need to add OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged" like below:

<asp:GridView ID="DG_Latest" runat="server" AutoGenerateSelectButton="True" 
BackColor="White" BorderColor="#999999" BorderStyle="None" 
BorderWidth="1px" CellPadding="3" GridLines="Vertical" 
OnSelectedIndexChanged="Latest_DVD_SelectedIndexChanged">
    <AlternatingRowStyle BackColor="#DCDCDC" />
    <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
    <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
    <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
    <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
    <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
    <SortedAscendingCellStyle BackColor="#F1F1F1" />
    <SortedAscendingHeaderStyle BackColor="#0000A9" />
    <SortedDescendingCellStyle BackColor="#CAC9C9" />
    <SortedDescendingHeaderStyle BackColor="#000065" />             
</asp:GridView>