0

Why is there an additional row in my DataGrid?

My DataSource has 15 rows.

source.DataSource = dt;
MessageBox.Show(Convert.ToString(source.Count)); // shows 15

dataGrid1.ItemsSource = source;
MessageBox.Show(Convert.ToString(dataGrid1.Items.Count)); // shows 16

My DataGrid has 16 rows, the last one is null.

How can I do delete the last row?

MatthiasG
  • 4,434
  • 3
  • 27
  • 47
igni
  • 47
  • 1
  • 9
  • Possible duplicate of [WPF datagrid empty row at bottom](http://stackoverflow.com/questions/1783839/wpf-datagrid-empty-row-at-bottom) – Neelam Prajapati Jul 21 '16 at 08:35

4 Answers4

8

I could imagine that what you see is the empty row adding new records. Try:

<DataGrid CanUserAddRows="False" ...

to disable adding new rows, and I expect the surplus row to be gone.

HCL
  • 36,053
  • 27
  • 163
  • 213
1

Your number as of type integral also offer a toString method. why don't you try it instead of using Convert function ?

To not populate the last row, there is a property of autocolumn-generate (? please recheck) which should be set to false.

[If my reply is correct please sing out : ANABELLA the best! lala la la la]

Anabella
  • 39
  • 6
0
foreach (DataGridViewRow row in DataGrid1.SelectedRows)
{
DataGrid1.Rows.Remove(row);
}

Not sure if i understood you right. Do you want to delete 16 rows? Select them and bind above to a button.

Andreas Nilsson
  • 231
  • 1
  • 6
0

By default if you add a DataGridView to your form, it will have a row, that row always sticks to the DataGridView unless you set CanUserAddRows property to false by selecting the DataGridView and pressing F4 or through XAML code.

If this property is set to true then the user can manually add a row to the DataGridView, but in cases where you want to display a 'read-only' kind of or bound data into the DataGridView then you should set the above mentioned property to false.

Hope it helps you.

Robert Langdon
  • 855
  • 1
  • 11
  • 27