Change current DataRowView to a specific DataRowView

134 views Asked by At

I would like to change the current DataRowView to a specific line, so I can set the value where I want to.

public void SetTimeFromTimer(DateTime date, string timeFromTimer)
{
     monthCalendar1.SetDate(date);
     dataGridView1.ClearSelection();
     dataGridView1.Rows[1].Selected = true;
     DataRowView rowView = (DataRowView)BindingContext[repTimeTable].Current;
     rowView["zr"] = timeFromTimer;
}
2

There are 2 answers

0
Nikunj Gaur On

You would have to loop through each column and assign the value of the current cell to the desired row's cell. The following code can help you.

public void ChangeRowValues(int parentRow, int rowToBeChanged)
    {
        for (int i = 0; i < dataGridView.Columns.Count; i++)
        {
            dataGridView.Rows[rowToBeChanged].Cells[i].Value = 
            dataGridView.Rows[parentRow].Cells[i].Value;
        }
    }
1
JohnnyDorcy On
public void SetTimeFromTimer(DateTime date, string timeFromTimer)
{
  monthCalendar1.SetDate(date);
  dataGridView1.ClearSelection();
  int rowIndex = repTimeTable.Rows.IndexOf(repTimeTable.Select($"zraufnr = '{GetProjectNumber}' AND zrpnr = '{GetPersonalNumber}'")[0]);
  if (!string.IsNullOrEmpty(rowIndex.ToString()))
  {
    dataGridView1.CurrentCell = dataGridView1.Rows[rowIndex].Cells[ColumnZrZeit.Index];
    DataRowView rowView = (DataRowView) BindingContext[repTimeTable].Current;
    rowView["zr"] = timeFromTimer;
  }
}