简体   繁体   中英

Adding new row to DataGridView by KeyDown

I have a DataGridView with 4 columns (column 3 is hidden). The DataGridView is ready for user input when it is loaded (I added one row and set AllowUserToAddRows=false) . When the user press Enter then the next cell is focused and editable. After the third column pressing Enter then a new row should be inserted.

For adding the new row I have to write the already inserted data back to a datatable because the DataGridView is already databound.

When I try to access the first row in which I inserted some values it returns me always zero:

在此处输入图片说明

在此处输入图片说明

How can I access the first row? Thanks!

Code for Datatable :

 dtKasse.Columns.Add("Nummer");
 dtKasse.Columns.Add("Unternummer");
 dtKasse.Columns.Add("Betrag");
 dtKasse.Columns.Add("Datum");

 dtKasse.Rows.Add("", "", "", DateTime.Now.ToShortTimeString());
 gv_Input.AllowUserToAddRows = false;
 gv_Input.DataSource = dtKasse;

 DataGridViewCell cell = gv_Input.Rows[0].Cells[0];
 gv_Input.CurrentCell = cell;
 gv_Input.BeginEdit(true);

Hide third column:

private void gv_Input_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
    {
        gv_Input.Columns[3].Visible = false;
    }

Handle pressing Enter :

private void gv_Input_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyData == Keys.Enter)
    {
        int col = gv_Input.CurrentCell.ColumnIndex;
        int row = gv_Input.CurrentCell.RowIndex;

        if (col < 2) 
        {
            col++;
        }
        else
        {
            col = 0; 
            row++; 
        }

        if (row == gv_Input.RowCount) 
        {
            dtKasse.Clear(); 
            foreach (DataGridViewRow dgr in gv_Input.Rows)
            {
                dtKasse.Rows.Add(dgr.Cells[0].Value, dgr.Cells[1].Value, dgr.Cells[2].Value, dgr.Cells[3].Value);
            }
            DataRow dr = dtKasse.NewRow();
            dtKasse.Rows.Add(dr);
            gv_Input.DataSource = dtKasse;
        }

        DataGridViewCell cell = gv_Input.Rows[row].Cells[col];
        gv_Input.CurrentCell = cell;
        gv_Input.BeginEdit(true);

    }
}

What you must understand about setting the DataGridView.DataSource is that:

  • Changes made to the source will be reflected in the DataGridView .

With that in mind, consider the following:

  1. Calling dtKasse.Clear(); will clear your DataGridView.Rows too.
  2. Adding a new row to the sourced DataTable for each row in the DataGridView will cause an infinite loop.
  3. Resetting the DataGridView.DataSource is redundant.

Solution : In your KeyDown event, change your inner if-statement to simply add a new row to the sourced DataTable :

if (row == gv_Input.RowCount) 
{
    DataRow dr = dtKasse.NewRow();
    dtKasse.Rows.Add(dr);
}

Also, you should add e.Handled = true after you set the CurrentCell and call BeginEdit , otherwise you'll see undesired behavior navigating cells.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM