sharepoint中一些gridview的使用方法

Question1 :将数据绑定到 GridView
Question2
:如何向 GridView 中加入格式化的超链接?

答: step1:在GridView中新建一个模板列。
       step2
:向新加的模板列中添加一个HyperLink控件。
       step3
:摄制组HyperLinkNavigateUrl值为'<%#"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.Web.UI.WebControls;
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(0xDE0xBA0x84);
      GridView1.BorderColor 
= Color.FromArgb(0xDE0xBA0x84);
      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(0xFF0xF70xE7);
      GridView1.RowStyle.ForeColor 
= Color.FromArgb(0x8C0x450x10);
      GridView1.SelectedRowStyle.BackColor 
         
= Color.FromArgb(0x730x8A0x9C);
      GridView1.SelectedRowStyle.Font.Bold 
= true;
      GridView1.SelectedRowStyle.ForeColor 
= Color.White;
      GridView1.PagerStyle.ForeColor 
= Color.FromArgb(0x8C0x450x10);
      GridView1.PagerStyle.HorizontalAlign 
= HorizontalAlign.Center;
      GridView1.HeaderStyle.BackColor 
= Color.FromArgb(0xA50x510x29);
      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;
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;
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();
       
    }

}

你可能感兴趣的:(SharePoint)