manual gridview paging - asp.net

i'm am pulling 10 out of 100 records back from the database and placing into gridview (no datasource objects here).
how do i enable paging that comes with the gridview? I know the total records is 100 can I use that somehow to activate the paging?
I know I can do this easily with DataSource objects but was just wondering if I could do it completely manually as far as the GridView is concerned.
Markup
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AllowSorting="True" onpageindexchanging="GridView1_PageIndexChanging"
onsorting="GridView1_Sorting">
</asp:GridView>
</div>
</form>
Code-behind
protected void Page_Load(object sender, EventArgs e)
{
GridView1.DataSource = GetCustomers();
GridView1.DataBind();
}
strong text

Try this:
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback) {BindData();}
}
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.DataSource = GetCustomers();
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataBind();
}
void BindData()
{
GridView1.DataSource = GetCustomers();
GridView1.DataBind();
}
Also you need to add this to gridview markup:
OnPageIndexChanging="GridView1_PageIndexChanging"

Related

Populate different gridview by clicking different button

In my web page header, there are several buttons. What I want is to click each button to display a desired table in a gridview.
click button1
Display table in gridview 1.
click button2 Display table in gridview 2, Gridview 1 disappeared.
The header and buttons are persisting in the page. I don't want to redirect the current page to different pages. I only want to implement the function in one web page.
ASPX
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
<asp:GridView ID="GridView2" runat="server">
</asp:GridView>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Button ID="Button2" runat="server" Text="Button" onclick="Button2_Click" />
</div>
C#
protected void Button1_Click(object sender, EventArgs e)
{
GridView1.Visible = true;
GridView2.Visible = !GridView1.Visible;
GridView1.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
GridView1.Visible = false;
GridView2.Visible = !GridView1.Visible;
GridView2.DataBind();
}
You may need to pass in the .DataBind() call depending on how you are binding your data to the gridviews.
On each button click just bind your GridView as needed. For example:
Button1 clicked do:
protected void Button1_OnClick(object sender, EventArgs e)
{
grdYourGrid1.DataSource = yourDataSource;
grdYourGrid1.DataBind();
grdYourGrid1.Visible = true;
grdYourGrid2.DataSource = null;
grdYourGrid2.DataBind();
grdYourGrid2.Visible = false;
}
Then do the opposite for Button2. I suggest rebinding and nulling the non visible grid so the ViewState doesn't get bloated when not required.
You can put this in the Page_Load
button1.Click+=(s,ev)=>
{
gridview1.visible = true;
gridview1.DataBind();
};
button2.Click+=(s,ev)=>
{
gridview2.DataBind();
gridview1.visible = false;
};
C#
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.visible = true; gridview1.DataBind(); gridview2.visible = false;}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.visible = true; gridview2.DataBind(); gridview1.visible = false;}
or
buttin1_Onclick(object sender, EventArgs e)
{ gridview1.DataBind(); gridview2.Rows.Clear();}
buttin2_Onclick(object sender, EventArgs e)
{ gridview2.DataBind(); gridview1.Rows.Clear();}

Unable to do paging on gridview asp.net,

I have code to have a dynamic button appear after it does an if else loop for checking for some data.
however, the gridview cant do any paging. it only shows the first page and cant go to 2nd page and so on.
It only can do the paging IF i nvr call the method below.
here is my code
public void filter_select(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
........
}
that method will be called on the rowdatabound gridview page source like this OnRowDataBound="filter_select"
my gridview code on source code is :
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" CellPadding="2" CellSpacing="2"
HorizontalAlign="Center" OnRowCommand="stop_survey"
OnRowDataBound="filter_select" OnSelectedIndexChanging="selected" PageSize="5"
Width="133%" DataKeyNames="SurveyID" DataSourceID="SqlDataSource1" AutoGenerateColumns="False"
>
<Columns>
<asp:BoundField DataField="SurveyID" HeaderText="Survey ID" ReadOnly="True"
.........
my onrowCommand code
public void stop_survey(object sender, GridViewCommandEventArgs e)
{
SqlConnection con;
string sqlcom;
SqlCommand cmd;
//is it because of this code below? as if i comment from this code onwards, the paging works.
GridViewRow row = (GridViewRow)((Button)e.CommandSource).NamingContainer;
Button btn = (Button)GridView1.Rows[row.RowIndex].FindControl("btnStop");
...............
Rebind your gridview in the PageIndexChanging event of the gridview control
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = DataAcceessLayer.GetEmployeeData();
GridView1.DataBind();
}

How to change the GridView page Index

How to change Gridview pages based on the dropdown list selected index? the dropdown list is not inside the gridview, any help would be appreciated.
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridView1.PageIndex = DropDownList1.SelectedItem.Value;
GridView1.DataSource = datatable;
GridView1.DataBind();
}
What you need to do is set the GridView's PageSize property to the value of your DropDownList's selected item value:
private void DropDownListPageSize_SelectedIndexChanged(object sender, EventArgs e)
{
gridview.PageSize = DropDownListPageSize.SelectedValue;
}

gridview event: no overload matches delegate

Here's the problem with the gridview as I have it:
in the aspx:
<asp: GridView..... ID="MyGrid" AllowSorting = "True" OnSorting = "SortMyGrid">
in the code behind:
protected void SortMyGrid(object sender, GridViewPageEventArgs e)
{
DataTable TheGridData = MyGrid.DataSource as DataTable;
}
And then when I run the code:
No overload for 'SortMyGrid' matches delegate
What's causing this problem?
Thanks.
You have the wrong EventArgs you need to be using GridViewSortEventArgs not GridViewPageEventArgs
MSDN Article GridView Sorting Event

Programmatically bind List<String> to GridView

Just trying a simple bind. No values are populated into the GridView however.
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Bread");
list.Add("Cheeze");
list.Add("Wine");
list.Add("Beer");
list.Add("Waffles");
GridView1.DataSource = list;
GridView1.DataBind();
}
Did you set AutoGenerateColumns to true?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="true" />
another approach
protected void Page_Load(object sender, EventArgs e)
{
List<string> list = new List<string>();
list.Add("Bread");
list.Add("Cheeze");
list.Add("Wine");
list.Add("Beer");
list.Add("Waffles");
GridView1.DataSource = list;
GridView1.DataBind();
GridView1.Columns.Add(new BoundField());
}

Resources