none
select datagridview cell value by selecting checkbox

    Question

  • I want to put a cell value into an array if a checkbox is selected.  This is my code:

    private void DataGridview_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
            {
                DataGridView DataGridView1 = new DataGridView();
    
                DataGridViewCheckBoxColumn Col1= new DataGridViewCheckBoxColumn();
                Col1.Name = "Col1";
                DataGridView1.Columns.Add(Col1);
                DataGridViewCheckBoxColumn Col2 = new DataGridViewCheckBoxColumn();
                Col2.Name = "Col2";
                DataGridView1.Columns.Add(Col2);
                DataGridViewTextBoxColumn Col3 = new DataGridViewTextBoxColumn();
                Col3.Name = "Col3";
                DataGridView1.Columns.Add(Col3);
    
                var rowIndex = e.Cell.ColumnIndex;
                var colIndex = e.Cell.ColumnIndex;
                int Array1Length = Array1.Length;
                int Array2Length = Array2.Length;
                if(e.Cell.Selected == true)
                {
                    if (colIndex == 0)
                    {
                        *Array1[delLen] = DataGridView1.Rows[rowIndex].Cells["Col3"].Value.ToString();
                    }
                    else if(colIndex == 1)
                    {
                        Array2[upLen] = DataGridView1.Rows[rowIndex].Cells["Col3"].Value.ToString();
                    }
                }
                else
                {
                    for(int x = 0; x < Array1.Length; x++)
                    {
                        if(Array1[x] == DataGridView1.Rows[rowIndex].Cells["Col3"].Value.ToString())
                        {
                            Array1.ElementAt(x).Remove(0);
                        }
                    }
                    for (int x = 0; x < Aray2.Length; x++)
                    {
                        if (Array2[x] == DataGridView1.Rows[rowIndex].Cells["Col3*"].Value.ToString())
                        {
                            Array2.ElementAt(x).Remove(0);
                        }
                    }
                }
    
            }

    I am getting Object reference not set to an instance of an object on the line begging with *.  Don't know what object its referring to.  Any help guidance appreciated.

    Tuesday, April 25, 2017 10:08 PM

Answers

