写出来的数据,自定义GridView控件中的每一行,并使用IEnumerable的底部加入插入行 [英] writing out the data for each row in custom gridview control and adding insert row at the bottom using IEnumerable

查看:325
本文介绍了写出来的数据,自定义GridView控件中的每一行,并使用IEnumerable的底部加入插入行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

林与在我的if语句写出的数据的问题,即如果(numRows行> 0)的code为假设在网格的底部以创建一个插入行。它wrties出与文本框插入行,但所以我可能需要遍历数据源和数据添加到我不知道如何适当的单元格不显示我的数据。因此,如果有人知道什么待办事项,并能够帮助这将是AP preciated样品code会有所帮助。谢谢

 公共类CustomGridView:GridView控件
{
    保护GridViewRow _footerRow2;
    保护GridViewRow _headerRow2;    保护覆盖INT的CreateChildControls(System.Collections.IEnumerable数据源,数据绑定布尔)
    {
        //调用基方法,并得到行数
        INT其行= base.CreateChildControls(数据源,数据绑定);        如果(其行== 0)
        {
            //创建数据行,创建空表
            //创建表
            表的表=新表();
            table.ID = this.ID;            //将已有的列转换成一个数组并初始化
            的DataControlField [] =领域新的DataControlField [this.Columns.Count]
            this.Columns.CopyTo(字段,0);            如果(this.ShowHeader)
            {
                //创建一个新的标题行
                _headerRow2 = base.CreateRow(-1,-1,DataControlRowType.Header,DataControlRowState.Normal);                this.InitializeRow(_headerRow2,场);
                _headerRow2.EnableTheming = TRUE;
                table.Rows.Add(_headerRow2);
            }            GridViewRow行= base.CreateRow(0,0,DataControlRowType.DataRow,DataControlRowState.Insert);
            this.InitializeRow(行,场);
            row.EnableTheming = TRUE;           如果(this.ShowFooter)
            {
                //创建页脚行
                _footerRow2 = base.CreateRow(-1,-1,DataControlRowType.Footer,DataControlRowState.Normal);                this.InitializeRow(_footerRow2,场);
                _footerRow2.EnableTheming = TRUE;
                table.Rows.Add(_footerRow2);
            }            this.Controls.Clear();
            this.Controls.Add(表);
        }
        否则如果(numRows行大于0)
        {
            //问题在这里我怎样写出来的集合
            // IEnumerable的,当我不知道该集合类型是什么            的DataControlField [] =领域新的DataControlField [this.Columns.Count]
            this.Columns.CopyTo(字段,0);            表的表=新表();
            table.ID = this.ID;            GridViewRow的insertRow = base.CreateRow(其行+ 1,其行+ 1,DataControlRowType.DataRow,DataControlRowState.Insert);
            this.InitializeRow(的insertRow,场);
            insertRow.Cells [0] .Controls.Add(新按钮的{text =插入,的CommandName =插入,ID =插入});
            table.Rows.Add(的insertRow);            this.Controls.Clear();
            this.Controls.Add(表);
        }        返回其行;
    }
}


解决方案

简短的回答是,你可能需要使用反射来确定的IEnumerable的类型。先从 dataSource.GetType()

Im have a problem with writing out the data in my if statement where if(numRows > 0) the code is suppose to create an insert row at the bottom of the grid. It wrties out the insert row with textboxes but it doesn't display my data so i probably need to iterate the datasource and add the data to the appropriate cells which i don't know how. so if someone knows what todo and is able to help that would be appreciated sample code will help. thanks

public class CustomGridView : GridView
{
    protected GridViewRow _footerRow2;
    protected GridViewRow _headerRow2;

    protected override int CreateChildControls(System.Collections.IEnumerable dataSource, bool dataBinding)
    {
        // Call base method and get number of rows
        int numRows = base.CreateChildControls(dataSource, dataBinding);

        if (numRows == 0)
        {
            //no data rows created, create empty table
            //create table
            Table table = new Table();
            table.ID = this.ID;

            //convert the exisiting columns into an array and initialize
            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);

            if (this.ShowHeader)
            {
                //create a new header row
                _headerRow2 = base.CreateRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);

                this.InitializeRow(_headerRow2, fields);
                _headerRow2.EnableTheming = true;
                table.Rows.Add(_headerRow2);
            }

            GridViewRow row = base.CreateRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Insert);
            this.InitializeRow(row, fields);
            row.EnableTheming = true;

           if (this.ShowFooter)
            {
                //create footer row
                _footerRow2 = base.CreateRow(-1, -1, DataControlRowType.Footer, DataControlRowState.Normal);

                this.InitializeRow(_footerRow2, fields);
                _footerRow2.EnableTheming = true;
                table.Rows.Add(_footerRow2);
            }

            this.Controls.Clear();
            this.Controls.Add(table);
        }
        else if (numRows > 0)
        {
            // problem is in here how do i write out a collection
            // of IEnumerable when I don't know what the collection type is

            DataControlField[] fields = new DataControlField[this.Columns.Count];
            this.Columns.CopyTo(fields, 0);

            Table table = new Table();
            table.ID = this.ID;

            GridViewRow insertRow = base.CreateRow(numRows + 1, numRows + 1, DataControlRowType.DataRow, DataControlRowState.Insert);
            this.InitializeRow(insertRow, fields);
            insertRow.Cells[0].Controls.Add(new Button { Text = "Insert", CommandName = "Insert", ID = "Insert" });
            table.Rows.Add(insertRow);

            this.Controls.Clear();
            this.Controls.Add(table);
        }

        return numRows;
    }
}

解决方案

The short answer is that you probably need to use reflection to determine the type of the IEnumerable. Start with dataSource.GetType().

这篇关于写出来的数据,自定义GridView控件中的每一行,并使用IEnumerable的底部加入插入行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