返回首页
当前位置: 主页 > 网络编程 > .Net实例教程 >

c#实现动态添加GridView行 代码及教程

时间:2012-08-19 16:02来源:betway必威官网www.etsupport.net 编辑:麦田守望者

C#代码:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
BindGrid();
}
}

private DataTable ReadGridView()
{
DataTable dt = new DataTable();
DataRow dr;
dt.Columns.Add(new DataColumn("ProductID", typeof(string)));
dt.Columns.Add(new DataColumn("ProductName", typeof(string)));
dt.Columns.Add(new DataColumn("CategoryID", typeof(string)));
for (int i = 0; i < this.GridView1.Rows.Count; i++)
{
dr = dt.NewRow();
dr[0] = this.GridView1.Rows[i].Cells[0].Text.Trim();
dr[1] = this.GridView1.Rows[i].Cells[1].Text.Trim();
dr[2] = this.GridView1.Rows[i].Cells[2].Text.Trim();
dt.Rows.Add(dr);
}
return dt;
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable dt = ReadGridView();
//this.GridView1.DataSource = dt;
//this.GridView1.DataBind();
DataRow row = dt.NewRow();
row.ItemArray = new object[] { "oec2003", "oec2003", "oec2003" };
dt.Rows.InsertAt(row, 0);
dt.AcceptChanges();
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}

private void BindGrid()
{
string str = ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ToString();
using (SqlConnection con = new SqlConnection(str))
{
SqlCommand cmd = new SqlCommand("SELECT top 1 [ProductID], [ProductName], [CategoryID] FROM [Products]", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.GridView1.DataBind();
sda.Dispose();
ds.Dispose();
}

}
}
Html代码

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 、
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>无标题页</title>
</head>
<body>
<form id="form2" runat="server">
<div>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="ProductID">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False"
ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID" />
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
数据库连接字串

<connectionStrings>
<add name="NorthwindConnectionString"
connectionString="Data Source=FENGWEI;Initial Catalog=Northwind;User ID=sa;Password=1234"
providerName="System.Data.SqlClient"/>
</connectionStrings>

顶一下
(0)
0%
踩一下
(0)
0%
标签(Tag):C# C#实例教程 c#基础教程 C#源代码 c#技巧
------分隔线----------------------------
------分隔线----------------------------
发表评论
请自觉遵守互联网相关的政策法规,严禁发布色情、暴力、反动的言论。
评价:
表情:
验证码:点击我更换图片