不使用DataSource控件的ASP.NET GridView示例 [英] ASP.NET GridView Examples without Using DataSource Controls

查看:73
本文介绍了不使用DataSource控件的ASP.NET GridView示例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 GridView 的一些示例,用于显示,编辑,删除,分页,排序,批量更新等常见任务,但 没有 使用任何数据源控件 。我想将代码中的数据绑定到自定义对象集合。到目前为止我在网上找到的所有样本都使用了一些数据源控件,我认为企业应用程序不应该使用这种模式。如果你有任何gridview的例子执行常见任务而不使用任何数据源控件,你能不能请分享他们?这将非常有帮助。谢谢。

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.

推荐答案

参考这个

http://www.vkinfotek.com/gridview/gridview-generic-list-collection.html [ ^ ]


按照以下步骤

1.打开asp.net

2.FILE - >新网站(default.aspx页面创建)

3.TOOLBAR - > DATA - >网格视图



在default.aspx页面中编写此代码



< asp:gridview id = GridView1runat =serverxmlns:asp =#unknown>

onselectedindexchanged =GridView1_SelectedIndexChanged>



4 。现在会出现一个网格框,用于插入数据

5.在SSMS或SQL(服务器管理工​​作室)

5.1在OBJECT EXPLORER中 - > DATABASES-- > ;新数据库 - >数据库名称(abc)

5.2现在我们已经创建了一个数据库

5.3 select - > NEW QUERY选项

5.4写这些查询



创建表月(m_id int,m_name varchar(20),q_numb int,semester int)

insert到月数值(1,'jan',1,1)

插入数月值(2,'feb',1,1)

插入月数值( 3,'mar',1,1)

插入数月值(4,'apr',2,1)

插入数月值(5,'may ',2,1)

插入数月值(6,'jun',2,1)

插入数月值(7,'jul',3, 2)

插入数月值(8,'aug',3,2)

插入数月值(9,'sep',3,2)

插入数月值(10,'oct',4,2)

插入数月值(11,'nov',4,2)

插入月份值(12,'dec',4,2)

选择*从几个月





6.现在回到default.aspx页面

进入设计模式(在页面底部)

7.双击出现的网格。

8.它将被重定向到默认的aspx.cs页面

9.在该页面中它们将是一个函数



protected void Page_Load(object sender,EventArgs e)

{

}



10 。在该函数中编写以下代码





SqlConnection con = new SqlConnection(@Data Source =< from system = > ;; Initial Catalog = arc; Integrated Security = True); //< from system => - > SERVER EXPLORER - > DATA CONNECTIONS - > ADD CONNECTION

// - >服务器名称(选择) - > OK

//接下来右键单击连接 - >属性(复制连接

//字符串到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();







这是一个仅供初学者使用的简单示例

谢谢
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


这篇关于不使用DataSource控件的ASP.NET GridView示例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