Gabor Virag
Gabor Virag

Reputation: 23

DateTimePicker value from DataGridView

I have some textboxes and a datetimepicker. There is also a datagridview on it. I upload data to a mysql table. I want to modify the data via textboxes and datetimepicker, but I get error:

System.ArgumentOutOfRangeException

on the dateTimePicker.Value row:

private void dataGridView1_SelectionChanged(object sender, EventArgs e) {
  dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[1].Value);

  idoTextBox.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
  pkcomboBox.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
  minositesbox.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
  esetextBox.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
  fotextBox.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
  megjegyzestextBox.Text = dataGridView1.SelectedRows[0].Cells[7].Value.ToString();
}

What did I do wrong?

Upvotes: 2

Views: 7033

Answers (2)

LarsTech
LarsTech

Reputation: 81610

You probably don't have a row selected, just a cell.

Make sure you have the correct SelectionMode property set:

dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;

Also, make sure you have selected rows:

if (dataGridView1.SelectedRows.Count > 0) {
  dateTimePicker1.Value = Convert.ToDateTime(dataGridView1.SelectedRows[0].Cells[1].Value);
  // ...
}

Upvotes: 1

Peroxy
Peroxy

Reputation: 646

You are getting ArgumentOutOfRangeException which means that you are most likely trying to access a value that does not exist at your specified index. You are probably doing this while calling

dataGridView1.SelectedRows[0].Cells[7].Value.ToString();

You are using a cell index of 7 right here, is it possible that Cells[7] does not exist?

Upvotes: 1

Related Questions