0

I have a dataGridView1_CellContentClick that checkes if a specifc cell is clicked.

When it is clicked it creates a new Form.

On this form is a dateTimePicker and a Button.

When the button is clicked, i want the value of the dateTimePicker to be added to the correct row and cell in the dataGridView.

This is what i have so far.

   private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
 if (e.ColumnIndex == dataGridView1.Columns[16].Index && e.RowIndex >= 0)
        {
                int numberRow = Convert.ToInt32(e.RowIndex);
                var form3 = new Form();
                form3.Width = 400;
                form3.Height = 200;
                form3.Text = "How long will you have the item?";
                form3.Show();

                DateTimePicker howLongPick = new DateTimePicker();
                howLongPick.Width = 150;
                howLongPick.Value = DateTime.Today.AddDays(7);
                howLongPick.Location = new Point(100, 50);

                Button addDate = new Button();
                addDate.Location = new Point(135, 100);
                addDate.Text = "OK";

                form3.Controls.Add(addDate);
                form3.Controls.Add(howLongPick);

                CheckoutUntil = howLongPick.Text;

                addDate.Click += new EventHandler(addDateClicked);

                dataGridView1.Rows[numberRow].Cells[4].Value = true;
                newHistoryRow["Action"] = "Checkout";
                sIMSDataSet.Tables["History"].Rows.Add(newHistoryRow);
                historyTableAdapter.Update(sIMSDataSet);
        }
     }

    private void addDateClicked(object sender, EventArgs e, int numberRow)
    {
        dataGridView1.Rows[numberRow].Cells[15].Value = CheckoutUntil;
    }

What i would like to do, is pass numberRow through the addDate.Click += new EventHandler(addDateClicked);

But i can't seem to figureout how to do it.

James Morrish
  • 455
  • 6
  • 24

1 Answers1

2

Simply avoid creating the method addDateClicked and just do it inline like this:

int numberRow = Convert.ToInt32(e.RowIndex);

// code commented out

addDate.Click += (s, e2) =>
{
    // you can use `numberRow` in here now.
    dataGridView1.Rows[numberRow].Cells[15].Value = CheckoutUntil;
};
Enigmativity
  • 113,464
  • 11
  • 89
  • 172