none
How to disable editing in the existing rows in a datagridview and allow the user to add new row to same?

    Question

  • Hello,

    I have a datagridview where I should not be able to edit the the existing rows, but i must be able to add new rows to it.

    I have set the datagridview.ReadOnly=True (for disabling edit)

    Also to allow the user to add new rows, i have set as  datagridview.AllowUserToAddNewRows=True


    But the desired effect is not happening. Still I am not able to add new row.

    Can anyone help me in solving this problem?

    Thanks in advance.

    Thursday, September 2, 2010 7:13 AM

Answers

  • Hello BetcPa,

    First, set both Readonly and AllowUserToAddNewRows to true is contradictory.

    You should first set the AllowUserToAddNewRows property to true, but ReadOnly to false, and then when you click some cell (that is to say in the CellClick event), change the ReadOnly property to true depends on the cell value. If the cell has value then change the ReadOnly to true, if the cell has no value, keep the ReadOnly to false to allow you input. Here is the code:

         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

            {

                dataGridView1.Rows[e.RowIndex].ReadOnly = true;

                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)

                {

                    dataGridView1.Rows[e.RowIndex].ReadOnly = false;

                }
            }

     

    This will meet your needs. If you have any questions, please feel free to let me know.

     

    Best regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Mike_999 Wednesday, September 8, 2010 6:52 AM
    • Marked as answer by Helen Zhou Monday, September 13, 2010 1:55 AM
    Wednesday, September 8, 2010 6:31 AM

All replies

  • Hi,

    You may set back  Readonly and AllowUserToAddNewsRows to true

    and may try this alternative:

     private static int AddedRowIndex=-1; 
    
        private void dataGridView1_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
          AddedRowIndex = dataGridView1.NewRowIndex - 1;
        }
    
        private void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
        {
    
          if (!dataGridView1.Rows[e.RowIndex].IsNewRow)
          {
            
            if (e.RowIndex < dataGridView1.NewRowIndex)
            {
              if (AddedRowIndex != e.RowIndex)
              {
    
                MessageBox.Show("Not allow edit");
                e.Cancel = true;
    
              }
    
            }
          }
        }
    
    

    Hope can helps

     

    Best regards

    cchitsiang

    Thursday, September 2, 2010 3:31 PM
  • Hello BetcPa,

    First, set both Readonly and AllowUserToAddNewRows to true is contradictory.

    You should first set the AllowUserToAddNewRows property to true, but ReadOnly to false, and then when you click some cell (that is to say in the CellClick event), change the ReadOnly property to true depends on the cell value. If the cell has value then change the ReadOnly to true, if the cell has no value, keep the ReadOnly to false to allow you input. Here is the code:

         private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

            {

                dataGridView1.Rows[e.RowIndex].ReadOnly = true;

                if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value == null)

                {

                    dataGridView1.Rows[e.RowIndex].ReadOnly = false;

                }
            }

     

    This will meet your needs. If you have any questions, please feel free to let me know.

     

    Best regards,
    Helen Zhou


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Proposed as answer by Mike_999 Wednesday, September 8, 2010 6:52 AM
    • Marked as answer by Helen Zhou Monday, September 13, 2010 1:55 AM
    Wednesday, September 8, 2010 6:31 AM