0

I've been looking for a way. To Edit, update and delete my data using a contextmenustrip.

Basically what I want is for my contextmenustrip to be able to interact with Datagridview, the problem is whenever I set the code for click event. it shows an error, I am still new in c# so I am probably not figuring something out. this is my code.

private void DgAddnew_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show("You can now Edit this data, Choose Update Button To populate, Thank you!", "Edit Data,", MessageBoxButtons.OK, MessageBoxIcon.Information);
    int index = e.RowIndex;// get the Row Index

    DataGridViewRow selectedRow = dgAddnew.Rows[index];

    txtNAME.Text = selectedRow.Cells[1].Value.ToString();
    txtADDRESS.Text = selectedRow.Cells[2].Value.ToString();
    txtCELLPHONE.Text = selectedRow.Cells[3].Value.ToString();
    txtCODE.Text = selectedRow.Cells[4].Value.ToString();
    cmbTYPE.Text = selectedRow.Cells[5].Value.ToString();
    cmbCOLOR.Text = selectedRow.Cells[6].Value.ToString();
    cmbHEELS.Text = selectedRow.Cells[7].Value.ToString();
    cmbPAYMENT.Text = selectedRow.Cells[8].Value.ToString();
    cmbPRICE.Text = selectedRow.Cells[9].Value.ToString();
    cmbRESERVE.Text = selectedRow.Cells[10].Value.ToString();
    cmbBONDFEE.Text = selectedRow.Cells[11].Value.ToString();
    dpDATEOUT.Text = selectedRow.Cells[12].Value.ToString();
    dpDATERETURN.Text = selectedRow.Cells[13].Value.ToString();
}

this work in datagridview. but when i add it on contextmenu all this item doesn't populate, and i get a row index error.

private void EDITToolStripMenuItem_Click_1(object sender, EventArgs e)
{
    MessageBox.Show("You can now Edit this data, Choose Update Button To populate, Thank you!", "Edit Data,", MessageBoxButtons.OK, MessageBoxIcon.Information);
    int index = e.RowIndex;// get the Row Index

    DataGridViewRow selectedRow = dgAddnew.Rows[index];

    txtNAME.Text = selectedRow.Cells[1].Value.ToString();
    txtADDRESS.Text = selectedRow.Cells[2].Value.ToString();
    txtCELLPHONE.Text = selectedRow.Cells[3].Value.ToString();
    txtCODE.Text = selectedRow.Cells[4].Value.ToString();
    cmbTYPE.Text = selectedRow.Cells[5].Value.ToString();
    cmbCOLOR.Text = selectedRow.Cells[6].Value.ToString();
    cmbHEELS.Text = selectedRow.Cells[7].Value.ToString();
    cmbPAYMENT.Text = selectedRow.Cells[8].Value.ToString();
    cmbPRICE.Text = selectedRow.Cells[9].Value.ToString();
    cmbRESERVE.Text = selectedRow.Cells[10].Value.ToString();
    cmbBONDFEE.Text = selectedRow.Cells[11].Value.ToString();
    dpDATEOUT.Text = selectedRow.Cells[12].Value.ToString();
    dpDATERETURN.Text = selectedRow.Cells[13].Value.ToString();
}

the part it has "e.RowIndex" Gives error. Is there A way I can declare a global declaration or store a rowIndex to a container and be able to interact with the contextmenustrip,??

I've been figuring it for days and it still doesn't work. Its my 12days of learning c# as my first language and it frustrates me that I still suck so bad. pls help me. Thanks.

Airn5475
  • 2,452
  • 29
  • 51
  • `EventArgs` doesn't have a property called `RowIndex`. Instead I'd just grab the selected row using `var selectedRow = dgAddnew.SelectedRows[0]`. This code presumes you're not attempting to do things when selecting more than one row. I would check `SelectedRows.Count > 0` first to be safe. – Zer0 Mar 10 '20 at 18:08
  • thank you for giving me idea, sorry my Programming Logic is not that really good yet since I am still starting out, and don't know how to approach it. You gave me A lot of ideas for this comment I will try to analyze it to be able to work on the features I want. Thank you very much zero! – Akagami Shanks Mar 10 '20 at 18:44

0 Answers0