All replies

  • I originally has the array as non-static in the Load() event.  I now have them as static arrays in the class.  

    this is my code at the moment

    DataGridView datagridview1 = new DataGridView();
                this.Controls.Add(dataGridView1);
                DataGridViewCheckBoxColumn Column1 = new DataGridViewCheckBoxColumn();
                
                DataGridViewCheckBoxColumn Column2 = new DataGridViewCheckBoxColumn();
    
                DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
    
                int rowIndex = dataGridView1.CurrentCell.RowIndex;
                int colIndex = dataGridView1.CurrentCell.ColumnIndex;
    
                
    
                if (dataGridView1.CurrentCell.Selected == true)
                {
                    if (colIndex == 0)
                    {
                        
                        if(Array1.Length == 0)
                        {
                            *Array1[Array1Length] = "1";
                            //MessageBox.Show(Array1Length.ToString());
                        }
                    }
                    else if (colIndex == 1)
                    {
                        //Array2[Array2Length] = dataGridView1.Rows[rowIndex].Cells["Column3"].Value.ToString();
                        //MessageBox.Show(Array2[Array2Length]);
                    }
                }
                else
                {
    
                }
    The line with the * is giving me System.OutOfRangeException.  If the array length is 0, how can it be out or range?

     
    Wednesday, April 26, 2017 9:49 AM
  • this is all the code

     public partial class Form1 : Form
        {
            private static string[] Array1 = new string[0];
            private static string[] Array2 = new string[0];
            static int Array1Length = Array1.Length;
            static int Array2Length = Array2.Length;
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataGridViewCheckBoxColumn Column1 = new DataGridViewCheckBoxColumn();
                Column1.Name = "Column";
                Column1.Selected = false;
                dataGridView1.Columns.Add(Column1);
    
                DataGridViewCheckBoxColumn Column2 = new DataGridViewCheckBoxColumn();
                Column2.Name = "Column2";
                Column2.Selected = false;
                dataGridView1.Columns.Add(Column2);
    
                DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
                Column3.Name = "Column3";
                dataGridView1.Columns.Add(Column3);
                dataGridView1.Rows.Add();
    
                dataGridView1.Rows[0].Cells["Column3"].Value = "Test";
    
                
             
    
            }
    
            private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
            {
                DataGridView datagridview1 = new DataGridView();
                this.Controls.Add(dataGridView1);
                DataGridViewCheckBoxColumn Column1 = new DataGridViewCheckBoxColumn();
                
                DataGridViewCheckBoxColumn Column2 = new DataGridViewCheckBoxColumn();
    
                DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
    
                int rowIndex = dataGridView1.CurrentCell.RowIndex;
                int colIndex = dataGridView1.CurrentCell.ColumnIndex;
    
                
    
                if (dataGridView1.CurrentCell.Selected == true)
                {
                    if (colIndex == 0)
                    {
    
    
                        *//Array1[Array1Length] = dataGridView1.Rows[colIndex].Cells["Column3"].Value.ToString();
                            //essageBox.Show(Array1Length);
                        
                    }
                    else if (colIndex == 1)
                    {
                        //Array2[Array2Length] = "1";
                        //MessageBox.Show(Array2[Array2Length]);
                    }
                }
                else
                {
    
                }
            }
        }
    * denotes the line giving Out of range exception for Array1.

    Wednesday, April 26, 2017 10:33 AM
  • I have changed the arrays to arrayLists.  Getting System.ArgeumentOutOfRangeException at line *

    private static ArrayList Array1 = new ArrayList();
            private static ArrayList Array2 = new ArrayList();
            int Array1Length = Array1.Count;
            int Array2Length = Array2.Count;
            public Form1()
            {
                InitializeComponent();
                
            }
    
            private void Form1_Load(object sender, EventArgs e)
            {
                DataGridViewCheckBoxColumn Column1 = new DataGridViewCheckBoxColumn();
                Column1.Name = "Column";
                Column1.Selected = false;
                dataGridView1.Columns.Add(Column1);
    
                DataGridViewCheckBoxColumn Column2 = new DataGridViewCheckBoxColumn();
                Column2.Name = "Column2";
                Column2.Selected = false;
                dataGridView1.Columns.Add(Column2);
    
                DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
                Column3.Name = "Column3";
                dataGridView1.Columns.Add(Column3);
                dataGridView1.Rows.Add();
    
                dataGridView1.Rows[0].Cells["Column3"].Value = "Test";
    
                
             
    
            }
    
            private void dataGridView1_CellStateChanged(object sender, DataGridViewCellStateChangedEventArgs e)
            {
                if (dataGridView1.CurrentCell.Selected == true)
                {
                    DataGridView datagridview1 = new DataGridView();
                    this.Controls.Add(dataGridView1);
                    DataGridViewCheckBoxColumn Column1 = new DataGridViewCheckBoxColumn();
                
                    DataGridViewCheckBoxColumn Column2 = new DataGridViewCheckBoxColumn();
    
                    DataGridViewTextBoxColumn Column3 = new DataGridViewTextBoxColumn();
    
                    int rowIndex = dataGridView1.CurrentCell.RowIndex;
                    int colIndex = dataGridView1.CurrentCell.ColumnIndex;
    
               
                    if (colIndex == 0)
                    {
    
    
                        Array1.Add(datagridview1.Rows[rowIndex].Cells["Column3"].Value.ToString()); *
                        
                        
                    }
                    else if (colIndex == 1)
                    {
                        Array2.Add("2");
                        //MessageBox.Show(Array2[Array2Length]);
                    }
                }
                else
                {
    
                }
            }
    Appreciate any help with this.

    Wednesday, April 26, 2017 11:48 AM
  • I'm trying to check if a checkbox is checked using

    Convert.ToBoolean(row.CurrentCell.Value) == true

    but getting System.InvalidCastException.

    this as well 

    Convert.ToBoolean(e.Cell.Value) == true

    Wednesday, April 26, 2017 1:44 PM
  • I'm using

    bool checkBoxStatus = Convert.ToBoolean(DataGridView.CurrentCell.EditedFormattedValue);

    to get the bool value of a checkbox in a CellContectClicked event.   the checkbox is ticked but its returning checkBoxStatus as false.   Does anyone know why?


    Wednesday, April 26, 2017 4:24 PM
  • Is there any reason why or how datagridview CellContentClick and CellValueChanged events on one form would affect another form?  I have 3 forms, depending on user choice, one form leads to another which leads to another.  The afore-mentioned events are only on a datagridview on the 3rd form(they display a messagebox, yet if I select a checkbox on form 2 and press a button to open form 3, the messagebox appears.  Has anyone ever seen this behaviour before?
    Wednesday, April 26, 2017 6:59 PM
  • Behaviour seems to have rectified itself. No idea what the issue was.
    Wednesday, April 26, 2017 9:17 PM