0

I am making a database and I generated a cell click event if the user clicks on the cell it's data shows up in the text box so later on I can update the data if needed.

 private void PrisonerRecordDATAGRIDVIEW2_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        try
        {
            prisoner_id = Convert.ToInt32(PrisonerRecordDATAGRIDVIEW2.Rows[0].Cells[0].Value);
            txtbox_fn.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[1].Value.ToString();
            textBox_age.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[2].Value.ToString();
            textBox_crime.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[3].Value.ToString();
            textBox_punish.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[4].Value.ToString();
            txtbox_lockup.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[5].Value.ToString();
            textBox_yearfrom.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[6].Value.ToString();
            textBox_yearto.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[7].Value.ToString();
            txtbox_status.Text = PrisonerRecordDATAGRIDVIEW2.SelectedRows[0].Cells[8].Value.ToString();

        }
        catch (Exception f)
        {
            MessageBox.Show(f.Message,"Error",MessageBoxButtons.OK,MessageBoxIcon.Error);
        }
    }

Its throwing exception everytime I click on the cell like this

Index was out of range. Must be non negative and less than the size of the collection. Parameter name: Index

Also I want to know how do I reset primary key Id. Is there some sort of code to do that? I deleted all the rows from the data base but the primary key wasnt resetted. Thankyou for taking your time to read this. Any help would be great. :)

tuba2000
  • 1
  • 3
  • Don't worry about resetting the primary key; if you have some concern like "I don't want to see gaps in numbering" then you're using the primary key for the wrong thing. Use another column for your contiguous display numbering and leave the PK alone; it's purely for uniquely identifying a single row and the db doesn't care about gaps. Leave auto numbering/management of the PK to the db. Trying to micromanage it yourself will give trouble – Caius Jard Jun 04 '20 at 09:16
  • See [what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i](https://stackoverflow.com/questions/20940979/what-is-an-indexoutofrangeexception-argumentoutofrangeexception-and-how-do-i-f)... – Caius Jard Jun 04 '20 at 09:18
  • THanks @Caius I will leave primary key as it is andd find another alternative. What about the cell click event. Do you have any solution for that? – tuba2000 Jun 04 '20 at 09:20
  • Well, simply put you're trying to access a collection by an index that doesn't exist. If you had an array of size 5 and you tried to get the tenth element you'd get an indexoutofbounds. If you clear your grid and then try and get the 0th (first) row it will fail because there is no first row. Tbh, you're also using datagridview incorrectly - your data should be stored in something like a datatable object; the grid is just for showing it – Caius Jard Jun 04 '20 at 09:22

2 Answers2

0

It would be better to use Bindings and IsSynchronizedWithCurrentItem="True". Here is a good example https://learn.microsoft.com/de-de/dotnet/api/system.windows.controls.primitives.selector.issynchronizedwithcurrentitem?view=netcore-3.1

Niklas
  • 172
  • 2
  • 8
  • Thankyou i will find another alternative for primarykey. My main problem is of the cell click event and the exception it is generating. – tuba2000 Jun 04 '20 at 09:22
  • On which line does it crash? Set a breakpoint on prisoner_id and step through. Then you will see where and why exactly it throws the exception. – Niklas Jun 04 '20 at 09:28
  • I dont think it crashes. It just throws exception when i click on any cell. – tuba2000 Jun 04 '20 at 09:35
  • Once you delete the try catch, it will ;) – Niklas Jun 04 '20 at 10:35
0

The block of data you have posted can really only give an IndexOutOfBounds in one of two ways:

  • There is no row in the datagridview, meaning .Rows[0] crashes
  • There aren't at least 9 columns in the datagridview, meaning one of the .Cells[x] crashes because x is equal to the number of columns in the datagridview

"Crashes" and "throws an Exception" mean the same. Just because you catch an exception doesn't mean the code didn't throw it

Caius Jard
  • 72,509
  • 5
  • 49
  • 80