21

This is about a .NET Windows Forms application written in C#. I have a DataGridView which is connected to a database. It displays the contents of a selected table.

Now if there are 4 rows in that table, it will display 4 rows. After this I am able to enter the 5th row in the datagrid view. It should be avoided. Now I have to disable creation of a new row. How can I do it?

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
saeed
  • 673
  • 3
  • 12
  • 22

4 Answers4

39

Simply set the DataGridView.AllowUserToAddRows property to false.

MarkJ
  • 30,070
  • 5
  • 68
  • 111
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
10

Set

dataGrid.AllowUserToAddRows = false
Axarydax
  • 16,353
  • 21
  • 92
  • 151
2

In XAML, set the property CanUserAddRows to false. For example see the following code

 <DataGrid   CanUserAddRows="False">

 </DataGrid>
Binoy Pilakkat
  • 150
  • 1
  • 6
  • 1
    This is the correct answer in WPF, but I don't think it works in Windows Forms. It helped me out tho in WPF – Lonefish Aug 21 '20 at 12:03
-6

Do this:

this.dataGridView1.DataBindings.Clear();
this.dataGridView1.DataSource = (set the original source back)
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
a user
  • 1