惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
博客园 - 司徒正美
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
大猫的无限游戏
大猫的无限游戏
酷 壳 – CoolShell
酷 壳 – CoolShell
Forbes - Security
Forbes - Security
D
Darknet – Hacking Tools, Hacker News & Cyber Security
小众软件
小众软件
人人都是产品经理
人人都是产品经理
Know Your Adversary
Know Your Adversary
Security Latest
Security Latest
雷峰网
雷峰网
Cisco Talos Blog
Cisco Talos Blog
Latest news
Latest news
GbyAI
GbyAI
Last Week in AI
Last Week in AI
Hacker News: Ask HN
Hacker News: Ask HN
U
Unit 42
S
SegmentFault 最新的问题
月光博客
月光博客
Security Archives - TechRepublic
Security Archives - TechRepublic
Attack and Defense Labs
Attack and Defense Labs
S
Secure Thoughts
N
News and Events Feed by Topic
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
A
Arctic Wolf
Schneier on Security
Schneier on Security
C
CERT Recently Published Vulnerability Notes
I
Intezer
V
Visual Studio Blog
Microsoft Security Blog
Microsoft Security Blog
SecWiki News
SecWiki News
Google Online Security Blog
Google Online Security Blog
N
Netflix TechBlog - Medium
I
InfoQ
T
Tor Project blog
腾讯CDC
T
Tenable Blog
Webroot Blog
Webroot Blog
Y
Y Combinator Blog
TaoSecurity Blog
TaoSecurity Blog
Google DeepMind News
Google DeepMind News
AI
AI
C
Cisco Blogs
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V2EX - 技术
V2EX - 技术
PCI Perspectives
PCI Perspectives
C
CXSECURITY Database RSS Feed - CXSecurity.com
G
GRAHAM CLULEY
S
Schneier on Security

博客园 - hulu

从一个int值显示相应枚举类型的名称或者描述 第8章 商品目录管理 第4章 创建商品目录 第2,第三章 概要 Scott Mitchell的ASP.NET2.0数据指南中文版索引 7/14 原型编码阶段里程碑完成 asp.net2.0 的 gridview 7/9 原型编码阶段: (6) 使用DetailsView和FormView(7-5 11:54) - hulu 7/5 原型编码阶段: (5) 使用ADO.NET 7/3 原型编码阶段: (4) GridView的编辑更新 成功软件开发者的9种编程习惯(一) 6/29 原型编码阶段:(3) CommandField,TemplateField 6/29 原型编码阶段:(2) GridView的数据库操作 6/29 原型编码阶段:(1)连接并访问数据库 6/29 项目目录结构有所调整 6/28 项目原型前期实现任务确定 C# 2.0 除了泛型 C# 2.0 泛型
如何自定义gridview的表格显示?
hulu · 2007-09-06 · via 博客园 - hulu

这个问题虽然比较实际,但是无意义。除非gridview只用于列表显示,不做交互。否则一些刷新操作重新绑定的操作会显示出错。
gridview的显示可以达到这个效果:

申请单 编号 方向 目的地 时间
事由
NT200708300100044 市内方向 开发区 2007-8-31 8:00:00
2007-8-31 18:30:00
NT200708310100048 市内方向 开发区 2007-9-1 8:00:00
2007-9-1 18:30:00
NT200708310100049 省内方向 南京 2007-9-1 8:00:00
2007-9-1 18:30:00

怎么做到的呢?
首先 page_load

        if (!IsPostBack)
        
{
            setGridViewStyle();
            setFields();
        }

setGridViewStyle

    //设置GridView外观格式
    private void setGridViewStyle()
    
{
        gvApplyList.AutoGenerateColumns 
= false;
        
string[] KeyNames = new string[] "Sheet_ID"};
        gvApplyList.DataKeyNames 
= KeyNames;
    }

setFields

    //设置GridView字段
    private void setFields()
    
