DataGrid動態添加模板列的一個例子

來源:互聯網
上載者:User
datagrid|動態|模板 資料來源結構同http://blog.csdn.net/lovecherry/archive/2005/03/24/328455.aspx
表dep:depid(標識主鍵),depname(學院名字)
表stu:stuid(標識主鍵),stuname(學生名字),studepid(學院id=表dep.depid)

前台:
<%@ Page language="c#" Codebehind="WebForm30.aspx.cs" AutoEventWireup="false" Inherits="csdn.WebForm30" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm30</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<link href="css.css" rel="stylesheet" type="text/css">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
</form>
</body>
</HTML>
後台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace csdn
{
/// <summary>
/// WebForm30 的摘要說明。
/// </summary>
public class WebForm30 : System.Web.UI.Page
{
DataGrid DataGrid1=new DataGrid();
private void Page_Load(object sender, System.EventArgs e)
{
// 在此處放置使用者代碼以初始化頁面
CreateDataGrid();
}

protected void CreateDataGrid()
{
DataGrid1.AutoGenerateColumns=false;
DataGrid1.CssClass="border";
DataGrid1.BorderWidth=0;
DataGrid1.CellSpacing=1;
DataGrid1.CellPadding=5;
DataGrid1.ItemStyle.CssClass="item";
DataGrid1.HeaderStyle.CssClass="header";
DataGrid1.DataKeyField="stuid";
//以上設定DataGrid的樣式
TemplateColumn tm=new TemplateColumn();
tm.ItemTemplate=new ColumnTemplate1();
tm.HeaderText="姓名";
DataGrid1.Columns.Add(tm);
//建立第一個模板列
TemplateColumn tm2=new TemplateColumn();
tm2.ItemTemplate=new ColumnTemplate2();
tm2.HeaderText="學院";
DataGrid1.Columns.Add(tm2);
//建立第二個模板列
ButtonColumn bc=new ButtonColumn();
bc.ButtonType=ButtonColumnType.PushButton;
bc.CommandName="del";
bc.Text="刪除";
DataGrid1.Columns.Add(bc);
//建立刪除按鈕列
SetBind();
//填充資料
Page.Controls[1].Controls.Add(DataGrid1);
//給頁面的form加入這個DataGrid1
}

protected void SetBind()
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from stu,dep where stu.studepid=dep.depid",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
this.DataGrid1.DataSource=ds.Tables["table1"];
this.DataGrid1.DataBind();

}

private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
//和上面串連給出的例子中的代碼一樣,給下拉框綁定資料,並且選擇預設的
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]);
SqlDataAdapter da=new SqlDataAdapter("select * from dep",conn);
DataSet ds=new DataSet();
da.Fill(ds,"table1");
if(e.Item.ItemType==ListItemType.Item||e.Item.ItemType==ListItemType.AlternatingItem)
{
DropDownList ddl=(DropDownList)e.Item.FindControl("dep");
ddl.DataSource=ds.Tables["table1"];
ddl.DataTextField="depname";
ddl.DataValueField="depid";
ddl.DataBind();
ddl.Items.FindByValue(Convert.ToString(DataBinder.Eval(e.Item.DataItem,"depid"))).Selected=true;
}
}

private void DataGrid1_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
if(e.CommandName=="del")
{
SqlConnection conn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["conn"]



相關文章

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。