Reputation: 3896
I have a local DB table that is displayed in a GridView
. In that table, I have a column called: "Completed".
When I first display the table, I am displaying it without completed records (Completed=false). Here is the SqlDataSource
select command:
"SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";
I have a checkbox which causes postback. I want to toggle the display of Completed records by ticking / unticking it:
protected void cbShowCompletedRecords_CheckedChanged(object sender, EventArgs e)
{
if (cbShowCompletedRecords.Checked)
CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] ORDER BY [Priority]";
else
{
CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";
}
}
Currently, when I check the box, I get all the records. But when I uncheck it, the GridView
doesn't update, even though the code above executes. What I am missing?
Upvotes: 1
Views: 2150
Reputation: 918
Try using your GridView
s DataBind
method after the if...else
block
protected void cbShowCompletedRecords_CheckedChanged(object sender, EventArgs e)
{
if (cbShowCompletedRecords.Checked)
CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] ORDER BY [Priority]";
else
{
CEDatabaseSource.SelectCommand = "SELECT * FROM [CERecord] WHERE [Completed]='false' ORDER BY [Priority]";
}
yourGrid.DataBind();
}
This will make sure the results shown in your grid reflect the changes you made to the select statement.
Upvotes: 2
Reputation: 698
If Completed is bit type in DB change your query to:
"SELECT * FROM [CERecord] WHERE [Completed]=0 ORDER BY [Priority]";
Upvotes: -1
Reputation: 57222
The SqlDataSource allows you to bind parameters to control properties:
<asp:SqlDataSource ID="myds" runat="server"
SelectCommand="SELECT * FROM [CERecord] WHERE [Completed]=:COMPLETED ORDER BY [Priority]">
<SelectParameters>
<asp:ControlParameter ControlID="MyCheckbox" Name="COMPLETED" PropertyName="Checked" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
This will modify your select command according to the "Checked" property of your checkbox.
Upvotes: 1