JBTGE
JBTGE

Reputation: 15

comboBox list values

quick question. I have a comboBox with a list of the following items with values:

line 1: 1
line 2: 2
line 3: 4
line 4: 5

I have a total of 5 rows in a DataGridView but I want to ignore the 3rd row. Now when I am using the following for loop it is acting as if line 3 is row 3 (Where I want it to be row 4), and line 4 is row 4 (And I want to be row 5) because of their positions in the list.

Is there a way that I can change this perhaps?

The problem occurs in the for-loop where I am changing the colour of the cell after the person's detail has been added.

here is my code:

    Passenger _Passenger = new Passenger();
        int selectedIndexRow = comboBox1.SelectedIndex;
        int selectedIndexCol = comboBox2.SelectedIndex;

        //ADDING DETAILS OF NEWLY ADDED PASSENGER
        _Passenger.Details = tbName.Text;
        _Passenger.wasteSizeCm = Convert.ToDouble(nudWaistSize.Value);
        _Passenger.BigToeSizeMm = Convert.ToDouble(nudToeSize.Value);

        MessageBox.Show("Passenger Added");

        //CHANGING COLOR OF THE CELL
        for (int k = 0; k < dgvPlane.ColumnCount; k++)
        {
            for (int t = 1; t < dgvPlane.RowCount; t++)
            {
                dgvPlane[selectedIndexRow, selectedIndexCol].Style.BackColor = Color.Goldenrod;
            }
        }


        //ADDING THE PASSENGER TO THE ARRAY
        //PassengerList[selectedIndexCol, selectedIndexRow] = _Passenger;



    }

Upvotes: 0

Views: 102

Answers (1)

Jeffrey Wieder
Jeffrey Wieder

Reputation: 2376

Remember, indexes start at 0, line 1 in your combo is index 0.

That said the easiest way to get the proper Row and Column indexes is to not use strings in your combo.

Populate your combo:

    public Form1()
    {
        InitializeComponent();
        comboBox1.Items.AddRange(new object[] { 1, 2, 4, 5 });
    }

Retrieve the numeric value from the combo:

        int selectedIndexRow = (int)comboBox1.SelectedItem;

Also, Why are you looping and using a set Row Index, and Column Index?

The way you are doing this you are just setting the BackColor of a single cell many times over.

You don't need to loop, just need:

        dgvPlane[selectedIndexRow, selectedIndexCol].Style.BackColor = Color.Goldenrod;

Upvotes: 1

Related Questions