Click here to Skip to main content
15,894,646 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear,


Please Help me,

In asp.net Gridview I select the grid row in anywhere the particular row get to be select. I need the C# Code


Regards
M.Mani
Posted
Comments
Do you need to highlight the row ?

1 solution

Mark Up:

XML
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Selecting GridView Row</title>
    <style type="text/css">
        body, html
        {
            font-family: Tahoma;
            font-size: small;
        }
        .Normal
        {
            background-color: #EFF3FB;
            cursor: hand;
        }
        .Normal:Hover, .Alternate:Hover
        {
            background-color: #D1DDF1;
            cursor: hand;
        }
        .Alternate
        {
            background-color: White;
            cursor: hand;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <asp:GridView runat="server" ID="GridView1" DataKeyNames="ID" AutoGenerateColumns="False"
        CellPadding="4" Font-Names="Tahoma" Font-Size="Small" ForeColor="#333333" GridLines="None"
        OnSelectedIndexChanged="GridView1_SelectedIndexChanged" OnRowDataBound="GridView1_RowDataBound">
        <Columns>
            <asp:TemplateField HeaderText="Row">
                <ItemTemplate>
                    <%# Container.DataItemIndex+1 %>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:BoundField DataField="FirstName" HeaderText="First Name" />
            <asp:BoundField DataField="LastName" HeaderText="Last Name" />
            <asp:CommandField ShowSelectButton="true" ButtonType="Link" Visible="false" SelectText="Enroll" />
        </Columns>
        <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
        <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
    </asp:GridView>
    <br />
    <asp:Label Text="" ID="lblSelectedRow" runat="server" />
    </form>
</body>
</html>


Code Behind:

C#
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        GridView1.DataSource = Enumerable.Range(1, 10).Select(a => new
        {
            ID = a,
            FirstName = String.Format("First Name {0}", a),
            LastName = String.Format("Last Name {0}", a)
        });
        GridView1.DataBind();
    }
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    lblSelectedRow.Text = String.Format("You selected row {0} with {1} {2}",
                                            GridView1.SelectedIndex + 1,
                                            GridView1.SelectedRow.Cells[0].Text,
                                            GridView1.SelectedRow.Cells[1].Text);
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        //add css to GridViewrow based on rowState
        e.Row.CssClass = e.Row.RowState.ToString();
        //Add onclick attribute to select row.
        e.Row.Attributes.Add("onclick", String.Format("javascript:__doPostBack('GridView1','Select${0}')", e.Row.RowIndex));
    }
}




 
Share this answer
 

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