This is a migrated thread and some comments may be shown as answers.

Next page problem gridview

1 Answer 80 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Grant
Top achievements
Rank 1
Grant asked on 20 Feb 2014, 01:28 PM
Hi 

When i click the next page or page 2 button the gridview doesnt populate.

the problem is that i databind from different sources depending on what the user selects previously to view.

  <telerik:RadGrid ID="rgLog" runat="server" AllowPaging="True" PageSize="15" 
                        AutoGenerateColumns="False" AllowSorting="true" 
                            onpageindexchanged="rgLog_PageIndexChanged">


Grant

1 Answer, 1 is accepted

Sort by
0
Accepted
Princy
Top achievements
Rank 2
answered on 21 Feb 2014, 05:16 AM
Hi Grant,

Make sure you bind the RadGrid using Advanced Data-binding (using NeedDataSource event). Please take a look at the below sample code snippet.

ASPX:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
    <asp:ListItem Text="empty grid" Value="0" />
    <asp:ListItem Text="1 column grid" Value="1" />
    <asp:ListItem Text="2 column grid" Value="2" />
    <asp:ListItem Text="3 column grid" Value="3" />
</asp:DropDownList>
<br />
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />

C#:
RadGrid grid;
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
    RadGrid grid = PlaceHolder1.FindControl("grid") as RadGrid;
    grid.Rebind();
}
protected void Page_Init(object sender, System.EventArgs e)
{
    PopulateGridOnPageInit();
}
    
protected void PopulateGridOnPageInit()
{
    string ddlValue = Request.Form.Get("DropDownList1");
    grid = new RadGrid();
    grid.ID = "grid";
    grid.Width = Unit.Pixel(400);
    grid.AllowSorting = true;
    grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
    grid.AllowPaging = true;
    grid.Skin = "Sunset";
    grid.NeedDataSource += new GridNeedDataSourceEventHandler(grid_NeedDataSource);
    grid.MasterTableView.AutoGenerateColumns = false;
    grid.MasterTableView.EnableColumnsViewState = false;
    GridBoundColumn boundColumn;
    if (ddlValue != null)
    {
        switch (ddlValue)
        {
            case "0":
                grid.DataSourceID = "";
                grid.DataSource = new object[0];
                break;
            case "1":
                boundColumn = new GridBoundColumn();
                boundColumn.HeaderText = "ContactName";
                boundColumn.DataField = "ContactName";
                boundColumn.UniqueName = "ContactName";
                grid.MasterTableView.Columns.Add(boundColumn);
                break;
            case "2":
                boundColumn = new GridBoundColumn();
                boundColumn.HeaderText = "OrderID";
                boundColumn.DataField = "OrderID";
                boundColumn.UniqueName = "OrderID";
                grid.MasterTableView.Columns.Add(boundColumn);
                boundColumn = new GridBoundColumn();
                boundColumn.HeaderText = "ShipCIty";
                boundColumn.DataField = "ShipCIty";
                boundColumn.UniqueName = "ShipCIty";
                grid.MasterTableView.Columns.Add(boundColumn);
                break;
            case "3":
                boundColumn = new GridBoundColumn();
                boundColumn.HeaderText = "EmployeeID";
                boundColumn.DataField = "EmployeeID";
                boundColumn.UniqueName = "EmployeeID";
                grid.MasterTableView.Columns.Add(boundColumn);
                boundColumn = new GridBoundColumn();
                boundColumn.HeaderText = "FirstName";
                boundColumn.DataField = "FirstName";
                boundColumn.UniqueName = "FirstName";
                grid.MasterTableView.Columns.Add(boundColumn);
                    
                break;
        }
    }
    else
    {
        grid.DataSource = new object[0];
    }
    PlaceHolder1.Controls.Add(grid);
}
 
void grid_NeedDataSource(object sender, GridNeedDataSourceEventArgs e)
{
    string ddlValue = Request.Form.Get("DropDownList1");
    if (ddlValue != null)
    {
        switch (ddlValue)
        {
            case "0":
                grid.DataSource = new object[0];
                break;
            case "1":
                grid.DataSource = GetDataTable("SELECT ContactName FROM Customers");
                break;
            case "2":
                grid.DataSource = GetDataTable("SELECT OrderID,ShipCIty FROM Orders");
                break;
            case "3":
                grid.DataSource = GetDataTable("SELECT EmployeeID,FirstName FROM Employees");                 
                break;
        }
    }
}
public DataTable GetDataTable(string query)
{
    String ConnString = ConfigurationManager.ConnectionStrings["Northwind_newConnectionString3"].ConnectionString;
    SqlConnection conn = new SqlConnection(ConnString);
    SqlDataAdapter adapter = new SqlDataAdapter();
    adapter.SelectCommand = new SqlCommand(query, conn);
 
    DataTable myDataTable = new DataTable();
 
    conn.Open();
    try
    {
        adapter.Fill(myDataTable);
    }
    finally
    {
        conn.Close();
    }
 
    return myDataTable;
}

Thanks,
Princy
Tags
Grid
Asked by
Grant
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Share this question
or