Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following asp.net code:

ASP.NET
<table class="taskGridView"  runat="server">
    <tr>
        <td>
            <asp:DropDownList ID="ddlTaskName" CssClass="chosen-select" DataSourceID="dsPopulateTaskName" AutoPostBack="true" DataValueField="Task Name" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlTaskName_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateTaskName" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlService" CssClass="chosen-select" DataSourceID="dsPopulateService" AutoPostBack="true" DataValueField="Service" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlService_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateService" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlStatus" CssClass="chosen-select" DataSourceID="dsPopulateStatus" AutoPostBack="true" DataValueField="Status" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlStatus_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateStatus" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlDueDate" CssClass="chosen-select" DataSourceID="dsPopulateDueDate" AutoPostBack="true" DataValueField="Due Date" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlDueDate_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateDueDate" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlOwner" CssClass="chosen-select" DataSourceID="dsPopulateOwner" AutoPostBack="true" DataValueField="Owner" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlOwner_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateOwner" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlClient" CssClass="chosen-select" DataSourceID="dsPopulateClient" AutoPostBack="true" DataValueField="Client" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlClient_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateClient" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlSite" CssClass="chosen-select" DataSourceID="dsPopulateSite" AutoPostBack="true" DataValueField="Site" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlSite_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateSite" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlPractice" CssClass="chosen-select" DataSourceID="dsPopulatePractice" AutoPostBack="true" DataValueField="Practice" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlPractice_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulatePractice" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
        <td>
            <asp:DropDownList ID="ddlProvider" CssClass="chosen-select" DataSourceID="dsPopulateProvider" AutoPostBack="true" DataValueField="Provider" runat="server" Width="100%" Font-Size="11px" AppendDataBoundItems="true" OnSelectedIndexChanged="ddlProvider_onSelectIndexChanged">
                <asp:ListItem Text="All" Value="%"></asp:ListItem>
            </asp:DropDownList>
            <asp:SqlDataSource ID="dsPopulateProvider" runat="server" ConnectionString="<%$ ConnectionStrings:gvConnString %>" SelectCommand=""></asp:SqlDataSource>
        </td>
    </tr>
</table>
<asp:GridView ShowHeaderWhenEmpty="false" AlternatingRowStyle-BackColor="#EBE9E9" AutoGenerateColumns="false" OnSorting="yourTasksGV_Sorting" AllowSorting="true" ID="yourTasksGV" runat="server" ClientIDMode="Static" EmptyDataText="You currently have no tasks assigned to you" OnRowDataBound="yourTasksGV_RowDataBound" OnRowCreated="yourTasksGV_RowCreated">
    <Columns>
        <asp:HyperLinkField Target="_self" DataNavigateUrlFields="Task Detail" DataTextField="Task Name" DataNavigateUrlFormatString="" HeaderText="Task Detail" SortExpression="Task Name" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Service" HeaderText="Service" SortExpression="Service" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Due Date" HeaderText="Due" SortExpression="Due Date" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Owner" HeaderText="Owner" SortExpression="Owner" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Client" HeaderText="Client" SortExpression="Client" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Site" HeaderText="Site" SortExpression="Site" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Practice" HeaderText="Practice" SortExpression="Practice" ItemStyle-CssClass="taskTableColumn" />
        <asp:BoundField DataField="Provider" HeaderText="Provider" SortExpression="Provider" ItemStyle-CssClass="taskTableColumn" />
    </Columns>
</asp:GridView>


code-behind:

