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. :)