How to properly filter gridview bound to sqldatasource
I am trying to populate an (editable) gridview in ASP.NET with a table from SQL. I have a SQLDataSource set up for this. I also need to have this gridview be filterable based on parameters entered开发者_JAVA百科 in textboxes. I have tried using ControlParameters for this and it works but the problem there is when all textboxes are empty I want it to display all results in the gridview. What it does is display nothing because no entries have parameters that equal "". Is there an easy way to do this that I am missing or is there a better way to go about it other than a SQLDataSource?
I think I got it working. In case anybody else is wondering here is what I had to do. First I needed to modify my select statement within the SQLDataSource a little bit from
SELECT * FROM [MyTable] WHERE ([ColumnName] = @Param1)
to
SELECT * FROM [MyTable] WHERE (@Param1 IS NULL OR [ColumnName] = @Param1)
.
Then I set the parameter for the SQLDataSource: CancelSelectOnNullParameter="False".
精彩评论