Click here to Skip to main content
15,880,796 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, im trying to rename headers of a gridview but the count is one and hence the last line is giving me an index out of range error. Im first creating a datatabe (the count is 19) then reordering a column (works fine) then when i try to assign a new header text the count is one and the error occurs. any help would be appreciated! thanks in advance!

using (DataProvider errors = new DataProvider())
        {
            DataSet ds = errors.GetErrors(this);

            DataTable dt = ds.Tables[0];
            
            int i = dt.Columns.Count;
            
            dt.Columns[18].SetOrdinal(0);            

            this.mainGridView.DataSource = dt;

            this.mainGridView.DataBind();
            
            this.mainGridView.Columns[2].HeaderText = "ReqID";                        
        }


note: this is my grid view

C#
<asp:GridView   ID="mainGridView" 
                               runat="server"                 OnPageIndexChanging="mainGridView_PageIndexChanging" 
                                                AllowPaging="True" 
                                                AllowSorting="True"
                                                PageSize = "12" 
                                                OnRowCreated="mainGridView_RowCreated"                                        
                                                Font-Size="10px">
                                    <AlternatingRowStyle BackColor="#99CCFF" />
                                    <HeaderStyle BackColor="#009BDE"  Font-Size="14px" Height="20px" 
                                        VerticalAlign="Middle" ForeColor="White" />
                                    <Columns>
                                        <asp:TemplateField>
                                            <ItemTemplate>
                                                <asp:CheckBox ID="selectCheckbox" runat="server" />
                                            </ItemTemplate>
                                            <ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                                            <HeaderTemplate>
                                                <asp:CheckBox ID="headerCheckbox" 
                                                     onclick="javascript:HeaderClick(this);" runat="server" />                             
                                            </HeaderTemplate>
                                            <HeaderStyle HorizontalAlign="Center" VerticalAlign="Middle" Width="50px" />
                                        </asp:TemplateField>
                                    </Columns>
                                </asp:GridView>


note: im calling a stored procedure. code below.

public DataSet GetErrors(IHome getErrors)
{
    DataSet dataset = new DataSet();

    OpenConnection();
    try
    {
        using (ISqlCommand command = CreateCommand())
        {
            command.SetCommandText(spGetErrors, System.Data.CommandType.StoredProcedure);
            command.SetDateParameter("@ADate", getErrors.Date);

            command.OpenCommand(dataset, "getErrors");
        }
    }
    finally
    {
        CloseConnection();
    }

    return dataset;
}
Posted
Updated 28-Oct-13 17:31pm
v3
Comments
Jim Jos 28-Oct-13 23:24pm    
After assigning the data source why are you clearing the columns? Could you please explain..
nebiam 28-Oct-13 23:27pm    
I was just using that for testing purposes, its not meant to be there. I was trying to clear then add columns, ill edit it out.
Jim Jos 28-Oct-13 23:41pm    
Could you run and check without clear...
nebiam 28-Oct-13 23:42pm    
No. Initially i didn't have the clear, i only used it to try and add the columns another way. The code in the example is what i had initially/currently.
Jim Jos 28-Oct-13 23:51pm    
OK.. You are getting the last column and reordering it in the data table only. Please verify after calling DataBind() whether the datagrid has all the data assigned..

1 solution

as answered by JimJos

using (DataProvider errors = new DataProvider())
        {
            DataSet ds = errors.GetErrors(this);
 
            DataTable dt = ds.Tables[0];
            
            int i = dt.Columns.Count;
            
            dt.Columns[18].SetOrdinal(0);  

            this.mainGridView.AutoGenerateColumns = false;

            foreach (DataColumn col in dt.Columns)
            {
                BoundField field = new BoundField();

                field.DataField = col.ColumnName;

                field.HeaderText = col.ColumnName;

                mainGridView.Columns.Add(field);
            }

            this.mainGridView.Columns[0].HeaderText = "Error";
            this.mainGridView.Columns[1].HeaderText = "ReqID";
            this.mainGridView.Columns[2].HeaderText = "SysID";          
 
            this.mainGridView.DataSource = dt;
 
            this.mainGridView.DataBind();    

                   
        }
 
Share this answer
 

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