silverlight 3 数据绑定及分页
前台页面
代码
<StackPanel Orientation="Vertical">
<data:DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="4" AutoGenerateColumns="False" HeadersVisibility="Column" GridLinesVisibility="Horizontal" RowBackground="Cornsilk" AlternatingRowBackground="LemonChiffon" RowHeight="30" IsReadOnly="True" CanUserResizeColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="标题" Binding="{Binding Title}" Width="350" ></data:DataGridTextColumn>
<data:DataGridTextColumn Header="作者" Binding="{Binding Author}" Width="150" ></data:DataGridTextColumn>
</data:DataGrid.Columns>
</data:DataGrid>
<data:DataPager x:Name="datapager1" Height="24" Margin="8,0,8,40" VerticalAlignment="Bottom" Background="#FFD7E2EE" FontSize="13.333" Width="304" PageIndexChanged="datapager1_PageIndexChanged"/>
</StackPanel>
<data:DataGrid x:Name="dataGrid" HorizontalAlignment="Stretch" VerticalAlignment="Top" Margin="4" AutoGenerateColumns="False" HeadersVisibility="Column" GridLinesVisibility="Horizontal" RowBackground="Cornsilk" AlternatingRowBackground="LemonChiffon" RowHeight="30" IsReadOnly="True" CanUserResizeColumns="False">
<data:DataGrid.Columns>
<data:DataGridTextColumn Header="标题" Binding="{Binding Title}" Width="350" ></data:DataGridTextColumn>
<data:DataGridTextColumn Header="作者" Binding="{Binding Author}" Width="150" ></data:DataGridTextColumn>
</data:DataGrid.Columns>
</data:DataGrid>
<data:DataPager x:Name="datapager1" Height="24" Margin="8,0,8,40" VerticalAlignment="Bottom" Background="#FFD7E2EE" FontSize="13.333" Width="304" PageIndexChanged="datapager1_PageIndexChanged"/>
</StackPanel>
后台页面代码:
代码
int PageSize = 10;//设定分页大小
List<int> itemCount = new List<int>();//用于DataPager的数据提供
public DataBaseOperate()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("",PageSize, 1);
webClient.GetsimpleListCompleted+=new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
GeneralDataPagerContent();//生成DataPager的数据提供
}
void webClient_GetsimpleListCompleted(object sender, GetsimpleListCompletedEventArgs e)
{
if (e.Error == null)
{
dataGrid.ItemsSource = e.Result;
}
}
void GeneralDataPagerContent()
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetTotalPagersAsync("",PageSize);
webClient.GetTotalPagersCompleted +=
new EventHandler<GetTotalPagersCompletedEventArgs>(webClient_GetTotalPagersCompleted);
}
void webClient_GetTotalPagersCompleted(object sender, GetTotalPagersCompletedEventArgs e)
{
int totalpagers = e.Result;
for (int i = 1; i <= totalpagers; i++) itemCount.Add(i);
PagedCollectionView pcv = new PagedCollectionView(itemCount);
pcv.PageSize = 1;
datapager1.Source = pcv;
}
void client_GetsimpleListCompleted(object sender, TerryLee.SilverlightDemo2.DataBaseOperateServiceReference.GetsimpleListCompletedEventArgs e)
{
if (e.Error == null)
{
dataGrid.ItemsSource = e.Result;
}
}
private void datapager1_PageIndexChanged(object sender, EventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("",PageSize, datapager1.PageIndex + 1);//索引值从0开始,所以要加1
webClient.GetsimpleListCompleted += new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
//Binding(datapager1.PageIndex+2);
}
private void datapager1_PageIndexChanging(object sender, System.ComponentModel.CancelEventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("", PageSize, datapager1.PageIndex + 1);//索引值从0开始,所以要加1
webClient.GetsimpleListCompleted += new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
}
List<int> itemCount = new List<int>();//用于DataPager的数据提供
public DataBaseOperate()
{
InitializeComponent();
}
private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("",PageSize, 1);
webClient.GetsimpleListCompleted+=new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
GeneralDataPagerContent();//生成DataPager的数据提供
}
void webClient_GetsimpleListCompleted(object sender, GetsimpleListCompletedEventArgs e)
{
if (e.Error == null)
{
dataGrid.ItemsSource = e.Result;
}
}
void GeneralDataPagerContent()
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetTotalPagersAsync("",PageSize);
webClient.GetTotalPagersCompleted +=
new EventHandler<GetTotalPagersCompletedEventArgs>(webClient_GetTotalPagersCompleted);
}
void webClient_GetTotalPagersCompleted(object sender, GetTotalPagersCompletedEventArgs e)
{
int totalpagers = e.Result;
for (int i = 1; i <= totalpagers; i++) itemCount.Add(i);
PagedCollectionView pcv = new PagedCollectionView(itemCount);
pcv.PageSize = 1;
datapager1.Source = pcv;
}
void client_GetsimpleListCompleted(object sender, TerryLee.SilverlightDemo2.DataBaseOperateServiceReference.GetsimpleListCompletedEventArgs e)
{
if (e.Error == null)
{
dataGrid.ItemsSource = e.Result;
}
}
private void datapager1_PageIndexChanged(object sender, EventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("",PageSize, datapager1.PageIndex + 1);//索引值从0开始,所以要加1
webClient.GetsimpleListCompleted += new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
//Binding(datapager1.PageIndex+2);
}
private void datapager1_PageIndexChanging(object sender, System.ComponentModel.CancelEventArgs e)
{
BlogServiceSoapClient webClient = new BlogServiceSoapClient();
webClient.GetsimpleListAsync("", PageSize, datapager1.PageIndex + 1);//索引值从0开始,所以要加1
webClient.GetsimpleListCompleted += new EventHandler<GetsimpleListCompletedEventArgs>(webClient_GetsimpleListCompleted);
}
WebService里面有两个方法
代码
[WebMethod]
public List<m_article> GetsimpleList(string strWhere, int PageSize, int CurrentPage)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
if (PageSize != 0)
strSql.Append(" top " + PageSize);
strSql.Append(" * ");
strSql.Append(" from (");
strSql.Append(" SELECT ROW_NUMBER() OVER (ORDER BY publishdate desc) AS RowNo, ");
strSql.Append(" [BH],[topicbh],[Title],[Author],[PublishDate]");
strSql.Append(" FROM J_Articles ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
strSql.Append(" ) as T ");
if (PageSize != 0)
{
strSql.Append(" where RowNo>");
strSql.Append((CurrentPage - 1) * PageSize);
}
DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
m_article article;
List<m_article> articles = new List<m_article>();
foreach (DataRow row in dt.Rows)
{
article = new m_article();
article.Title = row["title"].ToString();
article.Author = row["author"].ToString();
articles.Add(article);
}
return articles;
}
[WebMethod]//得到总分页数
public int GetTotalPagers(string where,int PageSize)
{
decimal count = 0;
StringBuilder sql = new StringBuilder();
sql.Append("select count(1) from J_Articles ");
if (where.Trim() != "")
{
sql.Append(" where " + where);
}
object obj = DbHelperSQL.GetSingle(sql.ToString());
if (obj != null)
count = decimal.Parse(obj.ToString());
decimal a = count / PageSize;
return int.Parse( Math.Ceiling(a).ToString());
}
public List<m_article> GetsimpleList(string strWhere, int PageSize, int CurrentPage)
{
StringBuilder strSql = new StringBuilder();
strSql.Append("select ");
if (PageSize != 0)
strSql.Append(" top " + PageSize);
strSql.Append(" * ");
strSql.Append(" from (");
strSql.Append(" SELECT ROW_NUMBER() OVER (ORDER BY publishdate desc) AS RowNo, ");
strSql.Append(" [BH],[topicbh],[Title],[Author],[PublishDate]");
strSql.Append(" FROM J_Articles ");
if (strWhere.Trim() != "")
{
strSql.Append(" where " + strWhere);
}
strSql.Append(" ) as T ");
if (PageSize != 0)
{
strSql.Append(" where RowNo>");
strSql.Append((CurrentPage - 1) * PageSize);
}
DataTable dt = DbHelperSQL.Query(strSql.ToString()).Tables[0];
m_article article;
List<m_article> articles = new List<m_article>();
foreach (DataRow row in dt.Rows)
{
article = new m_article();
article.Title = row["title"].ToString();
article.Author = row["author"].ToString();
articles.Add(article);
}
return articles;
}
[WebMethod]//得到总分页数
public int GetTotalPagers(string where,int PageSize)
{
decimal count = 0;
StringBuilder sql = new StringBuilder();
sql.Append("select count(1) from J_Articles ");
if (where.Trim() != "")
{
sql.Append(" where " + where);
}
object obj = DbHelperSQL.GetSingle(sql.ToString());
if (obj != null)
count = decimal.Parse(obj.ToString());
decimal a = count / PageSize;
return int.Parse( Math.Ceiling(a).ToString());
}