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
Related
I have a form and I am want to call a stored procedure on text change event of that text-box ,
Can you suggest any idea.
Thanks in advance.
You just have to handle the TextChanged-event:
<asp:TextBox ID="TextBox1" runat="server"
OnTextChanged="TextBox1_TextChanged">
</asp:TextBox>
Codebehind:
protected void TextBox1_TextChanged(Object sender, EventArgs e)
{
TextBox txt = (TextBox) sender;
// call your stored-procedure
}
Note that you should databind the controls only if(!IsPostBack), otherwise the events are not triggered. So for example in Page_Load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DataBindForm(); // which sets the `Text` property of this TextBox among others
}
}
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"
I have a dropdownlist and a gridview. When Dropdownlist is selected , gridview should show the data.
i think that's gonna work for you
http://www.facebook.com/groups/314912835194902/doc/406584239361094/
protected void DropDownListChanged(object sender, EventArgs e)
{
gridView.DataSource = GetDataSource();
gridView.DataBind();
}
you need to write an event SelectedIndexChanged="ddlChanged" and write your logic under it.
protected void ddlChanged(object sender, EventArgs e)
{
gridView.DataSource = GetData();
gridView.DataBind();
}
Make sure that you have AutoPostBack="true" on DropDownList. Otherwise it will not fire the event.
I have created:
one master page and one content page called Detail.
On Button click event, displaying data in grid view.
In grid view, columns are autogenerated.
I wanted to show 11 column in grid view, but it is more than page
size.
What to do for this?
I have created sql helper file for database connection code and calling that method, not using sqldatasource for connection.
When I trying to do paging, getting error:
The GridView 'GridView1' fired event PageIndexChanging which wasn't
handled.
You need to declare a method on your code behind that handles the PageIndexChanging event.
Something similar to this:
protected void GridView1_PageIndexChanging (object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
private void bindGridView()
{
GridView1.DataSource=getData();
GridView1.DataBind();
}
Providing sample code:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
bindGridView(); //bindgridview will get the data source and bind it again
}
protected void Page_Load(object sender , EventArgs e)
{
if(!IsPostBack)
bindGridView();
}
//this is some sample data
private void bindGridView()
{
DataTable t = new DataTable();
t.Columns.Add("Col1");
t.Columns.Add("Col2");
DataRow r = null;
for (int i = 0; i < 25; i++)
{
r = t.NewRow();
r.ItemArray = new object[] { "Val" + i, " Another " + i };
t.Rows.Add(r);
}
GridView1.DataSource = t;
GridView1.DataBind();
}
And this is the markup:
<asp:GridView OnPageIndexChanging="GridView1_PageIndexChanging" AllowPaging="true" PageSize="10" ID="GridView1" runat="server" AutoGenerateColumns="true">
Produces this:
For Paging you can use OnPageIndexChanging for this....
For Example
you have to use OnPageIndexChanging="gvdetails_PageIndexChanging" in your GridView...
You have to write below code into event in code behind like
protected void gvdetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvdetails.PageIndex = e.NewPageIndex;
BindData();
}
For more detail you can check the below link here I am using page Index change in my article...
Here I use PageIndexChange
I hope this will helps you....Share it with others...Thanks!
This is the final answer:
Imports System.Collections.Generic ' library
Protected Sub grdEmployees_PageIndexChanging1(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.GridViewPageEventArgs) Handles grdEmployees.PageIndexChanging
grdEmployees.PageIndex = e.NewPageIndex
LoadEmployeeList() 'FUNCTION FOR DATASET
grdEmployees.DataBind()
End Sub
you simply add this to your code :
protected void GridViewTrsEmail_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridViewTrsEmail.PageIndex = e.NewPageIndex;
GridViewTrsEmail.DataBind();
}
To fix this, I had to take a closer look at my datasource and datakeys. I have a set of records that are returned from SQL Server and what I was doing is binding them to a POCO. This class had several public properties of type Integer. These Integers were my datakeys on the grid. I replaced their type with a string instead to bypass the casting issue.
So, I have a GridView with an ObjectDataSource, and I want to programmatically set one of the SelectParameters of the ObjectDataSource.
I tried (during both Page_Load and DropdownList__SelectedIndexChanged)
objectDataSource.SelectParameters["my_parameter"].DefaultValue = "my_value";
objectDataSource.DataBind();
but it didn't work. What would you suggest?
Trap the onselecting event on the datasource.
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters["month"] = DateTime.Now.Month;
}
Nevermind, I solved it myself.
In Page_Load:
objectDataSource.Selecting += new ObjectDataSourceSelectingEventHandler(objectDataSource_Selecting);
Then write the handler method:
void objectDataSource_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
int four = 2 + 2;
e.InputParameters["my_parameter"] = four;
}
Then make sure to databind the GridView somewhere
protected void dropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
gridView.DataBind();
}