Winform Datagridview cellclick error - Index was out of range

I have a simple data grid that contains a list of records from a SQLSERVER table. The data network fills up without any problem. I want to click on a row and load the relevant data into the textboxes I created next to it. So far, everything is so simple.

Here is my code for the cellclick event

    private void dataGridVieworderitems_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        {
            //try
            //{
                //if (dataGridVieworderitems.SelectedRows.Count > 0) // make sure user select at least 1 row 
                {
                    string jobId = dataGridVieworderitems.SelectedRows[0].Cells[0].Value + string.Empty;
                    string standpack = dataGridVieworderitems.SelectedRows[0].Cells[1].Value + string.Empty;
                    string description = dataGridVieworderitems.SelectedRows[0].Cells[2].Value + string.Empty;
                    string price = dataGridVieworderitems.SelectedRows[0].Cells[3].Value + string.Empty;
                    string itemType = dataGridVieworderitems.SelectedRows[0].Cells[4].Value + string.Empty;
                    string notes = dataGridVieworderitems.SelectedRows[0].Cells[5].Value + string.Empty;

                    labelidvalue.Text = jobId;
                    labelstandpackvalue.Text = standpack;
                    labeldescriptionvalue.Text = description;
                    textBoxprice.Text = price;
                    labeltypevalue.Text = itemType;
                    textBoxnotes.Text = notes;
                }
            //}
            //catch (Exception)
            //{
            //    MessageBox.Show("something went wrong!");
            //}
        }
    }

      

I deliberately commented out the If statement and tried to block the block to generate an error. I am getting the following error.

The System.ArgumentOutOfRangeException was unhandled. HResult = -2146233086 Message = Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index ParamName = index .......

It is WINFORM and C # .. There is data in datagrid view. but it says the index is out of range. can anyone point me in the right direction?

This is how I fill the grid

    public DataTable GetStaffCurrentOrderItems()
    {
        try
        {
            DataTable dtstaffcurrentorderlist = new DataTable();

            string connString = System.Configuration.ConfigurationManager.ConnectionStrings["nav"].ConnectionString;
            using (SqlConnection con = new SqlConnection(connString))
            {
                using (SqlCommand cmd = new SqlCommand("select [ID],standpack as [Item], item_description as [Description], '$'+convert(varchar(5),price) as Price,item_type as [Item Type],notes as [Notes] from tbl_staff_orders_items", con))
                {
                    if (con.State == ConnectionState.Open)
                    {
                        con.Close();
                    }
                    con.Open();
                    SqlDataReader reader = cmd.ExecuteReader();
                    dtstaffcurrentorderlist.Load(reader);
                }
                con.Close();
            }
            return dtstaffcurrentorderlist;
        }
        catch (Exception)
        {
            return null;
        }
    }

      

+3


source to share


3 answers


Make a check inside an event handler cellClick

to handle nulls like this

if (dataGridVieworderitems.CurrentCell == null ||
    dataGridVieworderitems.CurrentCell.Value == null ||
    e.RowIndex == -1) return;

      



This will fix the problem as it checks for all possible zero values ​​when the cell is clicked GridView

. The other part will give you data if there is anything other than zero.

Hope it helps!

+3


source


Index was out of range

      

This means the index was not found in your datagrid cell.



Please check the rows of your datagrid if index exists

the same with column

too.

+2


source


Edited: Ha, got the exact source of the problem:
The SelectionMode property must be set to FullRowSelect for the SelectedRows property to be populated with the selected rows.

Otherwise, you can use the following options:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            string jobId = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();

        }

      

or

private void dataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            DataGridViewRow selectedRow = dataGridView1.Rows[dataGridView1.SelectedCells[0].RowIndex];
            string jobId = selectedRow.Cells[0].Value.ToString();
        }

      

+1


source







All Articles