sharepoint中一些gridview的使用方法
Question2:如何向GridView中加入格式化的超鏈接?
答: step1:在GridView中新建一個模板列。
?????? step2:向新加的模板列中添加一個HyperLink控件。
?????? step3:攝制組HyperLink的NavigateUrl值為'<%#"http://www."+ Eval("city")+".com" %>'>。
最終結果如下,省略數據綁定模塊:
<asp:GridView>
<Columns>
<asp:TemplateField>
????????????????????<ItemTemplate>
????????????????????????<asp:HyperLink?ID="HyperLink1"?runat="server"?Text="下載"?NavigateUrl='<%#"http://www."+?Eval("city")+".com"?%>'></asp:HyperLink>
????????????????????</ItemTemplate>
????????????????</asp:TemplateField>
????????????</Columns>
????????</asp:GridView>
???????解:第三步有兩個重要的知識點:第一個、’<%#Eval("x") %>'是前臺讀取C#后臺代碼中值得一個方法,假如Eval("x")的值為“baidu.com”,我們需要的值為“www.baidu.com”,我們就需要拼湊出這個值。正確的方法是:’<%#”www.”+Eval("x") %>',其它均會導致編譯錯誤如”www.”+’<%#Eval("x") %>'。第二個、必須在拼湊的網址前面加上“http://”或者“https://”,不然生產的鏈接為本網頁的子目錄地址。
Question3:用后臺程序生成一個可用的GridView,即用C#在后臺初始化一個GridView,我們還可以把它做成dll用在webpart里面。
using?System.Drawing;
using?System.Configuration;
using?System.Web.UI;
public?partial?class?Default2?:?System.Web.UI.Page
{
???//申明并初始化一個GridView
???GridView?GridView1?=?new?GridView();
???//定義一個datesorce
???SqlDataSource?EmployeeList?=?new?SqlDataSource();
???protected?void?Page_Init(object?sender,?System.EventArgs?e)
???{
??????//設置ID
??????GridView1.ID?=?"GridView1";
??????//允許翻頁、排序
??????GridView1.AllowPaging?=?true;
??????GridView1.AllowSorting?=?true;
??????//禁止自動生成列
??????GridView1.AutoGenerateColumns?=?false;
??????//設置一些樣式
??????GridView1.BackColor?=?Color.FromArgb(0xDE,?0xBA,?0x84);
??????GridView1.BorderColor?=?Color.FromArgb(0xDE,?0xBA,?0x84);
??????GridView1.BorderStyle?=?BorderStyle.None;
??????GridView1.BorderWidth?=?new?Unit("1px");
??????GridView1.CellPadding?=?3;
??????GridView1.CellSpacing?=?3;
??????GridView1.DataKeyNames?=?new?string[]?{?"EmployeeID"?};
??????//制定GridView的數據源
??????GridView1.DataSourceID?=?"EmployeeList";
??????//一頁可以顯示的記錄的數目
??????GridView1.PageSize?=?3;
??????//設置一些樣式
??????GridView1.RowStyle.BackColor?=?Color.FromArgb(0xFF,?0xF7,?0xE7);
??????GridView1.RowStyle.ForeColor?=?Color.FromArgb(0x8C,?0x45,?0x10);
??????GridView1.SelectedRowStyle.BackColor?
?????????=?Color.FromArgb(0x73,?0x8A,?0x9C);
??????GridView1.SelectedRowStyle.Font.Bold?=?true;
??????GridView1.SelectedRowStyle.ForeColor?=?Color.White;
??????GridView1.PagerStyle.ForeColor?=?Color.FromArgb(0x8C,?0x45,?0x10);
??????GridView1.PagerStyle.HorizontalAlign?=?HorizontalAlign.Center;
??????GridView1.HeaderStyle.BackColor?=?Color.FromArgb(0xA5,?0x51,?0x29);
??????GridView1.HeaderStyle.Font.Bold?=?true;
??????GridView1.HeaderStyle.ForeColor?=?Color.White;
??????//添加一個編輯事件
??????GridView1.RowEditing?+=
?????????new?GridViewEditEventHandler(GridView1_RowEditing);
??????//添加一個命令列
??????CommandField?cmdColumn?=?new?CommandField();
??????cmdColumn.ShowDeleteButton?=?true;
??????cmdColumn.ShowEditButton?=?true;
??????cmdColumn.ShowSelectButton?=?true;
??????GridView1.Columns.Add(cmdColumn);
??????//定義一個數據綁定列(可復用)
??????BoundField?bndColumn?=?new?BoundField();
??????bndColumn.DataField?=?"EmployeeID";
??????bndColumn.HeaderText?=?"ID";
??????bndColumn.InsertVisible?=?false;
??????bndColumn.ReadOnly?=?true;
??????bndColumn.SortExpression?=?"EmployeeID";
??????//添加一列
??????GridView1.Columns.Add(bndColumn);
??????
??????//重新定義
??????bndColumn?=?new?BoundField();
??????bndColumn.DataField?=?"LastName";
??????bndColumn.HeaderText?=?"Last?Name";
??????bndColumn.SortExpression?=?"LastName";
??????bndColumn.ControlStyle.Width?=?new?Unit("100px");
??????//再添加一列
??????GridView1.Columns.Add(bndColumn);
??????
??????//重新定義
??????bndColumn?=?new?BoundField();
??????bndColumn.DataField?=?"FirstName";
??????bndColumn.HeaderText?=?"First?Name";
??????bndColumn.SortExpression?=?"FirstName";
??????//再添加一列
??????GridView1.Columns.Add(bndColumn);
??????//重新定義
??????bndColumn?=?new?BoundField();
??????bndColumn.DataField?=?"BirthDate";
??????bndColumn.HeaderText?=?"Birth?Date";
??????bndColumn.SortExpression?=?"BirthDate";
??????bndColumn.ControlStyle.Width?=?new?Unit("150px");
??????//再添加一列
??????GridView1.Columns.Add(bndColumn);
??????//重新定義
??????bndColumn?=?new?BoundField();
??????bndColumn.DataField?=?"ReportsTo";
??????bndColumn.HeaderText?=?"Reports?To";
??????bndColumn.SortExpression?=?"ReportsTo";
??????bndColumn.ControlStyle.Width?=?new?Unit("50px");
??????//再添加一列
??????GridView1.Columns.Add(bndColumn);
??????//對SqlDataSource的設置
??????EmployeeList.ID?=?"EmployeeList";
??????EmployeeList.ConnectionString?=
?????????ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
??????EmployeeList.SelectCommand?=?"SELECT?[EmployeeID],?[LastName],?"
?????????+?"[FirstName],?[BirthDate],?[ReportsTo]?FROM?[Employees]";
??????EmployeeList.InsertCommand?=?"INSERT?INTO?[Employees]?([LastName],?"
?????????+?"[FirstName],?[BirthDate],?[ReportsTo])?"
?????????+?"VALUES?(@LastName,?@FirstName,?@BirthDate,?@ReportsTo)";
??????EmployeeList.UpdateCommand?=?"UPDATE?[Employees]?"
?????????+?"SET?[LastName]?=?@LastName,?"
?????????+?"[FirstName]?=?@FirstName,?[BirthDate]?=?@BirthDate,?"
?????????+?"[ReportsTo]?=?@ReportsTo?"
?????????+?"WHERE?[EmployeeID]?=?@original_EmployeeID";
??????EmployeeList.DeleteCommand?=?"DELETE?FROM?[Employees]?"
?????????+?"WHERE?[EmployeeID]?=?@original_EmployeeID";
??????EmployeeList.OldValuesParameterFormatString?=?"original_{0}";
??????EmployeeList.DeleteParameters.Add(
?????????new?Parameter("original_EmployeeID",?System.TypeCode.Int32));
??????EmployeeList.UpdateParameters.Add(
?????????new?Parameter("LastName",?System.TypeCode.String));
??????EmployeeList.UpdateParameters.Add(
?????????new?Parameter("FirstName",?System.TypeCode.String));
??????EmployeeList.UpdateParameters.Add(
?????????new?Parameter("BirthDate",?System.TypeCode.DateTime));
??????EmployeeList.UpdateParameters.Add(
?????????new?Parameter("ReportsTo",?System.TypeCode.Int32));
??????EmployeeList.UpdateParameters.Add(
?????????new?Parameter("original_EmployeeID",?System.TypeCode.Int32));
??????EmployeeList.InsertParameters.Add(
?????????new?Parameter("LastName",?System.TypeCode.String));
??????EmployeeList.InsertParameters.Add(
?????????new?Parameter("FirstName",?System.TypeCode.String));
??????EmployeeList.InsertParameters.Add(
?????????new?Parameter("BirthDate",?System.TypeCode.DateTime));
??????EmployeeList.InsertParameters.Add(
?????????new?Parameter("ReportsTo",?System.TypeCode.Int32));
??????Control?frm?=?this.FindControl("form1");
??????//將這兩個控件添加到form中
??????frm.Controls.Add(EmployeeList);
??????frm.Controls.Add(GridView1);
???}
???//編輯事件
???protected?void?GridView1_RowEditing(
??????object?sender,?GridViewEditEventArgs?e)
???{
??????System.Diagnostics.Debug.WriteLine("GridView1_RowEditing");
??????GridView1.SelectedIndex?=?e.NewEditIndex;
???}
}
把GridView整到dll中去,讓webpartzone可以使用它,其中包含一個可以綁定到數據庫的自定義模板列<sharepoint(有的叫moss有的叫portal,md到底叫啥?)開發者的福音>
using?System.Collections;
using?System.ComponentModel;
using?System.Data;
using?System.Data.SqlClient;
using?System.Web;
using?System.Web.SessionState;
using?System.Web.UI;
using?System.Web.UI.WebControls;
using?System.Web.UI.HtmlControls;
//將會做一個基于webpart的上傳下載控件,由于sharepoint個大垃圾,所有的東西居然都存到數據庫中
//真tmd的大垃圾,微軟必在sharepoint這個不陰不陽的東西上慘敗??2008badnewfish
namespace?uploadFile
{
????/**////?<summary>?
????///?一個在webpart中生成GridView的程序,其中包含一個能綁定到數據庫的模板列。
????///?不錯吧,可能正是你想要的哦?2008badnewfish
????///?</summary>?
????public?class?view?:?System.Web.UI.WebControls.WebParts.WebPart
????{
????????private?GridView?gv?=?new?GridView();
????????
????????protected?override?void?CreateChildControls()
????????{
????????????//去數據庫取數據,別問我什么是using??2008badnewfish
????????????using?(SqlConnection?cn?=?new?SqlConnection())
????????????{
????????????????//此處以Northwind數據庫為例,服務器默認本機的.\sqlexpress,沒有的可以去微軟下載。?2008badnewfish
????????????????cn.ConnectionString?=?@"Data?Source=.\sqlexpress;Initial?Catalog=Northwind;Integrated?Security=True";
????????????????
????????????????SqlCommand?cmd?=?new?SqlCommand();
????????????????cmd.Connection?=?cn;
????????????????cmd.CommandText?=?"select?*?from?employees";
????????????????
????????????????SqlDataAdapter?sda?=?new?SqlDataAdapter(cmd);
????????????????
????????????????DataTable?dt?=?new?DataTable("employees");
????????????????//打開鏈接??2008badnewfish
????????????????cn.Open();
????????????????//使用數據適配器填充datatable,??2008badnewfish
????????????????sda.Fill(dt);
????????????????//申明并初始化模板列??2008badnewfish
????????????????TemplateField?customField?=?new?TemplateField();
????????????????customField.ShowHeader?=?true;
????????????????customField.HeaderText?=?"自定義模板列";
????????????????//對模板列中的項進行定義,查查類庫定義就會明白,這是一個需要實現接口的類??2008badnewfish
????????????????customField.ItemTemplate?=?new?GridViewTemplate(DataControlRowType.DataRow,?"");
????????????????//把這模板列添加到GridView中去??2008badnewfish
????????????????gv.Columns.Add(customField);
????????????????gv.DataSource?=?dt;
????????????????//先要添加RowDataBound事件再執行DataBind()方法??2008badnewfish
????????????????//此事件用來建立數據項??2008badnewfish
????????????????gv.RowDataBound?+=?new?GridViewRowEventHandler(gv_RowDataBound);
????????????????gv.DataBind();
????????????????
????????????}
????????????//別忘了把GridView添加到webpart中??2008badnewfish
????????????this.Controls.Add(gv);
????????????
????????}
????????//這個事件很重要??2008badnewfish
????????void?gv_RowDataBound(object?sender,?GridViewRowEventArgs?e)
????????{
????????????if?(e.Row.RowType?==?DataControlRowType.DataRow)
????????????{
????????????????DataRowView?gv?=?(DataRowView)e.Row.DataItem;
????????????????Label?label1?=?(Label)e.Row.FindControl("lb");
????????????????//gv.Row["firstname"]就是你想綁定的數據庫中的列??2008badnewfish
????????????????label1.Text?=?gv.Row["firstname"].ToString();
????????????????
????????????}
????????}
????}
????//此類借鑒網上高人的大作,結構清晰無需太多注釋??2008badnewfish
????public?class?GridViewTemplate?:?ITemplate
????{
????????private?DataControlRowType?templateType;
????????private?string?columnName;
????????public?GridViewTemplate(DataControlRowType?type,?string?colname)
????????{
????????????templateType?=?type;
????????????columnName?=?colname;
????????}
????????public?void?InstantiateIn(System.Web.UI.Control?container)
????????{
????????????switch?(templateType)
????????????{
????????????????case?DataControlRowType.Header:
????????????????????Literal?lc?=?new?Literal();
????????????????????lc.Text?=?columnName;
????????????????????container.Controls.Add(lc);
????????????????????break;
????????????????case?DataControlRowType.DataRow:
????????????????????Label?lb?=?new?Label();
????????????????????lb.ID?=?"lb";
????????????????????lb.Text?=?"hear";
????????????????????container.Controls.Add(lb);
????????????????????break;
????????????????default:
????????????????????break;
????????????}
?????????
????????}
????}
}
Question3:后臺實現GridView分頁
要點:將GridView數據綁定寫到一個獨立的方法中
using?System.Collections;
using?System.Configuration;
using?System.Data;
using?System.Web;
using?System.Web.Security;
using?System.Web.UI;
using?System.Web.UI.HtmlControls;
using?System.Web.UI.WebControls;
using?System.Web.UI.WebControls.WebParts;
using?System.Data.SqlClient;
public?partial?class?badnewfish?:?System.Web.UI.Page
{
????protected?void?Page_Load(object?sender,?EventArgs?e)
????{
????}
????private?void?GridViewDataBind()
????{
????????DataTable?dt?=?new?DataTable("badnewfish");
????????using?(SqlConnection?cn?=?new?SqlConnection())
????????{
????????????cn.ConnectionString?=?@"Data?Source=.\sqlexpress;Initial?Catalog=Northwind;Integrated?Security=True";
????????????SqlCommand?cmd?=?new?SqlCommand();
????????????cmd.Connection?=?cn;
????????????cmd.CommandText?=?"select?*?from?Orders";
????????????SqlDataAdapter?sda?=?new?SqlDataAdapter(cmd);
????????????cn.Open();
????????????sda.Fill(dt);
????????}
????????GridView1.DataSource?=?dt;
????????GridView1.AllowPaging?=?true;
????????GridView1.DataBind();
????}
????protected?void?Button1_Click(object?sender,?EventArgs?e)
????{
????????GridViewDataBind();
????}
????protected?void?GridView1_PageIndexChanging(object?sender,?GridViewPageEventArgs?e)
????{
????????GridView1.PageIndex?=?e.NewPageIndex;
????????GridViewDataBind();
???????
????}
}
轉載于:https://www.cnblogs.com/ahjxxy/archive/2009/10/15/1583876.html
總結
以上是生活随笔為你收集整理的sharepoint中一些gridview的使用方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: matlab 反激变压器,基于matla
- 下一篇: MySQL怎么给表简明_科学网—mySQ