hulu

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

不是原来的结果了。 看来是自定义字段显示出错了。
posted on 2007-09-06 17:19  hulu  阅读(1010)  评论(0编辑  收藏  举报