none
Index out of Range in datagrid.select() RRS feed

  • Question

  • hi all,

    I am developing C# .net windows application 1.1, In this application i have two class. In form1 I am loading the datatable  from backend and binded it in to the datagrid. After this I am calling another form2 using one button click event in form1. i do some calculation in second form and pass one integer value to the first form PUBLIC FUNCTION and again in the first form PUBLIC FUNCTION from the value received i am selecting a row in the datagrid.  while this select statement i am INDEX OUT RANGE EXCEPTION

     

     

    Code sample

     

    Form1

    {

            datagrid.datasource = datatable;

    button click event

    {

      call  another form

    }

    }

    Form 2

    {

          //calculations

      calling public function of the Form1 with the integer value

    }

     

    Again in Form1

    {

       public void datagridselect(int i)

         {

             datagrid.select(i);

         }

    }

    while selecting i am getting INDEX OUT  RANGE EXCEPTION with the datagrid containing within the range values.

     

    I got the similar error for datatable.After coming back to form1 the table count become  0. I had overcome this one by declaring the datatable as PUBLIC STATIC

     

    Is this problem related to FOCUS?

     

    Plz help me to find out the solution

    Thanks in Advance

     

    With Regards

    B.Ananthvivek

     

     

     

    Tuesday, September 30, 2008 5:54 PM

Answers

  • Hi ANANTHVIVEK,

    I have read all of the posts in this thread. With the codes you provided, I may get what you want.

    As you said that the count of the data table is zero, it is reasonable for you didn’t pass the reference of the lobjFrm to the frmSearch. Instead, you define a new instance in frmSearch. When you defined a new instance of the lobjFrm, the dt member in the instance is null. Because it is not the instance which dt member is filled with data, the count of the dt member is zero. It also makes sense that when you change the dt to static, the count is never zero for static member is used by the class, not the instance of the class.

    For your requirement, I wrote the following code to prove you the correct way to achieve your goal.

    In my application, form1 is like your lobjFrm, form2 is like your frmSearch. When I click a button in form2, the application will search in the datagrid in form1, and select the correspond row.

    Code Snippet

    public partial class Form1 : Form

        {

            DataTable dt = new DataTable();

            public Form1()

            {

                InitializeComponent();

                this.Load += new EventHandler(Form1_Load);

            }

            void Form1_Load(object sender, EventArgs e)

            {

                CreateDataTable();

                DataBind();

            }

            public void FindJim(string name)

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    if (dt.Rows[i][1].ToString() == "Jim")

                        this.dataGrid1.Select(i);

                 }

            }

            private void DataBind()

            {

                this.dataGrid1.DataSource = this.dt;

            }

     

            private void CreateDataTable()

            {

                this.dt.Columns.Add("ID", typeof(int));

                this.dt.Columns.Add("Name", typeof(string));

                DataRow dr = dt.NewRow();

                dr["ID"] = 1;

                dr["Name"] = "Gog";

                dt.Rows.Add(dr);

                DataRow dr1 = dt.NewRow();

                dr1["ID"] = 2;

                dr1["Name"] = "Jim";

                dt.Rows.Add(dr1);

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                //Open serach dialog

                Form2 form = new Form2(this);//pass the reference of the main form

                form.ShowDialog();

            }

        }

     

     

    Code Snippet

        public partial class Form2 : Form

        {

            Form1 refMainForm;//store the reference of the mainform

            public Form2()

            {

                InitializeComponent();

            }

            public Form2(Form1 mainForm)

                : this()

            {

                refMainForm = mainForm;

            }

            private void button1_Click(object sender, EventArgs e)

            {

                refMainForm.FindJim("Jim");

            }

        }

     

    If you have further problems, please let me know.

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Monday, October 6, 2008 8:11 AM

