如何在2中拆分DataSet以在不同的GridView中显示

时间:2017-03-31 08:09:15

标签: c# asp.net gridview dataset

如何将DataSet分成两半,以便每一半都可以绑定到单独的GridView

目前我在做:

gridviewQc1.DataSource = SelectedQCItems;
gridviewQc1.DataBind();

SelectedQCItems是我的DataSet

我想做什么:

gridviewQc1.DataSource = SelectedQCItemsFirstHalf;
gridviewQc1.DataBind();

gridviewQc2.DataSource = SelectedQCItemsSecondHalf;
gridviewQc2.DataBind();

我试着打电话

var table = SelectedQCItems.Tables[0];
var halfData = table.Select().Take(table.Rows.Count/2);
gridviewQc1.DataSource = halfData;
gridviewQc1.DataBind();

但是在最后一行我得到HttpException

  

在所选数据源上找不到名称为“Criteria”的字段或属性。

我想标题名称不会存储在Table中的每个DataSet个对象中。

我做错了什么,还是应该以不同的方式评价?

GridView的代码:

<asp:GridView ID="gridviewQc1" runat="server" AutoGenerateColumns="false"
    AutoGenerateSelectButton="true" OnRowDataBound="gridviewQc1_RowDataBound"
    OnSelectedIndexChanged="gridviewQc1_SelectedIndexChanged" >
    <Columns>
        <asp:BoundField HeaderText="Criteria" DataField="Criteria" />
        <asp:BoundField HeaderText="Major" DataField="Major" />
        <asp:BoundField HeaderText="Mass" DataField="Mass"/>
    </Columns>
</asp:GridView>

1 个答案:

答案 0 :(得分:1)

我能够使用以下代码执行此操作,该代码创建DataSet的2个副本,然后在将每个条目绑定到2 GridViews之前删除每个副本中的一半条目:

var ds1 = SelectedQCItems.Copy();
var ds2 = SelectedQCItems.Copy();

var lastRowIndex = SelectedQCItems.Tables[0].Rows.Count - 1;
var halfwayIndex = SelectedQCItems.Tables[0].Rows.Count / 2 - 1;

for (int i = lastRowIndex; i > halfwayIndex; i--)
{
    ds1.Tables[0].Rows.RemoveAt(i);
}
ds1.AcceptChanges();

for (int i = halfwayIndex; i > -1; i--)
{
    ds2.Tables[0].Rows.RemoveAt(i);
}
ds2.AcceptChanges();

var one = ds1.Tables[0].Rows.Count;
var two = ds2.Tables[0].Rows.Count;

gridviewQc1.DataSource = ds1;
gridviewQc1.DataBind();

gridviewQc2.DataSource = ds2;
gridviewQc2.DataBind();