Get selected row number, randomly return 0

I have a DGV related context menu bar. When a cell is clicked in the DGV, it will open the context menu bar, and I can click "plotStripMenuItem" to open the form (formPlot), which basically reads the data from the text file and the graphs of the graphs and calculates some statistics that will (after as formPlot is closed) to the previously pressed DGV line.

to do this, I use "int clickedRow" to save the number of the clicked line before opening formPlot, and use this to update the DGV after I closed formPlot.

private void plotToolStripMenuItem_Click(object sender, EventArgs e) { //get the row number which was clicked string strClickedRow = DGVLeft.CurrentCellAddress.Y.ToString(); int clickedRow = Convert.ToInt16(strClickedRow); FormPlot formPlot = new FormPlot(strLot, pathLD); formPlot.ShowDialog(); DGVLeft.Rows[clickedRow].Cells[0].Value = formPlot.columnA; DGVLeft.Rows[clickedRow].Cells[1].Value = formPlot.ColumnB; DGVLeft.Rows[clickedRow].Cells[2].Value = formPlot.columnC; DGVLeft.Rows[clickedRow].Cells[3].Value = formPlot.columnD; } 

In most cases, this works as expected. But I noticed that sometimes it does not update the DGV cells that were clicked before I opened formPlot. Instead, he updated line number 0!

eg. I hit row 7, but after returning from formPlot, the value of DGV row 0 was updated. Not the value of the string DGV number 7.

Now it seems to me that line 0 will be updated as I clicked line 7 and saved it in a variable.

Am I missing something?

+4
source share
1 answer

This may or may not solve your problem, but why do you convert the line number to a string and vice versa?

  //get the row number which was clicked string strClickedRow = DGVLeft.CurrentCellAddress.Y.ToString(); int clickedRow = Convert.ToInt16(strClickedRow); 

it should be

  //get the row number which was clicked int clickedRow = DGVLeft.CurrentCellAddress.Y; 
+1
source

Source: https://habr.com/ru/post/1445690/


All Articles