开发者

Gridview is not displaying data with code-behind sqldatasource

开发者 https://www.devze.com 2023-03-31 18:39 出处:网络
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 -

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(Config开发者_如何学GourationManager.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?


you forgot to call DataBind on the grid:

gridBookings.DataBind();


you must databind

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

Although I'd prefer this approach

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


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

In general after associating the DataSource you should also call:

gridBookings.DataBind();
0

精彩评论

暂无评论...
验证码 换一张
取 消