Introduction

Step 1

Create table, using query, mentioned below.
  1. CREATE TABLE [tblgrid](
  2. [UserId] [int] IDENTITY(1,1) NOT NULL,
  3. [userName] [varchar](50) NULL,
  4. [City] [varchar](50) NULL,
  5. [Designation] [varchar](50) NULL,
  6. [sal] [bigint] NULL
  7. )
Step 2
Add below code to .aspx file.
  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3. <title></title>
  4. <script src="http://dotnettricks-abdul.in/Contents/jquery-1.4.1.js" type="text/javascript"></script>
  5. <script type="text/javascript">
  6. function visiblefalse() {
  7. $("#dv").attr("style", "display:none");
  8. $("#GridView1″).attr("
  9. style ", "
  10. backgroundColor: white ");
  11. }
  12. function GetDetails(id) {
  13. WebService.GetAllData(id, success, failure);
  14. }
  15. function success(result) {
  16. $(result).each(function(i) {
  17. var username = result[i].userName;
  18. var City = result[i].City;
  19. var des = result[i].des;
  20. var sal = result[i].sal;
  21. $("div[id$=dv]").attr("style", "block");
  22. $("#UName").html(username);
  23. $("#City").html(City);
  24. $("#Des").html(des);
  25. $("#Sal").html(sal)
  26. });
  27. }
  28. function failure() {
  29. alert("error");
  30. }
  31. </script>
  32. </head>
  33. <body>
  34. <form id="form1″ runat=" server ">
  35. <div>
  36. <asp:GridView ID="GridView1″ runat="server" AutoGenerateColumns="False" DataKeyNames="UserId,userName" EnableModelValidation="True" OnRowCommand="GridView1_RowCommand" HeaderStyle-BorderWidth="1″
  37. HeaderStyle-Font-Bold=" true " OnRowDataBound="GridView1_RowDataBound ">
  38. <Columns>
  39. <asp:TemplateField HeaderText="UserName ">
  40. <ItemTemplate>
  41. <asp:Label ID="lblitemUser " runat="server " Text=’<%# Eval("userName ") %>’></asp:Label>
  42. </ItemTemplate>
  43. </asp:TemplateField>
  44. <asp:TemplateField HeaderText="City ">
  45. <ItemTemplate>
  46. <asp:Label ID="lblitemCity " runat="server " Text=’<%# Eval("City ") %>’></asp:Label>
  47. </ItemTemplate>
  48. </asp:TemplateField>
  49. <asp:TemplateField HeaderText="Designation ">
  50. <ItemTemplate>
  51. <asp:Label ID="lblDesig " runat="server " Text=’<%# bind("Designation ")%>’></asp:Label>
  52. </ItemTemplate>
  53. </asp:TemplateField>
  54. <asp:TemplateField HeaderText="Salary ">
  55. <ItemTemplate>
  56. <asp:Label ID="lblsal " runat="server " Text=’<%# Eval("sal ") %>’></asp:Label>
  57. </ItemTemplate>
  58. </asp:TemplateField>
  59. </Columns>
  60. </asp:GridView>
  61. </div>
  62. <div>
  63. <asp:Label ID="lblresult " runat="server "></asp:Label>
  64. </div>
  65. <div id="dv " runat="server " style="display: none; ">
  66. <table>
  67. <tr>
  68. <td>
  69. User Name:
  70. </td>
  71. <td id="UName ">
  72. </td>
  73. </tr>
  74. <tr>
  75. <td>
  76. City:
  77. </td>
  78. <td id="City ">
  79. </td>
  80. </tr>
  81. <tr>
  82. <td>
  83. Designation:
  84. </td>
  85. <td id="Des ">
  86. </td>
  87. </tr>
  88. <tr>
  89. <td>
  90. Salary:
  91. </td>
  92. <td id="Sal ">
  93. </td>
  94. </tr>
  95. </table>
  96. <asp:ScriptManager ID="ScriptManager1″ runat="server">
  97. <Services>
  98. <asp:ServiceReference Path="~/WebService.asmx" />
  99. </Services>
  100. </asp:ScriptManager>
  101. </div>
  102. </form>
  103. </body>
  104. </html>
Step 3

Add below code to .cs page.
  1. SqlConnection con = new SqlConnection("Data Source=.\\SQLExpress;Initial Catalog=abc;Integrated Security=True");
  2. SqlDataAdapter da = new SqlDataAdapter();
  3. DataSet ds = new DataSet();
  4. protected void Page_Load(object sender, EventArgs e)
  5. {
  6. if (!IsPostBack)
  7. {
  8. bind();
  9. }
  10. }
  11. protected void bind()
  12. {
  13. SqlCommand cmd = new SqlCommand("select * from tblgrid", con);
  14. da.SelectCommand = cmd;
  15. da.Fill(ds, "tblgrid");
  16. if (ds.Tables[0].Rows.Count > 0)
  17. {
  18. GridView1.DataSource = ds.Tables[0];
  19. GridView1.DataBind();
  20. }
  21. }
  22. protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
  23. {
  24. if (e.Row.RowType == DataControlRowType.DataRow)
  25. {
  26. e.Row.Attributes.Add("onclick", "JavaScript: GetDetails('" + Convert.ToInt32(GridView1.DataKeys[e.Row.RowIndex].Value.ToString()) + "')");
  27. e.Row.Attributes.Add("onmouseover", "JavaScript:this.style.cursor=’pointer’; this.style.backgroundColor=’Red’");
  28. e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=’white’");
  29. }
  30. }
Step 4
Create webService project.
  1. [WebMethod]
  2. public GetRec[] GetAllData(int ID)
  3. {
  4. SqlCommand cmd = new SqlCommand("select userName,City,Designation,sal from tblgrid where UserId="+ID);
  5. DataTable dt = new DataTable();
  6. cmd.Connection = con;
  7. da.SelectCommand = cmd;
  8. //da.Fill(ds, "tblgrid");
  9. da.Fill(dt);
  10. List<GetRec> objList = new List<GetRec>();
  11. foreach(DataRow dr in dt.Select())
  12. {
  13. objList.Add(new GetRec
  14. {
  15. userName=dr["userName"].ToString(),
  16. City=dr["City"].ToString(),
  17. des = dr["Designation"].ToString(),
  18. sal = Convert.ToInt32(dr["sal"])
  19. }
  20. );
  21. }
  22. return objList.ToArray();
  23. }
  24. public struct GetRec {
  25. public string userName { get; set; }
  26. public string City { get; set; }
  27. public string des { get; set; }
  28. public int sal { get; set; }
  29. }
Step 5
We got the output, mentioned below.