C#
protected void yourTasksGV_RowCreated(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {
        GridView hGrid = (GridView)sender;
        GridViewRow gvrRow = new GridViewRow(0, 0, DataControlRowType.Header, DataControlRowState.Insert);


        TableHeaderCell tcCellTask = new TableHeaderCell();
        tcCellTask.Controls.Add(ddlTaskName);
        gvrRow.Cells.Add(tcCellTask);
        yourTasksGV.Controls[0].Controls.AddAt(0, gvrRow);

        TableHeaderCell tcCellSvc = new TableHeaderCell();
        tcCellSvc.Controls.Add(ddlService);
        gvrRow.Cells.Add(tcCellSvc);
        yourTasksGV.Controls[0].Controls.AddAt(1, gvrRow);

        /*TableHeaderCell tcCellStats = new TableHeaderCell();
        tcCellStats.Controls.Add(ddlStatus);
        gvrRow.Cells.Add(tcCellStats);
        yourTasksGV.Controls[0].Controls.AddAt(2, gvrRow);

        TableHeaderCell tcCellDD = new TableHeaderCell();
        tcCellDD.Controls.Add(ddlDueDate);
        gvrRow.Cells.Add(tcCellDD);
        yourTasksGV.Controls[0].Controls.AddAt(3, gvrRow);

        TableHeaderCell tcCellOwner = new TableHeaderCell();
        tcCellOwner.Controls.Add(ddlOwner);
        gvrRow.Cells.Add(tcCellOwner);
        yourTasksGV.Controls[0].Controls.AddAt(4, gvrRow);

        TableHeaderCell tcCellCli = new TableHeaderCell();
        tcCellCli.Controls.Add(ddlClient);
        gvrRow.Cells.Add(tcCellCli);
        yourTasksGV.Controls[0].Controls.AddAt(5, gvrRow);

        TableHeaderCell tcCellSit = new TableHeaderCell();
        tcCellSit.Controls.Add(ddlSite);
        gvrRow.Cells.Add(tcCellSit);
        yourTasksGV.Controls[0].Controls.AddAt(6, gvrRow);

        TableHeaderCell tcCellPra = new TableHeaderCell();
        tcCellPra.Controls.Add(ddlPractice);
        gvrRow.Cells.Add(tcCellPra);
        yourTasksGV.Controls[0].Controls.AddAt(7, gvrRow);

        TableHeaderCell tcCellPro = new TableHeaderCell();
        tcCellPro.Controls.Add(ddlProvider);
        gvrRow.Cells.Add(tcCellPro);
        yourTasksGV.Controls[0].Controls.AddAt(8, gvrRow);*/

        LinkButton btnSort;
        System.Web.UI.WebControls.Image image;
        //iterate through all the header cells
        foreach (TableCell cell in e.Row.Cells)
        {
            //check if the header cell has any child controls
            if (cell.HasControls())
            {
                //get reference to the button column
                btnSort = (LinkButton)cell.Controls[0];
                image = new System.Web.UI.WebControls.Image();
                if (ViewState["sortExp"] != null)
                {
                    //see if the button user clicked on and the sortexpression in the viewstate are same
                    //this check is needed to figure out whether to add the image to this heade column or not
                    if (btnSort.CommandArgument == ViewState["sortExp"].ToString())
                    {
                        //check what type of sort order it is
                        if (ViewState["sortOrder"].ToString() == "Asc")
                        {
                            image.ImageUrl = "../theImages/up.png";
                            image.CssClass = "hdrImage";
                        }
                        else
                        {
                            image.ImageUrl = "../theImages/down.png";
                            image.CssClass = "hdrImage";
                        }
                        cell.Controls.Add(image);
                    }
                }
            }
        }
    }
}


CSS
After creating the first two header cell, when it gets to the third and consequent one, I get an error stating:

Specified argument was out of the range of valid values.

Parameter name: index

I have 9 columns in the GridView but I am not sure why I am receiving the error.

How do I resolve it?

If I uncomment everything, only three columns are created but I still get the same error.
Posted

1 solution

You don't state exactly where it fails, but I guess that this is the line that fails.
C#
yourTasksGV.Controls[0].Controls.AddAt(2, gvrRow);


As you are using .AddAt, I suggest you check how many controls you have.

C#
int controlCount = yourTasksGV.Controls[0].Controls.Count;


If the count = 2, you will get an error when you try access the third position.
 
Share this answer
 
Comments
Member 10850583 2-Sep-14 14:40pm    
I had to add <pre lang="c#">yourTasksGV.Controls[0].Controls.AddAt(0, gvrRow);</pre> only once toward the end. Thank you

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900