GridView Row Single click Event for fetching its Data

GridView Row Single click Event for fetching its Data


 Step 1: Design web form .aspx Page

  
  <asp:GridView ID="gr" runat="server" DataKeyNames="Name" AutoGenerateColumns="false"
            AlternatingRowStyle-BackColor="WhiteSmoke" Width="40%" OnRowDataBound="gr_RowDataBound"
            OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
            <HeaderStyle BackColor="#3366ff" Font-Bold="true" ForeColor="White" />
            <Columns>
                <asp:BoundField DataField="Name" HeaderText="Name" />
                <asp:BoundField DataField="City" HeaderText="City" />
                <asp:BoundField DataField="Phone" HeaderText="Phone" />
                <asp:CommandField SelectText="Select" ShowSelectButton="true" Visible="false" />
            </Columns>
        </asp:GridView>


Step 2: Logic on .Cs page

  
protected void Page_Load(object sender, EventArgs e)
        {
            bind_gr();

        }

  public void bind_gr()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("Name");
            dt.Columns.Add("City");
            dt.Columns.Add("Phone");
            dt.Columns.Add("percentage");


            dt.Rows.Add("Kandy", "Delhi", "8802***2564", "54");
            dt.Rows.Add("Sam", "Hr", "545502*2544", "32");
            dt.Rows.Add("Izhar", "Up", "95802*2452", "90");
            dt.Rows.Add("Vineet", "Delhi", "88022564***", "31");

            gr.DataSource = dt;
            gr.DataBind();
        }
     protected void gr_RowDataBound(object sender, GridViewRowEventArgs e)
        {


           
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';this.style.textDecoration='underline';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";

            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gr, "Select$" + e.Row.RowIndex);
        }

        }
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string Name = gr.SelectedDataKey.Value.ToString();
            string City = gr.SelectedRow.Cells[1].Text;

            Response.Write(Name + ',' + City);
        }

 It Will look Like :-




Comments

Popular posts from this blog

Create and save QR code in asp.net with C#

Change text of RadGrid Header Dynamically

Telerik Radwindow Open on Button Click