1

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?

9
  • Try using the Rebind method after the if..else block Grid.Rebind() Commented Mar 14, 2013 at 14:16
  • what type is Completed in DB? bool or string? Commented Mar 14, 2013 at 14:17
  • @Ron Do you mean DataBind();? Commented Mar 14, 2013 at 14:19
  • I have already tried CEDatabaseSource.DataBind() and it doesn't change anything. Completed is bool in the db (well bit). Commented Mar 14, 2013 at 14:20
  • So why isn't your where clause WHERE [Completed]=0? Commented Mar 14, 2013 at 14:22

3 Answers 3

2

Try using your GridViews 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.

Sign up to request clarification or add additional context in comments.

1 Comment

Added some more context, and an explanation of the solution. You should always try to post as complete of an answer as you can. It will help future visitors more, and help you get more upvotes =)
1

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.

3 Comments

That is not quite the logic the OP is looking for. They either want all records, or just uncompleted records. Very good point about the ControlParameters, though.
Also, that syntax looks a little funky. Does [Completed]=:COMPLETED work? I've always used [Completed]=@COMPLETED.
@jadarnel27: sorry, I misread the question, thanks for pointing this out. By the way, I always used the : syntax and it worked for me...
-1

If Completed is bit type in DB change your query to:

"SELECT * FROM [CERecord] WHERE [Completed]=0 ORDER BY [Priority]";

1 Comment

This answer doesn't really answer the question, but it is better to practice to write your query this way @sd_dracula.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.