开发者

Changing IsNewRow status to false in DataGridView or Simulating keystroke on a cell

开发者 https://www.devze.com 2023-01-29 01:03 出处:网络
I\'m using a DGV in winforms application. I wanted to change a row status from New (IsNewRow) to edited. Whenever we start entering values in the last row of the DGV it creates another row below editi

I'm using a DGV in winforms application. I wanted to change a row status from New (IsNewRow) to edited. Whenever we start entering values in the last row of the DGV it creates another row below editing row by making current row IsNewRow property set to false.

In my requirement, when user is in the last row and if F6 is pressed then I copy couple of cell values from above row to current row. At this moment the current row is still in New row status (IsNewRow=true). Once I copy values to the current row from the above row, I want to change the status of the current row to edited, so that DGV creates another row below current row as a new row. Not sure which event makes DGV to create a new row, so please help me, in that.

Is there anyway I can change the status of the current row? IsNewRow property is readonly so cant modify it.

Update: I found开发者_StackOverflow中文版 a way to set the current row status (IsNewRow=false). Use the following method: MyGridView.NotifyCurrentCellDirty(true);.

At least I would like to simulate the keystroke, so that I fool DGV to create another row by making this row edited. Can anybody tell me how to do this in DGV?

This DGV is not databound.

Following is the code used to copy the values from above row to current row:

  switch (e.KeyData)
  {
    case Keys.F6: //Copy the row above.
      if (MyGridView.CurrentRow != null && MyGridView.CurrentRow.Index > 0)
      {
        MyGridView.CurrentRow.Cells[CustomColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[Customer.Index].Value;

        MyGridView.CurrentRow.Cells[DateColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[DateColumn.Index].Value;

        MyGridView.CurrentRow.Cells[RefColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[RefColumn.Index].Value;
       }
  }


I don't know how to manipulate the row state to do this, but you could add a new row, then set the current row values. I found that when you add a new row, it appears above the currently new row, so if you don't reset the current row, you get a blank row in the grid.

Try the following and see if it does what you want. I also changed the row index test so it only copies values if the user is on the new row.

  switch (e.KeyData)
  {
    case Keys.F6: //Copy the row above.
      if (MyGridView.NewRowIndex > 0 && MyGridView.NewRowIndex == rowIndex)
      {
        int colIndex = MyGridView.CurrentCell.ColumnIndex;

        MyGridView.Rows.Add();
        MyGridView.CurrentCell = MyGridView.Rows[rowIndex].Cells[colIndex];

        MyGridView.CurrentRow.Cells[CustomColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[Customer.Index].Value;

        MyGridView.CurrentRow.Cells[DateColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[DateColumn.Index].Value;

        MyGridView.CurrentRow.Cells[RefColumn.Index].Value
          = MyGridView.Rows[rowIndex - 1].Cells[RefColumn.Index].Value;
       }
  }
0

精彩评论

暂无评论...
验证码 换一张
取 消