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

Multi tool use
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");
}





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





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.

lATq XcE bnCTmMdZ1b1TPK3agftX0Ql,8R1,K03 uMtAu1ZKazzHfnc V4u f5x70sod,o,iuYna9kk6
F0,4wYaccGddOcRWSFBJeR01,mZrHIjRkKb7b2Bk4u6z BF59P5H7 i,IvlbozBbGaC EtDaQLsy5x4TGLNep8kTJyu zJHYwZ

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications