Search code examples
c#datagridviewcellrequirednewrow

C# Datagridview make mandatory cell on new row


i'm searching a way to make an cell an mandatory cell on a new row. The row consists of 7 cells. The Problem i have is, that if User enters the new row, he can skip the very important first cell which have to contain a required number. I want that if user enters the new row that he has to enter first the required cell in that row, before he can add more informations to the second cell and so on. I've tried row validating and checked for DBNull and so on, but nothing works. The best solution would be, that if an new row is entered, the first cell jumps to edit mode. If number is entered, the following cells can be edited, else not. If user cancels adding, the current row isn't added.

Thx for any kind of suggestions and info!


Solution

  • I've found a simple solution by myself. Just check in CellBeginEdit if the first cell has been filled on the new row - else other cells can not be edited and so no new row is been added. May someone else can need it too. Thank you very much for your help!

    private void dgvUsers_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
            {
                if (e.ColumnIndex != 0)
                {
                    if ((e.RowIndex == dgvUsers.NewRowIndex) && (dgvUsers[0, e.RowIndex].Value == null))
                    {
                        e.Cancel = true;
                    }
                }
            }