question

farzadkhajeh-3020 avatar image
0 Votes"
farzadkhajeh-3020 asked farzadkhajeh-3020 commented

Tab Page Insert and Remove with gridview HeaderText has index was out of range error

Hello Dear All

In C# Application i have a form with multiple Tab Pages on it and each tab page has a grid view on it.
When I try to Insert and Remove Tabs bellow error pop up.

"index was out of range. must be non-negative and less than the size of the collection"

what I did exactly :

1- I populate all grid views with bellow sample code :

 public void FiLL_Sub3()
         {
               
 string      strCommandText = @"SELECT        USERS.User_Name, USERS_GRO.Discipline_Name, USERS.User_Tel,
                                                   CONCAT(CAST(CUS_CON_FLW.LastAction AS TIME(0)), ' ', CUS_CON_FLW.LastAction) AS LastAction, 
                                                              CUS_CON_STA.CUS_CON_STA_Name, CUS_CON_FLW.FLW_Note
                                     FROM            CUS_CON INNER JOIN
                                                              CUS_CON_FLW ON CUS_CON.CUS_CON_ID = CUS_CON_FLW.CUS_CON_ID LEFT OUTER JOIN
                                                              USERS_GRO RIGHT OUTER JOIN
                                                              USERS ON USERS_GRO.Discipline_ID = USERS.Discipline_ID ON CUS_CON_FLW.LastUser = USERS.User_ID LEFT OUTER JOIN
                                                              CUS_CON_STA ON CUS_CON_FLW.CUS_CON_STA_ID = CUS_CON_STA.CUS_CON_STA_ID
                         WHERE CUS_CON.CUS_DEV_ID = '" + CUS_DEV_ID.Text + "' ORDER BY CUS_CON_FLW.ID";
    
    
             try
             {
                 DataManagement.DT = DataManagement.Search(strCommandText);
                 if (DataManagement.DT.Rows.Count > 0)
                 {
                     Sub3.DataSource = DataManagement.DT;
    
                         Sub3.Columns[0].HeaderText = "User Name";   <<<<<<<< Error show here
                         Sub3.Columns[1].HeaderText = "Discipline";
                         Sub3.Columns[2].HeaderText = "Int Tel";
                         Sub3.Columns[3].HeaderText = "Date";
                         Sub3.Columns[4].HeaderText = "Wo Status";
                         Sub3.Columns[5].HeaderText = "Note";
    
                     Sub3.Columns[0].Width = 100;
                     Sub3.Columns[1].Width = 100;
                     Sub3.Columns[2].Width = 80;
                     Sub3.Columns[3].Width = 200;
                     Sub3.Columns[4].Width = 100;
                     if (SCO.State != ConnectionState.Closed) SCO.Close();
                 }
                 else
                 {
                     Sub3.DataSource = null;
                 }
             }
             catch { DBProblem.Form(this); }
         }
    
    
 2- On Load form I used this code for insert and remove tab pages
    
 if (Sub3.Rows.Count > 0)
                     {
                         if (All_Pages.TabPages["Flow_Tab"] == null)
                         {
                             All_Pages.TabPages.Insert(2, Flow_Tab);
                         }
                     }
                     else
                     {
                         All_Pages.TabPages.Remove(Flow_Tab);
                     }

Explain texts------------------------------------------------------
Sub3 is my grid view name. not matter.
DataManagement.DT is a class to populate grid views not matter.
DBProblem.Form(this); is a class for managing errors not matter.
Flow_Tab is my 3rd tab page and first error show in bellow line :
Sub3.Columns[0].HeaderText = "User Name";


appreciate any help
thanks
Farzad

dotnet-csharp
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Index out of range error means you are trying to access something which is not in the collection.

If you change this line


  All_Pages.TabPages.Insert(2, Flow_Tab);

to

  All_Pages.TabPages.Add( Flow_Tab);

Do you still get the error on insert?

with this code. I would add a check to see if Flow_Tab is in the collection before trying to remove it

                   {
                      if(All.Pages.TabPages.Contains(Flow_Tab)){
                              All_Pages.TabPages.Remove(Flow_Tab);
                       }
                   }

0 Votes 0 ·

Hi dear Ken Tucker
thanks for your response

yes i did before but i still get error

i thought this maybe happened because of tab page order , and changed it you add instead of insert but i doesn't work.

thanks anyway
farzad

0 Votes 0 ·

Hi @farzadkhajeh-3020,
Mark sure that datasource is valid and column count is greater than 0
You can check if your Header is correct.
int i = gvOffer.Columns.Count;
And you can also try the user311509's answer.
Best Regards,
Daniel Zhang


0 Votes 0 ·
Show more comments

0 Answers