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

farzad khajeh 1 Reputation point

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

                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();
                    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);

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

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
6,961 questions
{count} votes