rafalefighter
rafalefighter

Reputation: 734

Pass all datagridview selected row values to second form using context menu

in my application i have a datagridview with many rows and many users update it regularly.i want to add a context menu to it that enable to user to right click and select edit from the context menu and then it should open a second form with all that selected row values.

i use following code

private void toolStripMenuItem2_Click(object sender, EventArgs e)
{                
    foreach (DataGridViewCell oneCell in dataGridView1.SelectedCells)
    {

        if (oneCell.Selected)
        {

            int editloannumber = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[0].Value.ToString());
            int editlid = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[8].Value.ToString());
            string editloantype = dataGridView1.Rows[oneCell.RowIndex].Cells[1].Value.ToString();
            string editneworsecond = dataGridView1.Rows[oneCell.RowIndex].Cells[2].Value.ToString();
            string editpurpose = dataGridView1.Rows[oneCell.RowIndex].Cells[11].Value.ToString();
            string editstatus = dataGridView1.Rows[oneCell.RowIndex].Cells[3].Value.ToString();
            string editcomments = dataGridView1.Rows[oneCell.RowIndex].Cells[7].Value.ToString();
            string editretail = dataGridView1.Rows[oneCell.RowIndex].Cells[12].Value.ToString();


            wartif_UW.editform e2 = new wartif_UW.editform(editloannumber, editloantype, editneworsecond, editpurpose, editstatus, editcomments, editretail,editlid);
             e2.ShowDialog();
         }
     }
}

i set the datagridview selection mode to fullrowselect and when i run the code it load my second form but not only once it looping back several times because of the for each loop. how can i stop this ?

when i set datagridview selection mode to cellselect it working perfectly fine. but i want to select all the row

to write click function i use following code

private void dataGridView1_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                var hti = dataGridView1.HitTest(e.X, e.Y);
                if (hti.RowIndex >= 0)
                {
                    dataGridView1.ClearSelection();
                    dataGridView1.CurrentCell =dataGridView1[hti.ColumnIndex,hti.RowIndex];
                }
            }
        }

Upvotes: 0

Views: 929

Answers (1)

Matt Ko
Matt Ko

Reputation: 979

Let's take a look at your for-loop.
If you have the full row selection mode, then your cells are always going to be selected.
So the if-statement really doesn't do anything.
Plus, the RowIndex of your selected cells will always be the same.
So why don't you substitute the for-loop for something like this:

if(dataGridView1.SelectedCells.Count > 0)
{
    var oneCell = dataGridView1[0];
    int editloannumber = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[0].Value.ToString());
    int editlid = int.Parse(dataGridView1.Rows[oneCell.RowIndex].Cells[8].Value.ToString());
    string editloantype = dataGridView1.Rows[oneCell.RowIndex].Cells[1].Value.ToString();
    string editneworsecond = dataGridView1.Rows[oneCell.RowIndex].Cells[2].Value.ToString();
    string editpurpose = dataGridView1.Rows[oneCell.RowIndex].Cells[11].Value.ToString();
    string editstatus = dataGridView1.Rows[oneCell.RowIndex].Cells[3].Value.ToString();
    string editcomments = dataGridView1.Rows[oneCell.RowIndex].Cells[7].Value.ToString();
    string editretail = dataGridView1.Rows[oneCell.RowIndex].Cells[12].Value.ToString();

    wartif_UW.editform e2 = new wartif_UW.editform(editloannumber, editloantype, editneworsecond, editpurpose, editstatus, editcomments, editretail,editlid);
    e2.ShowDialog();
 }

Upvotes: 0

Related Questions