I am currently working on a small project. How it works is a passenger must book where he/she wants to sit on plane. Using a DataGridView he/she can pick a seat except in the ilse.

The DataGridView has 7 columns and 5 rows. The ilse is occupying the whole 3rd row so that they cant pick a seat there.

I want to add a passengers details into a cell on the DataGridView and display it in a list box afterwards. I am a bit new to C# so my code will not be working perfectly so please be patient.

Questions: HOW do I add the Passenger to the array and HOW do I change the row and column color of the selected cell after the passenger has been added??

I have the following code:

public Form1()
    {
        InitializeComponent();

        dgvPlane.Rows.Add(5);
        dgvPlane.Rows[0].HeaderCell.Value = "1";
        dgvPlane.Rows[1].HeaderCell.Value = "2";
        dgvPlane.Rows[2].HeaderCell.Value = "3";
        dgvPlane.Rows[3].HeaderCell.Value = "4";
        dgvPlane.Rows[4].HeaderCell.Value = "5";

        for (int k = 0; k < dgvPlane.ColumnCount; k++)
        {
            dgvPlane[k, 2].Style.BackColor = Color.DarkSlateGray;
        }

        dgvPlane.ClearSelection();
    }




    //ADDING THE PASSENGER INTO THE DATAGRIDVIEW

    private void btnAddPassenger_Click(object sender, EventArgs e)
    {
        Passenger _Passenger = new Passenger();

        _Passenger.Details = tbName.Text;
        _Passenger.wasteSizeCm = Convert.ToDouble(nudWaistSize.Value);
        _Passenger.BigToeSizeMm = Convert.ToDouble(nudToeSize.Value);

        MessageBox.Show("Passenger Added");

        //comboBox1 is the Columns in a drop down for eg. "1", "2", "3", etc. 
        //comboBox2 is the Rows in a drop down for eg. "A", "B", "C", etc


        dgvPlane.Rows[comboBox2].Cells[comboBox1].Value = _Passenger;

        for (int k = 0; k < dgvPlane.ColumnCount; k++)
        {
            //Change the color of the added passenger's cell to indicate that the seat is now taken
            //NOTE: [0,0] is there because I'm not sure how to refer to the row and column values
            dgvPlane[0,0].Style.BackColor = Color.Goldenrod;
        }

        //ADDING THE PASSENGER TO THE ARRAY
        PassengerList[comboBox1.Value, comboBox2.Value] = _Passenger;

    }

And this is simply my class:

namespace WindowsFormsApplication3
{
    class Passenger
    {
        private string mDetails;
        private double mWasteSizeCm;
        private double mBigToeSizeMm;

        public Passenger()
        {
            mDetails = "";
            mWasteSizeCm = 0;
            mBigToeSizeMm = 0;
        }

        public string Details
        {
            get { return mDetails; }
            set { mDetails = value; }
        }

        public double wasteSizeCm
        {
            get { return mWasteSizeCm; }
            set { mWasteSizeCm = value; }
        }

        public double BigToeSizeMm
        {
            get { return mBigToeSizeMm; }
            set { mBigToeSizeMm = value; }
        }
    }
}

ANY help on this will be IMMENSELY appreciated.

J

You could try DGV[col, row].Style.BackColor = Color.Orange;
if you want to change the backgroundcolor of column col and row row of the DataGrid. Will have a look at your passenger problem.

A very nice article about databinding object to DGVs is this one
It also happens to use example of plane and passengers.
Hope it helps.

Okay cool. I have come this far. Which adds a passenger to the list, changes his position a vacant and also shows who the position belongs to.

BUT... when I click on the DataGridView to update a passenger's info it gives me the following error:

InvalidArgument=Value of '-1' is not valid for 'index'.

I have no idea what this means... :(

And also its not picking up the information of each passenger when I click on their positions on the DataGridView so that I will be able to update it.

Any suggestions?

I have the following code:

private void btnAddPassenger_Click(object sender, EventArgs e)
        {
            Passenger _Passenger = new Passenger();
            int selectedIndexCol = comboBox1.SelectedIndex;
            int selectedIndexRow = 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 = 1; k < dgvPlane.ColumnCount; k++)
            {
                for (int t = 1; t < dgvPlane.RowCount; t++)
                {
                    if (comboBox2.Items[comboBox2.SelectedIndex].ToString() == "1")
                    {
                        selectedIndexRow = 0;
                        dgvPlane[selectedIndexCol, selectedIndexRow].Style.BackColor = Color.Goldenrod;
                    }
                    else if (comboBox2.Items[comboBox2.SelectedIndex].ToString() == "2")
                    {
                        selectedIndexRow = 1;
                        dgvPlane[selectedIndexCol, selectedIndexRow].Style.BackColor = Color.Goldenrod;
                    }
                    else if (comboBox2.Items[comboBox2.SelectedIndex].ToString() == "4")
                    {
                        selectedIndexRow = 3;
                        dgvPlane[selectedIndexCol, selectedIndexRow].Style.BackColor = Color.Goldenrod;
                    }
                    else if (comboBox2.Items[comboBox2.SelectedIndex].ToString() == "5")
                    {
                        selectedIndexRow = 4;
                        dgvPlane[selectedIndexCol, selectedIndexRow].Style.BackColor = Color.Goldenrod;
                    }
                }
            }

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

            //OUTPUT THE LIST
            lbOut.Items.Add(_Passenger.Details + " " + comboBox1.Items[comboBox1.SelectedIndex].ToString() + "" + comboBox2.Items[comboBox2.SelectedIndex].ToString());
        }

Could you specify the line and the code where theerror occurs?

if (comboBox2.Items[comboBox2.SelectedIndex].ToString() == "1") Do you mean this line? Could you please paste the exception you are getting?

If no item in a combobox is selected, combobox.SelectedIndex will be -1. The Items collection of a combobox is zero based, hence your error.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.