using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Web.UI.WebControls;
using Microsoft.SharePoint.WebControls;
using System.Data;
namespace John.Research.SharePoint.Webparts
{
[Guid("95438EB1-D8DA-41df-AD72-92EAD87B2A76")]
public class SPGridViewDisplayerPart : System.Web.UI.WebControls.WebParts.WebPart
{
private Label lbl_display;
ObjectDataSource MyDatasource1;
private Table UITable;
private TextBox searchText;
private Button searchBut;
private Button groupButton;
private SPGridView SPGridView1;
private SPGridViewPager SPGridViewPager1;
private int pageSize = 3;
protected override void CreateChildControls()
{
base.CreateChildControls();
this.CreateMessage();
this.CreateUI();
this.CreateCommandUI();
this.CreateDisplayUI();
// this.BindData();
}
private void CreateMessage()
{
this.lbl_display = new Label();
this.Controls.Add(this.lbl_display);
}
private void CreateUI()
{
this.UITable = new Table();
TableRow row = new TableRow();
TableCell cell = new TableCell();
row.Cells.Add(cell);
this.UITable.Rows.Add(row);
row = new TableRow();
cell = new TableCell();
row.Cells.Add(cell);
this.UITable.Rows.Add(row);
this.Controls.Add(this.UITable);
}
private void CreateCommandUI()
{
this.searchText = new TextBox();
this.searchBut = new Button();
this.searchBut.Text = "查询";
this.searchBut.Click += new EventHandler(searchBut_Click);
this.groupButton = new Button();
this.groupButton.Text = "分组";
this.groupButton.Click += new EventHandler(groupButton_Click);
TableCell cell = this.UITable.Rows[0].Cells[0];
cell.Controls.Add(this.searchText);
cell.Controls.Add(this.searchBut);
cell.Controls.Add(this.groupButton);
}
void searchBut_Click(object sender, EventArgs e)
{
ViewState["searchParam"] = this.searchText.Text;
this.SPGridView1.DataBind();
}
void groupButton_Click(object sender, EventArgs e)
{
if( ViewState["MyGirdAllowGroup"]==null)
{
this.SPGridView1.AllowGrouping = true;
this.SPGridView1.AllowGroupCollapse = true;
SPGridView1.GroupField = "Title";
SPGridView1.GroupDescriptionField = "Title";
SPGridView1.GroupFieldDisplayName = "分组:";
ViewState["MyGirdAllowGroup"] = "MyGirdAllowGroup";
}
else
{
this.SPGridView1.AllowGrouping = false;
ViewState["MyGirdAllowGroup"] = null;
}
this.SPGridView1.DataBind();
}
private void CreateDisplayUI()
{
try
{
MyDatasource1 = new ObjectDataSource();
MyDatasource1.ID = "MyDatasource1";
MyDatasource1.TypeName = this.GetType().FullName + "," + this.GetType().Assembly.FullName;
MyDatasource1.SelectMethod = "GetDataTable";
Parameter param1=new Parameter("id",TypeCode.String);
MyDatasource1.SelectParameters.Add(param1);
MyDatasource1.Selecting += new ObjectDataSourceSelectingEventHandler(MyDatasource1_Selecting);
this.Controls.Add(MyDatasource1);
this.SPGridView1 = new SPGridView();
this.SPGridView1.DataSourceID = "MyDatasource1";
this.SPGridView1.AutoGenerateColumns = false;
this.SPGridView1.ID = "SPGridView1";
this.SPGridView1.EnableViewState = false;
this.SPGridViewPager1 = new SPGridViewPager();
this.SPGridViewPager1.GridViewId = "SPGridView1";
//???
SPGridView1.FilteredDataSourcePropertyFormat = "{1}='{0}'";
SPGridView1.FilteredDataSourcePropertyName = "FilterExpression";
TableCell displayCell = this.UITable.Rows[1].Cells[0];
displayCell.Controls.Add(this.SPGridView1);
displayCell.Controls.Add(this.SPGridViewPager1);
//排序
this.SPGridView1.AllowSorting = true;
//分页
this.SPGridView1.AllowPaging = true;
this.SPGridView1.PageSize = this.pageSize;
//绑定列
SPBoundField col = new SPBoundField();
col.DataField = "Title";
col.SortExpression = "Title";
col.HeaderText = "Title";
SPGridView1.Columns.Add(col);
col = new SPBoundField();
col.DataField = "Author";
col.SortExpression = "Author";
col.HeaderText = "Author";
SPGridView1.Columns.Add(col);
this.SPGridView1.AllowFiltering = true;
this.SPGridView1.FilterDataFields = "Title,Author";
}
catch (Exception ex)
{
this.lbl_display.Text = ex.Message + "/r/n" + ex.Source + "/r/n" + ex.StackTrace;
}
}
void MyDatasource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
e.InputParameters[0] = ViewState["searchParam"];
}
private void BindData()
{
//绑定数据源
this.SPGridView1.DataSourceID = "MyDatasource1";
this.SPGridView1.DataBind();
}
public DataTable GetDataTable(string id)
{
DataTable tblData = new DataTable();
tblData.Columns.Add("Title");
tblData.Columns.Add("Author");
//tblData.Columns.Add("Rating");
if (!string.IsNullOrEmpty(id))
{
string tick = DateTime.Now.Ticks.ToString();
tblData.Rows.Add("Obsession" + id, "Robards, Karen" + tick);
tblData.Rows.Add("Obsession" + id, "Robards, Karen" + tick);
tblData.Rows.Add("Vanished" + id, "Robards, Karen" + tick);
tblData.Rows.Add("Magician: Apprentice" + id, "Feist, Raymong E." + tick);
tblData.Rows.Add("Magician: Master" + id, "Feist, Raymong E." + tick);
tblData.Rows.Add("Silverthorn" + id, "Feist, Raymong E." + tick);
tblData.Rows.Add(@"Lord Foul‘s Bane" + id, "Donaldson, Stephen R." + tick);
tblData.Rows.Add("The Illearth War" + id, "Donaldson, Stephen R." + tick);
}
return tblData;
}
}
}