0

I have gridview and its binded with datasource which is created in the code behind file. And I needed to enable sorting and paging in the easiest way so I wrote as following -

In button's click event

SqlDataSource dataSource = new SqlDataSource(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, searchQuery);
        dataSource.SelectCommandType = SqlDataSourceCommandType.Text;
        dataSource.SelectCommand = searchQuery;
        if (txtSearchQuery.Text != "")
        {
            dataSource.SelectParameters.Add("searchQuery", txtSearchQuery.Text);
        }
        gridBookings.DataSourceID = dataSource.ID;

However, when the button was clicked, the gridview wasn't populated with data. Any ideas?

3 Answers 3

3

you forgot to call DataBind on the grid:

gridBookings.DataBind();
Sign up to request clarification or add additional context in comments.

1 Comment

Now its returning empty data. I'm sure the query is correct and there are data in the DB. However, if I change to - gridBookings.DataSource = dataSource; gridBookings.DataBind(); The grid is populated. However, I can't sort it.
0

you must databind

...
gridBookings.DataSourceID = dataSource.ID;
gridBookings.DataBind();

Although I'd prefer this approach

...
gridBookings.DataSource = dataSource;
gridBookings.DataBind();

4 Comments

From what I've read on a blog, if I use DataSource instead of DataSourceID, I'd have to manually write the code for sorting and paging. Is it correct?
That depends on the control, but if you want control then yes. Have a look at msdn.microsoft.com/en-us/library/ms745786.aspx
So how would you recommend if I want it to get sorting and paging without minimal codes.
I'd say use the controls shipping with the GridView.
0

difficult to say without seeing your full page markup and code behind.

In general after associating the DataSource you should also call:

gridBookings.DataBind();

Comments

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.