Search code examples
c#asp.netcustom-server-controlsservercontrols

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


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

Solution

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