Here I will explain how to bind generic list to gridview and how to bind database values to generic list using asp.net.
Description:
In previous article I explained clearly about what is WCF and how we can use WCF in our applications. Now I will explain how to use Lists in our application to bind data to gridview and I will explain how to bind database values to generic list using asp.net.
First Create new website and Design your aspx page like this
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Bind Generic List to Gridview</title> </head> <body> <form id="form1" runat="server"> <div> <asp:GridView runat="server" ID="gvDetails" AutoGenerateColumns="false"> <RowStyle BackColor="#EFF3FB" /> <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> <AlternatingRowStyle BackColor="White" /> <Columns> <asp:BoundField HeaderText="UserName" DataField="UserName" /> <asp:BoundField HeaderText="FirstName" DataField="FirstName" /> <asp:BoundField HeaderText="LastName" DataField="LastName" /> <asp:BoundField HeaderText="Location" DataField="Location" /> </Columns> </asp:GridView> </div> </form> </body> </html> |
After that add one class file to your website for that Right click on your website and select Add New Item à one window will open in that select Class file and give name as UserDetails.cs because here I am using this name in my sample if you want to give another name change UserDetails reference name in your sample.
Now open your Class file UserDetails.cs and write the following code
After that write the following code in code behind
public class UserDetails { string username = string.Empty; string firstname = string.Empty; string lastname = string.Empty; string location = string.Empty; public string UserName { get { return username; } set { username = value; } } public string FirstName { get { return firstname; } set { firstname = value; } } public string LastName { get { return lastname; } set { lastname = value; } } public string Location { get { return location; } set { location = value; } } } |
After completion writing code in your class file open your Default.aspx.cs page add the following namespaces
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; |
After that write the following code in code behind
List<UserDetails> objUserDetails = new List<UserDetails>(); protected void Page_Load(object sender, EventArgs e) { if(!IsPostBack) { BindUserDetails(); } } protected void BindUserDetails() { objUserDetails = GetUserDetails(); gvDetails.DataSource = objUserDetails; gvDetails.DataBind(); } protected List<UserDetails> GetUserDetails() { string strConnection = "Data Source=SureshDasari;Integrated Security=true;Initial Catalog=MySampleDB"; DataTable dt = new DataTable(); SqlConnection con = new SqlConnection(strConnection); con.Open(); SqlCommand cmd = new SqlCommand("select * from UserInformation", con); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); if(dt.Rows.Count>0) { for(int i=0;i<dt.Rows.Count;i++) { UserDetails userinfo = new UserDetails(); userinfo.UserName = dt.Rows[i]["UserName"].ToString(); userinfo.FirstName = dt.Rows[i]["FirstName"].ToString(); userinfo.LastName = dt.Rows[i]["LastName"].ToString(); userinfo.Location = dt.Rows[i]["Location"].ToString(); objUserDetails.Add(userinfo); } } return objUserDetails; } |
Demo
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
13 comments :
nice Article. Every one can understand easily
Does this example work with importing data from multiple tables and displaying in one grid?
I have use above example with multiple tables in select query but it's giving error.
A field or property with the name 'XX' was not found on the selected data source."
Ok all good it does work with multiple tables.. awesome example....
but this is useless ,can you tell me the live example where we need the genric,because this can we done by direct bindin colmn fiels to grid column
Now how to delete a item from generic list and again bind to grid
telugu valla satta chupistunnav..googlelo asp articles anni neeve chustunnaru...keep rocking suresh....all the good luck....
Sir Can You Please Help Me Out in Binding the Data from datatable in mysql to ListView in C# Windows Application...
Congratulations for your blog! It is really useful.
Can you explain how to fill only first column and first row with data from sql server?
Sorry for my english. Thank you!
how to store one by one row dinamically in two dimensional array and display them in asp.net using c#.............
Please Me............................
Hi
subject :
Bind grid when table record updated @ our side or any other user updating it
Read the senario carefully.
I have the grid and bind it using LINQ. Now when i update record in grid that time i rebind and get updated record. but problem in this case when any other user change the record and i no refresh the grid and try to update it that time i seen old record in grid. I want solution when table data is change either my side or any othe user side that time 1 trigger is fire and bind the grid automatic
Is it possible? I know the how to bind grid do not post such a solution.
HI,
Nice Post,Any doubt i m having any doubt in asp.net with c# ,means most of the time i referred your blogs only.its easy to understand..
Sir, This Abhinav Singh993 ; Please tell me is it possible to perform CRUD Operations using Generic List in asp.net using c# or it is only for the Displaying data, if possible please provide a tutorial over it. thank you.
how to insert value into the database using stored procedure and List<>........
Please help me..............
Note: Only a member of this blog may post a comment.