{
        
//创建命令字段
        CommandField editField = new CommandField();
        editField.ButtonType 
= ButtonType.Button;
        editField.ShowSelectButton 
= true;
        editField.SelectText 
= "操作";

        
//创建数据绑定字段
        BoundField sheetNRBField = new BoundField();
        BoundField startDTField 
= new BoundField();
        BoundField endDTField 
= new BoundField();
        BoundField dirGrpNameTField 
= new BoundField();
        BoundField outDestField 
= new BoundField();
        TemplateField appNoteField 
= new TemplateField();

        sheetNRBField.DataField 
= "sheet_NBR";
        sheetNRBField.HeaderText 
= "编号";

        startDTField.DataField 
= "Start_DT";
        startDTField.HeaderText 
= "开始时间";

        endDTField.DataField 
= "End_DT";
        endDTField.HeaderText 
= "结束时间";

        dirGrpNameTField.DataField 
= "dir_Grp_Name";
        dirGrpNameTField.HeaderText 
= "方向";

        outDestField.DataField 
= "out_Dest";
        outDestField.HeaderText 
= "目的地";

        appNoteField.ItemTemplate 
= new GridViewTemplate(DataControlRowType.DataRow, "事由");

        
//将字段添加到GridView
        gvApplyList.Columns.Add(editField);
        gvApplyList.Columns.Add(sheetNRBField);
        gvApplyList.Columns.Add(dirGrpNameTField);
        gvApplyList.Columns.Add(outDestField);
        gvApplyList.Columns.Add(startDTField);
        gvApplyList.Columns.Add(appNoteField);
        gvApplyList.Columns.Add(endDTField);

    }

重定义自定义字段模式

    //模板类产生器,以创建模板字段
    public class GridViewTemplate : ITemplate
    
{
        
private DataControlRowType templateType;
        
private string columnName;

        
public GridViewTemplate(DataControlRowType rowtype, string colname)
        
{
            templateType 
= rowtype;
            columnName 
= colname;
        }


        
public void InstantiateIn(System.Web.UI.Control container)
        
{
            
if (templateType == DataControlRowType.DataRow)
            
{
                TextBox txtAppNote 
= new TextBox();
                
switch (columnName)
                
{
                    
case "事由":
                        txtAppNote.DataBinding 
+= new EventHandler(txtAppNote_DataBinding);
                        txtAppNote.Width 
= 300;
                        txtAppNote.ReadOnly 
= true;
                        container.Controls.Add(txtAppNote);
                        
break;
                }

            }

        }


        
void txtAppNote_DataBinding(object sender, EventArgs e)
        
{
            TextBox txtAppNote 
= (TextBox)sender;
            GridViewRow row 
= (GridViewRow)txtAppNote.NamingContainer;
            txtAppNote.Text 
= DataBinder.Eval(row.DataItem, "App_Note").ToString();
        }

    }

创建表头 创建数据行

   //创建GridView表头
    protected void gvApplyList_RowCreated(object sender, GridViewRowEventArgs e)
    
{
        
if (e.Row.RowType == DataControlRowType.Header)
        
{
            TableCellCollection tc 
= e.Row.Cells;
            tc.Clear();

            tc.Add(
new TableHeaderCell());
            tc[
0].Attributes.Add("rowspan""2");
            tc[
0].Text = "申请单";

            tc.Add(
new TableHeaderCell());
            tc[
1].Text = "编号";

            tc.Add(
new TableHeaderCell());
            tc[
2].Text = "方向";

            tc.Add(
new TableHeaderCell());
            tc[
3].Text = "目的地";

            tc.Add(
new TableHeaderCell());
            tc[
4].Attributes.Add("rowspan""2");
            tc[
4].Text = "时间</th></tr></tr>";

            tc.Add(
new TableHeaderCell());
            tc[
5].Attributes.Add("colspan","3");
            tc[
5].Text = "事由";

        }

    }

    
    
//创建数据行
    protected void gvApplyList_RowDataBound(object sender, GridViewRowEventArgs e)
    
{
        e.Row.Cells[
0].RowSpan = 2;
        e.Row.Cells[
0].RowSpan = 2;
        e.Row.Cells[
4].Text += "</td></tr><tr>";
        e.Row.Cells[
5].ColumnSpan = 3;
    }

gridview前台就这么调用:

<asp:GridView ID="gvApplyList" runat="server" OnRowCreated="gvApplyList_RowCreated" OnRowDataBound="gvApplyList_RowDataBound">

选择行操作后,刷新gridview

    protected void gvApplyList_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    
{
        setFields();
    }

不过实际运行的时候,点击操作后,gridview变成:

申请单 编号 方向 目的地 时间
事由
NT200708300100044 市内方向 开发区 2007-8-31 8:00:00
2007-8-31 18:30:00
NT200708310100048 市内方向 开发区 2007-9-1 8:00:00
2007-9-1 18:30:00
NT200708310100049 省内方向 南京 2007-9-1 8:00:00
2007-9-1 18:30:00

不是原来的结果了。 看来是自定义字段显示出错了。