为感谢烈火の炎的无私热心帮助下才能实现这个功能,特别整理出来给大家分享一下。
1、WCFService.svc
public class WCFService : IWCFClass //这个是引用新增的一个类文件IWCFClass.cs,分页在这个类里。
{
//一级表
[OperationContract]
public List<PageBj> PageBjFilter(int? PageSize, int PageIndex, string filter)
{
PageBj data = new PageBj();
List<PageBj> sources = data.CreatePageSources(PageSize, PageIndex, filter);
return sources.ToList();
}
[OperationContract]
public int PageBjCount(string filter)
{
string strWhere = "";
if (filter != "")
{
strWhere = " 1=1 AND " + filter;
}
else
{
strWhere = " 1=1 ";
}
int Bjcount = int.Parse(MssqlHelper.ExecuteScalar("SELECT COUNT(1) FROM View_bj where " + strWhere + "").ToString());
return Bjcount;
}
//二级表
[OperationContract]
public List<BjDetailInfo> GetBjDetailInfo(int bj_id)
{
List<BjDetailInfo> DetectionList = new List<BjDetailInfo>();
string strsql = @"SELECT brand.brand_name, bj_detection.id, bj_detection.bz_name, bj_detection.brand_id, bj_detection.bj_id,bj_detection.brand_price, bj_detection.temp_id, bj_detection.shop_price FROM bj_detection LEFT OUTER JOIN brand ON bj_detection.brand_id = brand.id where bj_id=" + bj_id + " order by bj_detection.brand_id desc";
DataTable dt = MssqlHelper.ExecuteDataTable(strsql);
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
DetectionList.Add(new BjDetailInfo()
{
brand_name = dr["brand_name"].ToString(),
bz_id = dr["id"].ToString(),
bz_name = dr["bz_name"].ToString(),
brand_id = dr["brand_id"].ToString(),
bj_id = dr["bj_id"].ToString(),
brand_price = dr["brand_price"].ToString(),
shop_price = dr["shop_price"].ToString(),
temp_id = dr["temp_id"].ToString()
});
}
}
return DetectionList;
}
public class BjDetailInfo
{
public string brand_name { get; set; }
public string bz_id { get; set; }
public string bz_name { get; set; }
public string brand_id { get; set; }
public string bj_id { get; set; }
public string brand_price { get; set; }
public string temp_id { get; set; }
public string shop_price { get; set; }
}
}
2、IWCFClass.cs //新增的自定义类文件
public class IWCFClass
{
public class PageBj
{
public string Id { get; set; }
public string bj_name { get; set; }
public string bj_Sample { get; set; }
public string cdate { get; set; }
public string bj_remarks { get; set; }
public string cId { get; set; }
public string cName { get; set; }
public List<BjDetailInfo> BjDetailInfo { get; set; } //二级表
public List<PageBj> CreatePageSources(int? PageSize, int PageIndex, string filter)
{
List<PageBj> entitys = new List<PageBj>();
string strWhere = filter;
string strSort = "id desc";
string strTable = "View_bj";
DataSet ds = new DataSet();
int Count = 0;
int tempPageSize = 0;
if (PageSize.HasValue)
{
tempPageSize = PageSize.Value;
}
ds = MssqlHelper.GetList(tempPageSize, PageIndex, strTable, strWhere, strSort, out Count);
DataTable dt = ds.Tables[0];
if (dt.Rows.Count > 0)
{
foreach (DataRow dr in dt.Rows)
{
entitys.Add(new PageBj()
{
Id = dr["Id"].ToString(),
bj_name = dr["bj_name"].ToString(),
bj_Sample = dr["bj_Sample"].ToString(),
cdate = dr["cdate"].ToString(),
bj_remarks = dr["bj_remarks"].ToString(),
cId = dr["cId"].ToString(),
cName = dr["cName"].ToString()
});
}
}
else
{
entitys = null;
}
return entitys;
}
}
}
3、BjList.xaml
<sdk:DataGrid Margin="10,6,16,0" Name="dataGrid1" AutoGenerateColumns="False" RowDetailsVisibilityChanged="dataGrid1_LoadingRowDetails" RowDetailsVisibilityMode="VisibleWhenSelected" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Header="编号" IsReadOnly="True" Binding="{Binding Id,Mode=OneWay}" />
<sdk:DataGridTextColumn Header="分类" IsReadOnly="True" Binding="{Binding cName,Mode=OneWay}" />
<sdk:DataGridTextColumn Header="项目" IsReadOnly="True" Binding="{Binding bj_name,Mode=OneWay}" />
</sdk:DataGrid.Columns>
<sdk:DataGrid.RowDetailsTemplate>
<DataTemplate>
<sdk:DataGrid Name="dataGridDetail1" AutoGenerateColumns="False" ItemsSource="{Binding BjDetailInfo, Mode=OneWay}" >
<sdk:DataGrid.Columns>
<sdk:DataGridTextColumn Width="100"
Header="二级字段A"
Binding="{Binding brand_name,Mode=OneWay}"/>
<sdk:DataGridTextColumn Width="100"
Header="二级字段B"
Binding="{Binding bz_name,Mode=OneWay}"/>
<sdk:DataGridTextColumn Width="100"
Header="二级字段C"
Binding="{Binding brand_price,Mode=OneWay}"/>
<sdk:DataGridTextColumn Width="100"
Header="二级字段D"
Binding="{Binding shop_price,Mode=OneWay}"/>
</sdk:DataGrid.Columns>
</sdk:DataGrid>
</DataTemplate>
</sdk:DataGrid.RowDetailsTemplate>
</sdk:DataGrid>
<sdk:DataPager Height="26" Name="dataPager1" PageSize="10" Width="200" PageIndexChanged="dataPager1_PageIndexChanged" HorizontalAlignment="Left" Margin="10,10,0,0"/>
4、BjList.xaml.cs文件:
IWCFClassPageBj fi = new IWCFClassPageBj();
WCFServiceClient client = new WCFServiceClient();
public BjList()
{
Filter(wherestr);
}
#region 分页事件
private void dataPager1_PageIndexChanged(object sender, EventArgs e)
{
Filter(wherestr);
}
private void Filter(string wherestr)
{
client.PageBjCountCompleted += new EventHandler<PageBjCountCompletedEventArgs>(client_PageBjCountCompleted);
client.PageBjCountAsync(wherestr);
}
void client_PageBjCountCompleted(object sender, PageBjCountCompletedEventArgs e)
{
if (e.Result < 1)
return;
SetPageSource(e.Result);
var pageIndex = dataPager1.PageIndex + 1;
client.PageBjFilterCompleted += new EventHandler<PageBjFilterCompletedEventArgs>(client_PageBjFilterCompleted);
client.PageBjFilterAsync(dataPager1.PageSize, pageIndex, wherestr);
}
void client_PageBjFilterCompleted(object sender, PageBjFilterCompletedEventArgs e)
{
dataGrid1.ItemsSource = e.Result;
}
private void SetPageSource(int resultCount)
{
if (dataPager1.Source != null && (dataPager1.Source as PagedCollectionView).TotalItemCount == resultCount)
return;
List<int> source = new List<int>();
for (int i = 0; i < resultCount; i++)
{
source.Add(i);
}
PagedCollectionView pagedCollection = new PagedCollectionView(source);
dataPager1.Source = pagedCollection;
}
#endregion
private void dataGrid1_LoadingRowDetails(object sender, DataGridRowDetailsEventArgs e)
{
fi = e.Row.DataContext as IWCFClassPageBj;
client.GetBjDetailInfoCompleted += new EventHandler<GetBjDetailInfoCompletedEventArgs>(client_GetBjDetailInfoCompleted);
client.GetBjDetailInfoAsync(int.Parse(fi.Id));
}
void client_GetBjDetailInfoCompleted(object sender, GetBjDetailInfoCompletedEventArgs e)
{
fi.BjDetailInfo = e.Result as ObservableCollection<BjDetailInfo>;
}
服务端分页方法其实可以只读一次就可以的,只是我们这次讨论的是详细信息的动态列表绑定,就不说这个了。再次感谢烈火の炎的技术支持,向烈火の炎学习,希望能帮得上有需要的人。