All replies

  • How many rows are in your datagrid and what is the int value you are passing

     

    if you are passing say 4 and number of rows in your datagrid is 3 you will get out of range expeception

     

     

    Wednesday, October 1, 2008 2:02 PM
  • No i had tested with  the following code in the public function

     

    datagrid.select(0);

    still its throwing same exception (but values are there in datagrid)

     

    now i had passed that value (parameter)  to one event (by means of global variable) and selecting it, it works! . but I want to select it in the public function itself.

     

    With Regards

    B.Ananthvivek

    Wednesday, October 1, 2008 4:02 PM
  • it has majorly to do with when are you doign this select.

     

    Please sure that you have binded the data to grid.

     

    Can you put a messageBox to count number of rows before this function fire and vefiry if your dg has data boubd to it..

     

    Else send me the code so that I can simulate this sitituation.. it is not reproducible for me as of now

     

    Wednesday, October 1, 2008 4:59 PM
  • I had counted the rows in the datatable(which is binded to the grid) and its more than 500 after coming to the form1 again.

    I am sure, as per the error the number of records in the grid is  -1 but it has datatable(has rows 500) binded to it initially and I didn't make it null afterwards and also how to find out number of rows in the grid?

     

     

    I got similar error for datatable also(when i come back again to the first class the row count is 0) but i overcome that by declaring as PUBLIC STATIC. I couldn't do the same thing for datagrid as its windows generated. I had only changed modifier property to Public

     

     

    With Regards

    B.Ananthvivek

     

    Wednesday, October 1, 2008 5:36 PM
  • STATIC means that for instances the share dataset will be shared (kind of like application variable in wbe application)#

     

    are you sure you want to make this static ( I dont think you want all users to view same data do you ?)

     

    Now the problem is related to scoping . it is a fact that you dont have anything in you data set ..

     

    I need you code to see you scoping (or a snippet ) that can simul;ate the same for me. Once I am able to re craete the problem I can try fixing it

    Wednesday, October 1, 2008 6:10 PM
  • Hi Roy,

    Sorry for the late reply. This is the exact replica of my code as I couldn't share the exact one.

     

    All these things i am doing is to find out an specified item in the datagrid.In the second form i am getting item name and loop through the datatable in the first form and i select the given item in the grid.

     

    This is my code

    Code Snippet

     In form 1(frm)

    public static DataTable dt = new DataTable();

     

    private void Frm_Load(object sender, System.EventArgs e)

    {

       datagrid1.datasource = dt;

    }

     

    private void btnFind_Click(object sender, System.EventArgs e)

    {

    frmSearch lobjfrmFind = new frmSearch();

    lobjfrmFind.ShowDialog();

    }

     

    public void ProcFind (string par1,string par2)

    {

    for(int i =0; i< dt.Rows.Count;i++)

    {

    if (par1== dt.Rows[i][0].ToString().ToUpper())

    {

               datagrid,select(i);            // IndexOutOfRangeException occurs here

               break;

              }

    }

     

    In form 2(frmSearch)

    In the form 2 have search button

     

    Frm lobjFrm = new Frm();

     

    private void btnSearch_Click(object sender, System.EventArgs e)

    {

    lobjFrm.ProcFind(par1,par2);

    this.Close();

    }

     

     

     

    At first dt count in the Procfind function shown as 0. I had overcome that by declaring the datatable as Public static. Is it anything wrong with that?

     

    Wat's wrong with my code.I think its related with Scoping.

     

    All these things i am doing is to find out an specified item in the datagrid

     

    With Regards

    B.Ananthvivek

     

     

     

     

     

     

     

    Friday, October 3, 2008 3:18 PM
  • Create a new object

     

     

    try this before you bind data to grid

     

       

       

    private void Frm_Load(object sender, System.EventArgs e)

    {

     private DataGridView dataGridView1 = new DataGridView();

       datagrid.datasource = dt;

    }

     

     

    have a look here for the complete code..

    http://msdn.microsoft.com/en-us/library/fbk67b6z.aspx

     

    I think you are just creating a reference to datagrid class and not some object where your datatable can store its output

     

    thus there is nothing in the datagrid


     

     

    Friday, October 3, 2008 3:53 PM
  • I have tried that but it didn't work.  When I am selecting a row in  the grid from the windows supplied events of all the controls in the form,that  exception didn't come and the row gets selected.

     

    can u figure out wat may be the reason now

     

    With Regards

    B.Ananthvivek

     

     

    Friday, October 3, 2008 4:27 PM
  • Hi ANANTHVIVEK,

    I have read all of the posts in this thread. With the codes you provided, I may get what you want.

    As you said that the count of the data table is zero, it is reasonable for you didn’t pass the reference of the lobjFrm to the frmSearch. Instead, you define a new instance in frmSearch. When you defined a new instance of the lobjFrm, the dt member in the instance is null. Because it is not the instance which dt member is filled with data, the count of the dt member is zero. It also makes sense that when you change the dt to static, the count is never zero for static member is used by the class, not the instance of the class.

    For your requirement, I wrote the following code to prove you the correct way to achieve your goal.

    In my application, form1 is like your lobjFrm, form2 is like your frmSearch. When I click a button in form2, the application will search in the datagrid in form1, and select the correspond row.

    Code Snippet

    public partial class Form1 : Form

        {

            DataTable dt = new DataTable();

            public Form1()

            {

                InitializeComponent();

                this.Load += new EventHandler(Form1_Load);

            }

            void Form1_Load(object sender, EventArgs e)

            {

                CreateDataTable();

                DataBind();

            }

            public void FindJim(string name)

            {

                for (int i = 0; i < dt.Rows.Count; i++)

                {

                    if (dt.Rows[i][1].ToString() == "Jim")

                        this.dataGrid1.Select(i);

                 }

            }

            private void DataBind()

            {

                this.dataGrid1.DataSource = this.dt;

            }

     

            private void CreateDataTable()

            {

                this.dt.Columns.Add("ID", typeof(int));

                this.dt.Columns.Add("Name", typeof(string));

                DataRow dr = dt.NewRow();

                dr["ID"] = 1;

                dr["Name"] = "Gog";

                dt.Rows.Add(dr);

                DataRow dr1 = dt.NewRow();

                dr1["ID"] = 2;

                dr1["Name"] = "Jim";

                dt.Rows.Add(dr1);

            }

     

            private void button1_Click(object sender, EventArgs e)

            {

                //Open serach dialog

                Form2 form = new Form2(this);//pass the reference of the main form

                form.ShowDialog();

            }

        }

     

     

    Code Snippet

        public partial class Form2 : Form

        {

            Form1 refMainForm;//store the reference of the mainform

            public Form2()

            {

                InitializeComponent();

            }

            public Form2(Form1 mainForm)

                : this()

            {

                refMainForm = mainForm;

            }

            private void button1_Click(object sender, EventArgs e)

            {

                refMainForm.FindJim("Jim");

            }

        }

     

    If you have further problems, please let me know.

    Best Regards,

    Bruce Zhou

     

    Windows Forms General FAQs
    Windows Forms Data Controls and Databinding FAQs

     

    Monday, October 6, 2008 8:11 AM