'C# Windows Forms Data Grid View Cell Click unsure of data types

I'm relatively new to coding so I wasn't sure how to word my question very well but I am working on a database and I am not sure what the correct lines of code to use are for each of my datatypes (I have temporarily added the line of code that would be used for a string just to fill in the space to create the error). I have included my code and a screenshot of the errors. Hoping someone understands what I mean and is able to help me out. Thank you.

My Code:

My code (probably not the best)Error messages currently displaying

I tried creating a question previously but I was in a rush as I was in class and it wasn't very clear at all so I hope this is somewhat better and someone is able to properly understand it.

I tried changing the "ToString" section to "ToDateTime" hoping maybe it was a simple fix but still not luck.

Edit: Added my code as text

private void DgvPayment_CellClick(object sender, DataGridViewCellEventArgs e) {

        if (e.RowIndex >= 0)

        {
            DataGridViewRow row = this.DgvPayment.Rows[e.RowIndex];

            selectedPaymentID = (int)row.Cells[0].Value;
            DatePaid = row.Cells[1].Value.ToString();
            TxtAmountPaid.Text = row.Cells[2].Value.ToString();
            TxtPaymentDescription.Text = row.Cells[3].Value.ToString();
            BookingID = (int)row.Cells[4].Value();
            CustomerID = (int)row.Cells[5].Value();
        }


Solution 1:[1]

The errors you show are fairly clear. The first error the compiler is complaining about on the line…

DatePaid = row.Cells[1].Value.ToString();

The compiler is complaining because the compiler does NOT know what DatePaid is. Where is DatePaid defined and what type is DatePaid? The name of the variable implies the variable is a DateTime object, however, this is unknown and defining the variable as a string should at least get rid of the error.

The compiler is complaining about BookingID and CustomerID for the same reason. It does not know what BookingID or CustomerID is... so you need to define those variables.

And lastly, the compiler is complaining about a “non-invocable” member is being used like a method. This error is coming from…

Value()

In the line of code…

(int)row.Cells[4].Value();

Value is a property… not a method, so you need to remove the “()” as it is shown on the second line of code.…

selectedPaymentID = (int)row.Cells[0].Value;

Something like below should get rid of the current errors you have shown…

int selectedPaymentID;
string DatePaid;
int BookingID;
int CustomerID;

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e) {
  if (e.RowIndex >= 0) {
    DataGridViewRow row = this.DgvPayment.Rows[e.RowIndex];
    selectedPaymentID = (int)row.Cells[0].Value;
    DatePaid = row.Cells[1].Value.ToString();
    TxtAmountPaid.Text = row.Cells[2].Value.ToString();
    TxtPaymentDescription.Text = row.Cells[3].Value.ToString();
    BookingID = (int)row.Cells[4].Value;
    CustomerID = (int)row.Cells[5].Value;
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JohnG