Example for Gridview Row Command Event :
=================================
This below code is useful for Gridview sorting Purpose . Let me know if you are facing any problem .
Design :
<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="gvNew_RowCancelingEdit"
OnRowCommand="gvNew_RowCommand" OnRowDataBound="gvNew_RowDataBound" OnRowEditing="gvNew_RowEditing"
OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="gvNew_RowUpdating" AllowPaging="true"
PageSize="30" DataKeyNames="ecm_id" OnPageIndexChanging="gvNew_PageIndexChanging"
ShowFooter="false" OnRowDeleting="gvNew_RowDeleting">
<Columns>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM_ID" runat="server" Text="ECM Number" CommandName="ECM_ID"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="lblECM_id" Width="100px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="txtECMidET" Width="100px"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<asp:GridView>
Coding :
static int i;
Page load you can declare the static variable . This variable is used for ascending and descending purpose .
Default set to 0 .
i = 0;
protected void gvNew_RowCommand(object sender, GridViewCommandEventArgs e)
{
//This code is useful for Grid view sorting purpose
if (e.CommandName == "ECM_ID")
{
if (i == 0)
{
ViewState["SortString"] = "ecm_id ASC";
//Below method is used to bind data to Grid view
BindGridDataAll();
i = 1;
}
else
{
ViewState["SortString"] = "ecm_id DESC";
//Below method is used to bind data to Grid view
BindGridDataAll();
i = 0;
}
}
}
// Declare dataview
public void BindGridDataAll()
{
ds = ObjAtSite.GetECMdataUsingPId();
dvData = ds.Tables[0].DefaultView;
string SortString = "";
if (ViewState["SortString"] != null)
{
SortString = ViewState["SortString"].ToString();
}
if (SortString != "")
{
dvData.Sort = SortString;
gvNew.DataSource = dvData;
gvNew.DataBind();
}
else
{
gvNew.DataSource = dvData;
gvNew.DataBind();
}
}
=================================
This below code is useful for Gridview sorting Purpose . Let me know if you are facing any problem .
Design :
<asp:GridView ID="gvNew" runat="server" AutoGenerateColumns="false" OnRowCancelingEdit="gvNew_RowCancelingEdit"
OnRowCommand="gvNew_RowCommand" OnRowDataBound="gvNew_RowDataBound" OnRowEditing="gvNew_RowEditing"
OnRowUpdated="gvNew_RowUpdated" OnRowUpdating="gvNew_RowUpdating" AllowPaging="true"
PageSize="30" DataKeyNames="ecm_id" OnPageIndexChanging="gvNew_PageIndexChanging"
ShowFooter="false" OnRowDeleting="gvNew_RowDeleting">
<Columns>
<asp:TemplateField HeaderStyle-Height="30px" FooterStyle-Height="25px">
<HeaderTemplate>
<asp:LinkButton ID="lnkECM_ID" runat="server" Text="ECM Number" CommandName="ECM_ID"></asp:LinkButton>
</HeaderTemplate>
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="lblECM_id" Width="100px"></asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:Label runat="server" Text='<%# Eval("ecm_id") %>' ID="txtECMidET" Width="100px"></asp:Label>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<asp:GridView>
Coding :
static int i;
Page load you can declare the static variable . This variable is used for ascending and descending purpose .
Default set to 0 .
i = 0;
protected void gvNew_RowCommand(object sender, GridViewCommandEventArgs e)
{
//This code is useful for Grid view sorting purpose
if (e.CommandName == "ECM_ID")
{
if (i == 0)
{
ViewState["SortString"] = "ecm_id ASC";
//Below method is used to bind data to Grid view
BindGridDataAll();
i = 1;
}
else
{
ViewState["SortString"] = "ecm_id DESC";
//Below method is used to bind data to Grid view
BindGridDataAll();
i = 0;
}
}
}
// Declare dataview
public void BindGridDataAll()
{
ds = ObjAtSite.GetECMdataUsingPId();
dvData = ds.Tables[0].DefaultView;
string SortString = "";
if (ViewState["SortString"] != null)
{
SortString = ViewState["SortString"].ToString();
}
if (SortString != "")
{
dvData.Sort = SortString;
gvNew.DataSource = dvData;
gvNew.DataBind();
}
else
{
gvNew.DataSource = dvData;
gvNew.DataBind();
}
}
No comments:
Post a Comment