C# 在自定义gridview控件中写出每行的数据,并使用IEnumerable在底部添加插入行

C# 在自定义gridview控件中写出每行的数据,并使用IEnumerable在底部添加插入行,c#,asp.net,custom-server-controls,servercontrols,C#,Asp.net,Custom Server Controls,Servercontrols,Im在写if语句中的数据时遇到问题,如果(numRows>0)代码假定在网格底部创建一个插入行。它用文本框写出插入行,但它不显示我的数据,因此我可能需要迭代数据源并将数据添加到适当的单元格中,我不知道如何添加。所以,如果有人知道该做什么,并且能够提供帮助,那么示例代码将非常有用。谢谢 public class CustomGridView : GridView { protected GridViewRow _footerRow2; protected GridViewRow _

Im在写if语句中的数据时遇到问题,如果(numRows>0)代码假定在网格底部创建一个插入行。它用文本框写出插入行,但它不显示我的数据,因此我可能需要迭代数据源并将数据添加到适当的单元格中,我不知道如何添加。所以,如果有人知道该做什么,并且能够提供帮助,那么示例代码将非常有用。谢谢

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;
    }
}

简而言之,您可能需要使用反射来确定IEnumerable的类型。从
dataSource.GetType()

开始查看如何在asp.net GridView中插入新行的示例代码:

编辑

如果出于某种原因不想使用页脚,请参见此处的示例代码:

我认为这超出了我的能力范围,我不知道该怎么做,如果我能做upo求和,这可能会帮助我理解,也许有一个更简单的方法。您能解释一下为什么首先要重写GridView控件吗?