DataGrid.Items.Count doesn't work as per expectation

Multi tool use
DataGrid.Items.Count doesn't work as per expectation
I have a WPF DataGrid
named datagrid1
and I want to check if DataGrid
rows are not empty.
DataGrid
datagrid1
DataGrid
Then how could I achieve this. I tried this but failed.
if (dataGrid1.Items.Count != 0)
{
//submit
}
else
{
MessageBox.Show("Data Grid is empty");
}
fails means that even my datagrid is empty it executes if block
– user9984958
Jul 3 at 5:54
fails means that even my datagrid is empty it executes if block, but i want that if datagrid is epmty it should go to else block.
– user9984958
Jul 3 at 5:55
1 Answer
1
You need to set CanUserAddRows
false:
CanUserAddRows
<DataGrid Name="dataGrid1" CanUserAddRows="false"></DataGrid>
You get an empty row at the bottom of your DataGrid
usually. This is why, you don't go to the else part, even though your DataGrid
is empty.
DataGrid
DataGrid
what would be effect of CanUserAddRows="false" ??
– user9984958
Jul 3 at 6:04
@JahanzaibNiazi Just try it and you will see the result. You get an empty row at the bottom of your DataGrid usually. This is why, you don't go to the else part, even your
DataGrid
is empty.– S.Akbari
Jul 3 at 6:06
DataGrid
wao it has worked :) Thanks a lot
– user9984958
Jul 3 at 6:08
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
What does it mean it failed? What is the error you get? Please provide Minimal, Complete, and Verifiable example
– FCin
Jul 3 at 5:51