Click here to Skip to main content
15,889,595 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am looking for some examples for GridView for common tasks like displaying, editing, deleting, paging, sorting, batch updates etc, but WITHOUT using any datasource controls. I would like to bind the data in the code to custom object collection. all the samples I found on the web so far use some data source control, I think Enterprise applications shouldn't be using this pattern.If you have any examples of gridview that performs the common tasks without using any data source control, can you please share them? that would be very helpful. thanks.
Posted

 
Share this answer
 
Comments
Jagan911 28-Feb-12 3:49am    
hi Anuja thanks 4 ur reply.but i need examples of Edting,Selection,Deleting tasks in GridView.So,please suggest me according to that
Follow these steps
1.open asp.net
2.FILE-->new WEBSITE(default.aspx page created)
3.TOOLBAR-->DATA-->GRID VIEW
or
write this code in default.aspx page

<asp:gridview id="GridView1" runat="server" xmlns:asp="#unknown">
onselectedindexchanged="GridView1_SelectedIndexChanged">

4.Now a grid box will appear, to insert your data
5.In SSMS or SQL (server management studio)
5.1 In OBJECT EXPLORER-->DATABASES-->NEW DATABASE-->DATABASE NAME(abc)
5.2 Now we have created a database
5.3 select-->NEW QUERY option
5.4 Write these queries

create table months(m_id int,m_name varchar(20),q_numb int,semester int)
insert into months values(1,'jan',1,1)
insert into months values(2,'feb',1,1)
insert into months values(3,'mar',1,1)
insert into months values(4,'apr',2,1)
insert into months values(5,'may',2,1)
insert into months values(6,'jun',2,1)
insert into months values(7,'jul',3,2)
insert into months values(8,'aug',3,2)
insert into months values(9,'sep',3,2)
insert into months values(10,'oct',4,2)
insert into months values(11,'nov',4,2)
insert into months values(12,'dec',4,2)
select * from months


6.Now go back to default.aspx page
go to design mode(at bottom of page)
7.double click on the grid that appears.
8.It will be redirected to default aspx.cs page
9.In that page their will be a function

protected void Page_Load(object sender, EventArgs e)
{
}

10.In that function write the following code


SqlConnection con = new SqlConnection(@"Data Source=<from system="">;Initial Catalog=arc;Integrated Security=True"); //<from system="">-->SERVER EXPLORER-->DATA CONNECTIONS-->ADD CONNECTION
//-->SERVER NAME(select)-->OK
// Next right click on the connection-->properties(copy the connection
// string to DATA SOURCE

con.Open();
//SqlDataAdapter Comm = new SqlDataAdapter("SELECT m_id,m_name FROM months", con);
SqlDataAdapter Comm1 = new SqlDataAdapter("INSERT INTO months(m_id,m_name) VALUES(1,'ppt')", con);
SqlDataAdapter Comm = new SqlDataAdapter("SELECT m_id,m_name FROM months", con);
DataSet ds = new DataSet();
Comm.Fill(ds);
DataTable dt = new DataTable();
dt = ds.Tables[0];
GridView1.DataSource = dt;
GridView1.DataBind();


con.Close();



This is a simple example just for beginners
Thank you
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900