writing out the data for each row in custom gridview control and adding insert row at the bottom using IEnumerable

帅比萌擦擦* 提交于 2019-12-13 20:43:00

问题


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

回答1:


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




回答2:


See this sample code of how to insert a new row in an asp.net GridView:
http://amitpatriwala.wordpress.com/2008/04/18/inserting-new-row-in-gridview-in-aspnet-20/

Edit

If you'd like to not use the footer for some reason, see the sample code here:
http://forums.asp.net/t/1496213.aspx



来源:https://stackoverflow.com/questions/8698008/writing-out-the-data-for-each-row-in-custom-gridview-control-and-adding-insert-r

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!