473,396 Members | 2,011 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,396 developers and data experts.

How to bind a GridView in ASP.net

This code will help for the beginners, who want to learn, how to bind a gridview.

Before showing the code I will give a small introduction how to work with gridview.
  1. Add a gridview controls to the ASPX Page.
  2. Create a method to get a data from the database and bind to the gridview
  3. Create a dataset object.
  4. create a sqlcommand object.
  5. create a sqlconnection object


Description:
Below is a sample code.

In ASPX Page
Expand|Select|Wrap|Line Numbers
  1.      <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true">
  2.  
  3.         </asp:GridView>
  4.  
IN ASPX.CS File
Expand|Select|Wrap|Line Numbers
  1.     protected void Page_Load(object sender, EventArgs e)
  2.         {
  3.  
  4.              GetData();
  5.  
  6.         }
  7.     void GetData()
  8.         {
  9.             DataSet ds = new DataSet();
  10.             SqlCommand cmd = new SqlCommand();
  11.             SqlDataAdapter da = new SqlDataAdapter();
  12.             SqlConnection con = new SqlConnection("Data Source=msdb;Initial Catalog=TEST;Persist Security Info=True;User ID=sa;Password=sa");
  13.             if (con.State == ConnectionState.Open)
  14.                 con.Close();
  15.             else
  16.                 con.Open();
  17.             cmd.CommandText = "Select * from Platform";
  18.             cmd.Connection = con;
  19.             da.SelectCommand = cmd;
  20.             da.Fill(ds);
  21.             cmd.ExecuteNonQuery();
  22.             GridView1.DataSource = ds;
  23.             GridView1.DataBind();
  24.         }
  25.  
Summary
There are many topics to learn with ASP.Net. I have covered a small portion hope this will helps all the beginners to startup. Please give your feedback and suggestion.
Mar 23 '11 #1
0 3416

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: sck10 | last post by:
Hello, I have a GridView that is using the following to connect to a SQL Server 2000 stored procedure: <asp:SqlDataSource ID="dsWebDocList" SelectCommand="sp_web_WebDocument"...
5
by: Steven Baggs | last post by:
Hi!, I have defined a List<T> like this List<Container> Container class holds my Item object and it's state as a Enum. Container.Item Container.State I would like to bind it to GridView.
1
by: Smash | last post by:
I have problem binding gridview.... At first i didn't want to bind gridview at first page load...so in my objectdatasource_selecting event i wrote this: If Not Page.IsPostBack Then e.Cancel =...
0
by: DC | last post by:
The problem I'm using the .NET GridView and FormView objects for the first time and im getting the error "An OleDbParameter with ParameterName '@ID' is not contained by this...
13
by: AG | last post by:
I have a gridview that I bind to a List(of Type) at runtime. Not using a datasource control. The gridview has a template column with an imagebutton whose commandname is set to 'Delete'. The...
3
by: =?Utf-8?B?YmJkb2J1ZGR5?= | last post by:
I have a question that maybe somebody can help me out. I have a gridview that is bound to a sqltable, and I have created two template columns. I am having problems getting the sorting to work....
0
by: singh79 | last post by:
Hi.. In My DropDown some Procedure Bind,My Drop Down name is Ddlpro ,(Bind By Table "PatProcedure") ,same Table PatProcedure again use and bind GridView ,In Gridviw I Can Take Template...
1
by: svchosthunter | last post by:
I want to bind a gridview with data table but I need one field as combo box as DataGridviewComboBoxColumn I want that when I bind my grid there should be a combo box in grid which will be binded to...
4
by: Craig Buchanan | last post by:
I dynamically add data-bound templates to a gridview in my ascx control. while this works correctly when the gridview is databound to the datatable, i'm having issues on postback. i would like...
2
by: satyabhaskar | last post by:
hi all, how to bind gridview in parent window to the child window using client side java script....actually in my main page i have a grid view consisting a report... my problem is to display that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.