在后代码里创建DataGrid控件
2024-07-21 02:23:05
供稿:网友
在后代码里创建datagrid控件
本文介绍如何用代码创建datagrid,并且有四个绑定列和一个模板列,支持排序功能。
代码如下:
c#
createdatagrid.aspx
<%@ page language="c#" enableviewstate = "false"codebehind="createdatagrid.aspx.cs"
autoeventwireup="true" inherits="aspxwebcs.mydatagrid" %>
<script runat="server">
public void page_load(object sender,eventargs e){
createdatagridform.controls.add(makegrid());
}
</script>
<!doctype html public "-//w3c//dtd html 4.0 transitional//en" >
<html>
<head>
<title>用代码创建datagrid</title>
<meta name="generator" content="microsoft visual studio 7.0">
<meta name="code_language" content="c#">
<meta name="vs_defaultclientscript" content="javascript">
<meta name="vs_targetschema" content="http://schemas.microsoft.com/intellisense/ie5">
</head>
<body ms_positioning="gridlayout">
<form id="createdatagridform" method="post" runat="server">
<div align="center"><b>用代码创建datagrid</b></div>
</form>
</body>
</html>
createdatagrid.aspx.cs
using system;
using system.configuration;
using system.data;
using system.data.sqlclient;
using system.web;
using system.web.ui;
using system.web.ui.webcontrols;
using system.web.ui.htmlcontrols;
using system.drawing;
/// <summary>
/// createdatagrid 的摘要说明。
/// </summary>
namespace aspxwebcs
{
public class mydatagrid : page
{
public string sql = "select firstname,lastname,homephone,title from employees";
public datagrid mygrid = new datagrid();
public string sortexpression;
/// <summary>
/// 创建一个模板列和一个列模板
/// </summary>
public templatecolumn tm = new templatecolumn();
public columntemplate mycol = new columntemplate();
//返回dataview
public dataview createdatasource ()
{
string strsql;
strsql = "data source=.;initial catalog=northwind;user id=sa;password=;";
sqlconnection conn = new sqlconnection(strsql);
sqldataadapter db_sqladaptor = new sqldataadapter(sql,conn);
dataset ds = new dataset();
db_sqladaptor.fill(ds,"employees");
dataview myview = ds.tables["employees"].defaultview;
//myview.sort=sortexpression;
//response.write(sql);
return myview;
}
/// <summary>
/// 处理排序
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
public void sort_grid(object sender, datagridsortcommandeventargs e)
{
sortexpression = e.sortexpression.tostring();
session["sortfield"]=sortexpression.trim();
if(session["order"]==null) session["order"] = "asc";
session["order"] = (session["order"].tostring()=="desc")?"asc":"desc";
if(session["sortfield"]==null) session["sortfield"] = "firstname";
sql += " order by "+session["sortfield"].tostring() + " " + session["order"].tostring();
mygrid.datasource = createdatasource();
mygrid.databind();
}
/// <summary>
/// 创建和设置datagrid属性,这里的属性设置为固定值,但也可以动态设置
/// </summary>
/// <returns></returns>
public datagrid makegrid()
{
mygrid.cellpadding=2;
mygrid.attributes.add("align","center");
mygrid.cellspacing=0;
mygrid.width=500;
mygrid.borderwidth=1;
mygrid.bordercolor=colortranslator.fromhtml("black");
mygrid.autogeneratecolumns=false;
mygrid.forecolor=colortranslator.fromhtml("black");
mygrid.font.size=9;
mygrid.font.name="宋体";
mygrid.allowsorting=true;
///sort命令的事件处理器
mygrid.sortcommand += new datagridsortcommandeventhandler(sort_grid);
///设置headerstyle
mygrid.headerstyle.backcolor=colortranslator.fromhtml("gold");
mygrid.headerstyle.forecolor=colortranslator.fromhtml("black");
mygrid.headerstyle.font.name="宋体";
mygrid.headerstyle.font.size=9;
mygrid.headerstyle.font.bold=true;
mygrid.headerstyle.horizontalalign=horizontalalign.center;
///设置alternating style
mygrid.alternatingitemstyle.backcolor=colortranslator.fromhtml("silver");
mygrid.alternatingitemstyle.forecolor=colortranslator.fromhtml("black");
///设置itemstyle
mygrid.itemstyle.horizontalalign=horizontalalign.left;
///创建绑定列和属性
boundcolumn firstname = new boundcolumn();
boundcolumn lastname = new boundcolumn();
boundcolumn homephone = new boundcolumn();
boundcolumn title = new boundcolumn();
firstname.headertext="名字";
firstname.datafield="firstname";
firstname.sortexpression="firstname";
lastname.headertext="姓";
lastname.datafield="lastname";
lastname.sortexpression="lastname";
homephone.headertext="电话";
homephone.datafield="homephone";
homephone.sortexpression="homephone";
title.headertext="职务";
title.datafield="title";
title.sortexpression="title";
mygrid.columns.addat(0, firstname);
mygrid.columns.addat(1, lastname);
mygrid.columns.addat(2, homephone);
mygrid.columns.addat(3, title);
///设置模板列属性和itemstyle模板
tm.headertext="**删除信息**";
tm.headerstyle.horizontalalign=horizontalalign.center;
tm.itemstyle.backcolor = colortranslator.fromhtml("#fff778");
tm.itemstyle.horizontalalign=horizontalalign.center;
///创建列模板。
///列模板从itemplate继承
tm.itemtemplate = mycol;
mygrid.columns.addat(4, tm);
///绑定和返回
mygrid.datasource = createdatasource();
mygrid.databind();
return mygrid;
}
}
/// columntemplate 从itemplate继承。
/// "instantiatein"定义子控件的属于谁
public class columntemplate : itemplate
{
public void instantiatein(control container)
{
label mylabel = new label();
mylabel.text="点击删除";
checkbox mycheckbox = new checkbox();
container.controls.add(mylabel);
container.controls.add(mycheckbox);
}
}
}