一、gridview使用SqlDataSource作为数据源
<div> <asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1" AllowSorting="true"> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:IHM_20130424ConnectionString %>" SelectCommand="SELECT [sFileName], [sCreatedBy], [sStatus], [dCreatedDate] FROM [tbReport_DownLoad] WHERE (([bIsEnable] = @bIsEnable) AND ([bIsVisible] = @bIsVisible))"> <SelectParameters> <asp:Parameter DefaultValue="true" Name="bIsEnable" Type="Boolean" /> <asp:Parameter DefaultValue="true" Name="bIsVisible" Type="Boolean" /> </SelectParameters> </asp:SqlDataSource> </div>
定义gridview,AllowSorting=true并使DataSource=sqlDatasrouceID就可以排序
二、我们需要使用模板,在可编辑模板里放其他控件做特殊使用。
我们在上面添加一个itemTemplateField,里面是一个label使它与sqldatasource字段绑定
绑定方法,编辑模板->找到itemTemplateField然后Label任务里有个编辑DataBindings
选中Text,如图
选绑定到哪个字段,然后下面自定义绑定就会给出你选了的绑定字段的代码表达式,如果不能字段绑定就点“刷新架构”
我们选择sFileName这个字段后先自定义绑定得到代码表达式:Eval("sFileNam“)
<asp:TemplateField> <HeaderTemplate> <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton> </HeaderTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("sFileName") %>' ForeColor="Red"></asp:Label> </ItemTemplate> </asp:TemplateField>
上面添加了一个<HeaderTemplate>,这样点击Linkbutton是不能排序的。
我们得把HeadTemplate去掉,在开加标签加入sortexpression和headertext
<asp:TemplateField SortExpression="sFileName" HeaderText="FileName"> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Eval("sFileName") %>' ForeColor="Red"></asp:Label> </ItemTemplate> </asp:TemplateField>
上面就可以在gridview上点击FileName来排序了。其中SortExpressionr 的值是数据源的列名,不是列名就出错。
三、不使用sqlDatasource作为数据源,使用DataView,此时出现下面一行错误
GridView“GridView1”激发了未处理的事件“Sorting”。
不使用sqlDatasource作数据源时,AllowSorting需要处理Sorting事件
DataView.Sort
// // 摘要: // 获取或设置 System.Data.DataView 的一个或多个排序列以及排序顺序。 // // 返回结果: // 一个字符串,它包含列名,后跟“ASC”(升序)或“DESC”(降序)。在默认情况下列按升序排序。多个列可用逗号隔开。 [DefaultValue("")] [ResCategory("DataCategory_Data")] [ResDescription("DataViewSortDescr")] public string Sort { get; set; }
上面是DataView.Sort的文档,其说明了默认为空值,格式是:列名+空格" "+“asc”或+"desc"
下面是数据源
public DataView CreateDataSource() { DataTable dt = new DataTable(); dt.Columns.Add("sFileName", typeof(string)); dt.Columns.Add("sCreatedBy", typeof(string)); dt.Columns.Add("sStatus", typeof(string)); dt.Columns.Add("dCreatedDate", typeof(string)); string[] row0 = new string[4]; row0[0] = "报表1.xls"; row0[1] = "Lam"; row0[2] = "完成"; row0[3] = DateTime.Now.ToShortDateString(); dt.Rows.Add(row0); string[] row1 = new string[4]; row1[0] = "报表2.xls"; row1[1] = "Sam"; row1[2] = "完成"; row1[3] = DateTime.Now.AddDays(-1).ToShortDateString(); dt.Rows.Add(row1); DataView dv = new DataView(dt); return dv; }
下面是Sorting事件
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { DataView dv = CreateDataSource(); if (e.SortDirection==SortDirection.Descending) { dv.Sort = e.SortExpression + " "+"asc"; } else { dv.Sort = e.SortExpression +" "+ "desc"; } GridView1.DataSource = dv; GridView1.DataBind(); }
这一事件可惜只能改变一次值,因为
e.SortDirection一直都是值为SortDirection.Ascending如果没有作其他保存。
我们用ViewState来保存排序状态
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e) { DataView dv = CreateDataSource(); if (ViewState["sortDirection"]==null) { dv.Sort = e.SortExpression + " " + "asc"; ViewState["sortDirection"] = dv.Sort; } else if (ViewState["sortDirection"].ToString() == e.SortExpression + " " + "asc") { dv.Sort = e.SortExpression + " " + "desc"; ViewState["sortDirection"] = dv.Sort; } else { dv.Sort = e.SortExpression + " " + "asc"; ViewState["sortDirection"] = dv.Sort; } GridView1.DataSource = dv; GridView1.DataBind(); }
ViewState["sortDirection"]刚开始是null值。上面的sorting事件也已可以排序了。
三、还是使用sqldatasource
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
switch (e.CommandName)
{
case "Sorting":
if (GridView1.SortExpression == e.CommandArgument.ToString())
{
switch (GridView1.SortDirection)
{
case SortDirection.Ascending:
GridView1.Sort(e.CommandArgument.ToString(), SortDirection.Descending);
break;
case SortDirection.Descending:
GridView1.Sort(e.CommandArgument.ToString(), SortDirection.Ascending);
break;
default:
break;
}
}
else
{
GridView1.Sort(e.CommandArgument.ToString(), SortDirection.Ascending);
}
break;
default:
break;
}
}
The RowCommand event is raised when a button is clicked in theGridView control.
当在GridView控件里的某一个Button被点击时,RowCommand事件被触发。
e.CommandName可以获取这个Button的CommandName属性值,我们根据取值的不同来执行不同的代码块。
因为每个在GridView里的Button我们都设置了CommandName所以代码里也就没有那么多的Button_click事件。
而e.CommandArgument是点击行的索引值。
这样都可以排序
GridView.RowDataBound Event
Occurs when a data row is bound to data in a GridView control.
You can determine which row type (header row, data row, and so on) is being bound by using theRowType property
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) { if(e.Row.RowType == DataControlRowType.DataRow) { // Display the company name in italics. e.Row.Cells[1].Text = "<i>" + e.Row.Cells[1].Text + "</i>"; } }
Before the GridView control can be rendered, each row in the control must be bound to a record in the data source.
RowDataBound Event 在每次绑定数据时发生并且在GridView控件能被看到之前。
GridView的分页,做项目的时候因为有模板,一直不停地Copy,想想竟然连原理都没记住
分页事件:
void ContactsGridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
ContactsGridView.PageIndex = e.NewPageIndex;
GridView_DataBind();
}
单击 GridView 控件中的页导航按钮( CommandName 属性设置为“Page”的按钮)时,该控件会引发 PageIndexChanging 事件,
但在 GridView 控件处理分页操作之前。
就像上面那段代码,在点击页导航Button时,触发分页事件,GridView的页索引PageIndex变为点击的的页导航索引e.NewPageIndex;
绑定数据源(索引改变了),这时才显示GridView.这个是GridView自带的分页功能。