{"id":191,"date":"2010-05-18T19:21:06","date_gmt":"2010-05-18T13:51:06","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=191"},"modified":"2010-05-18T19:21:06","modified_gmt":"2010-05-18T13:51:06","slug":"datagrid-example-part-1","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/datagrid-example-part-1\/","title":{"rendered":"DataGrid Example &#8211; Part 1"},"content":{"rendered":"<p>For the basics of DataGrid please refer this post: <a href=\"https:\/\/jitendrazaa.com\/blog\/?p=188\">https:\/\/jitendrazaa.com\/blog\/?p=188<\/a><\/p>\n<p>In this example, i will explain the basics of DataGrid control like Theming, Databinding etc.<\/p>\n<p>We will start our example with creating the SQL Express database of Employee which will contain the following fields : Id, FName, LName, Email.<\/p>\n<p style=\"text-align: center;\"><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Employee.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-194\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Employee.jpg?resize=486%2C124&#038;ssl=1\" alt=\"\" width=\"486\" height=\"124\" \/><\/a><\/p>\n<p style=\"text-align: left;\">Now create a stored procedure to insert the data into Table.<\/p>\n<pre class=\"brush: sql; title: ; notranslate\" title=\"\">\nCREATE PROCEDURE dbo.AddEmployee\n@FName varchar(50),\n@LName varchar(50),\n@Email varchar(50)\nAS\nINSERT INTO Employee (FName,LName,Email) values (@FName,@LName,@Email)\n<\/pre>\n<p><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/SP.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-200\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/SP.jpg?resize=252%2C235&#038;ssl=1\" alt=\"\" width=\"252\" height=\"235\" \/><\/a><\/p>\n<p>In ASPX page, write the code to input the Employee information.<\/p>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\nFirst Name : &lt;asp:TextBox ID=&quot;txtFName&quot; runat=&quot;server&quot;\/&gt;&lt;br \/&gt;\nLast Name :&lt;asp:TextBox ID=&quot;txtLName&quot; runat=&quot;server&quot;\/&gt;&lt;br \/&gt;\nEmail :&lt;asp:TextBox ID=&quot;txtEmail&quot;; runat=&quot;server&quot;\/&gt;&lt;br \/&gt;\n&lt;asp:Button ID=&quot;btnSubmit&quot; Text=&quot;Add Record&quot; runat=&quot;server&quot; OnClick=&quot;btnSubmit_Click&quot;\/&gt;\n<\/pre>\n<p>Go to the property of <strong>Employee.mdf<\/strong> and select connection string as shown in below image.<\/p>\n<pre><a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/ConString.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-208\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/ConString.jpg?resize=396%2C125&#038;ssl=1\" alt=\"\" width=\"396\" height=\"125\" \/><\/a><\/pre>\n<p>On the click event of the button write below function:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nstring conString = @&quot;Data Source=.SQLEXPRESS;AttachDbFilename=&quot;&quot;|DataDirectory|Employee.mdf&quot;&quot;;Integrated Security=True;User Instance=True&quot;;\nprotected void btnSubmit_Click(object sender, EventArgs e)\n{\nSqlConnection con = null;\nSqlCommand cmd = null;\ntry\u00a0{\ncon = new SqlConnection(conString);\n     con.Open();\n     cmd = new SqlCommand(&quot;AddEmployee&quot;, con);\n     cmd.CommandType = CommandType.StoredProcedure;\n     cmd.Parameters.AddWithValue(&quot;@FName&quot;, txtFName.Text);\n     cmd.Parameters.AddWithValue(&quot;@LName&quot;, txtLName.Text);\n     cmd.Parameters.AddWithValue(&quot;@Email&quot;, txtEmail.Text);\n     cmd.ExecuteNonQuery();\n     showConfirmationMessage();\n}\ncatch (CustomException ex)\u00a0{\nex.logException();\n}finally{\n    con.Close();\n}\n}\n private void showConfirmationMessage()\n{\n      ClientScriptManager csMngr = Page.ClientScript;\n      csMngr.RegisterStartupScript(Page.GetType(), &quot;Success&quot;, &quot;alert('Record Added succesfully');&quot;, true);\n}\n<\/pre>\n<p><strong> ASPX Code for data grid<\/strong><\/p>\n<div>\n<pre class=\"brush: xml; title: ; notranslate\" title=\"\">\n&lt;asp:DataGrid ID=&quot;grdEmp&quot; runat=&quot;server&quot;&gt;\n&lt;HeaderStyle CssClass=&quot;Header&quot; \/&gt;\n&lt;AlternatingItemStyle CssClass=&quot;AlternateItemStyle&quot; \/&gt;\n&lt;\/asp:DataGrid&gt;\n<\/pre>\n<\/div>\n<p>Write below code to bind the data into DataGrid.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nprivate void bindData()\n{\nSqlConnection con = null;\nSqlDataAdapter adp = null;\ntry\n{\ncon = new SqlConnection(conString);\ncon.Open();\nadp = new SqlDataAdapter(&quot;Select * from Employee&quot;,con);\nDataSet ds = new DataSet();\nadp.Fill(ds);\ngrdEmp.DataSource = ds;\ngrdEmp.DataBind();\n}\ncatch (CustomException ex)\n{\nex.logException();\n}finally{\ncon.Close();\n}\n}\n<\/pre>\n<p><strong>Final output:<\/strong><br \/>\n<a href=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/output2.jpg?ssl=1\"><img data-recalc-dims=\"1\" loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-223\" src=\"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/output2.jpg?resize=280%2C265&#038;ssl=1\" alt=\"\" width=\"280\" height=\"265\" \/><\/a><\/p>\n<h1 style=\"text-align: center;\"><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/output2.jpg\"><\/a><span style=\"color: #0000ee;\"><span style=\"text-decoration: underline;\"><a href=\"https:\/\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/DataGridDemo.zip\">Download Code<\/a><br \/>\n<\/span><\/span><\/h1>\n","protected":false},"excerpt":{"rendered":"<p>For the basics of DataGrid please refer this post: https:\/\/jitendrazaa.com\/blog\/?p=188 In this example, i will explain the basics of DataGrid control like Theming, Databinding etc. We will start our example with creating the SQL Express database of Employee which will contain the following fields : Id, FName, LName, Email. Now create a stored procedure to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_seo_schema_type":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_hide_featured_image":false,"_jetpack_featured_image_duplicate_dismissed":0,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[21],"tags":[43,71],"class_list":["post-191","post","type-post","status-publish","format-standard","hentry","category-net","tag-asp-net","tag-datagrid"],"jetpack_sharing_enabled":true,"jetpack_likes_enabled":true,"jetpack-related-posts":[{"id":1376,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/use-of-openxml-in-sql-server\/","url_meta":{"origin":191,"position":0},"title":"Use of OPENXML in SQL Server","author":"Jitendra","date":"December 4, 2010","format":false,"excerpt":"In SQL Server, OpenXML is very powerful method for the XML data manipulation. This article shows that how to use the OpenXML for XML string.","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"OpenXML in SQL Server","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/12\/OpenXML-in-SQL-Server.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2861,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/pagination-and-switch-case-in-where-clause-sql-server\/","url_meta":{"origin":191,"position":1},"title":"Pagination and Switch Case in Where clause &#8211; SQL Server","author":"Jitendra","date":"May 19, 2012","format":false,"excerpt":"Example of using pagination and switch case in where clause in SQL server","rel":"","context":"In &quot;SQL&quot;","block_context":{"text":"SQL","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/"},"img":{"alt_text":"SQL Server Employee Table","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2012\/05\/SQL-Server-Employee-Table.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":936,"url":"https:\/\/www.jitendrazaa.com\/blog\/java\/jdbc-example-with-microsoft-access-in-swing\/","url_meta":{"origin":191,"position":2},"title":"JDBC Example with Microsoft Access in Swing, Next and Previous navigation","author":"Jitendra","date":"August 31, 2010","format":false,"excerpt":"JDBC - Java Database Connectivity example in Swing","rel":"","context":"In &quot;JAVA&quot;","block_context":{"text":"JAVA","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/java\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":2281,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/web\/disable-inputs-after-submit-to-avoid-double-submission-using-jquery-and-ajax\/","url_meta":{"origin":191,"position":3},"title":"Disable inputs after submit to avoid double submission using JQuery and Ajax","author":"Jitendra","date":"June 24, 2011","format":false,"excerpt":"This article will explain the safe way to submit the forms using JQuery. It will avoid accidental double submission.","rel":"","context":"In &quot;HTML&quot;","block_context":{"text":"HTML","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/webtech\/web\/"},"img":{"alt_text":"Disable inputs after submit to avoid double submission using JQuery","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/06\/Disable-inputs-after-submit-to-avoid-double-submission-using-JQuery.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":5882,"url":"https:\/\/www.jitendrazaa.com\/blog\/sql\/sqlserver\/sql-server-read-all-files-in-directory-and-store-in-table\/","url_meta":{"origin":191,"position":4},"title":"SQL Server &#8211; Read all files in directory and store in Table","author":"Jitendra","date":"January 18, 2017","format":false,"excerpt":"T-SQL Script to read all files in directory and store back in table - SQL Server","rel":"","context":"In &quot;SQL Server&quot;","block_context":{"text":"SQL Server","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/sql\/sqlserver\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":227,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/datagrid-example-sorting-part-2\/","url_meta":{"origin":191,"position":5},"title":"DataGrid Sorting and Paging Example \u2013 Part 2","author":"Jitendra","date":"May 18, 2010","format":false,"excerpt":"For Part 1, visit This URL\u00a0https:\/\/jitendrazaa.com\/blog\/?p=191 In this part, i will show that how to Sort the Datagrid Component of ASP.NET. To sort Datagrid, we will need DataView and then assign Dataview as datasource to our Grid control. We will need to change our code of Part 1. Previously, we\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Sorting1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]}],"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/191","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=191"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/191\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=191"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=191"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=191"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}