在c#.net中的两个网格中拆分Gridview

时间:2016-07-29 00:13:43

标签: c# asp.net gridview

我有GridViewSQL Server数据库获取数据。

GridView当用户从CalendarExtender选择日期时绑定,因为数据在某一天与另一天不同,也是行数量。

电子。 G.,在星期六,GridView充满了18行。周二,58岁。

关注:

我需要做的是将GridView拆分为2个部分(2 GridViews)。 E. G.,在星期二,每个GridView 29行,星期六,每行9行。

我试过了:

将每日数据转换为GridView,称为“GVTotal”:

if (Weekday.Value == "Saturday")
            {
                GVTotal.DataSourceID = SaturdayData.ID;
                GVTotal.DataBind();
            }

从GVTotal计算行数,并除以2.

int everything = GVTotal.Rows.Count;
            int half = everything / 2;

我现在要做的是将行从0复制到halfGVPart1,从half复制到everything到{{1} },与GVPart2中的顺序完全相同。

我已经读过,使用GVTotal可能会使这成为可能。 我不太清楚该怎么做。有人可以帮帮我吗?

2 个答案:

答案 0 :(得分:1)

你可以有一个带有两个项目的Repeater,每个GridView一个。在下面的示例中,Repeater呈现为具有单行的表。每个GridView都在该行的单元格中,它们之间有一个空单元格。

<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
    <HeaderTemplate>
        <table cellspacing="0" cellpadding="0">
            <tr>
    </HeaderTemplate>
    <ItemTemplate>
        <td>
            <asp:GridView ID="gvHalf" runat="server" >
                ...
            </asp:GridView>
        </td>
    </ItemTemplate>
    <SeparatorTemplate>
        <td style="width: 32px;" />
    </SeparatorTemplate>
    <FooterTemplate>
            </tr> 
        </table>
    </FooterTemplate>
</asp:Repeater>

为了获得两个数据源,您在类中声明了两个DataTable:

private DataTable dtTopHalf;
private DataTable dtBottomHalf;

Page_Load中,通过将完整的DataTable分成两部分来填充两个DataTable,并将Repeater的数据源设置为两个布尔值:

void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        using (SqlConnection conn = new SqlConnection("Data Source=(local); Integrated Security = True; Initial Catalog=TestMP"))
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Clients ORDER BY ClientID ASC", conn))
        {
            cmd.CommandType = CommandType.Text;
            SqlDataAdapter dataAdapter = new SqlDataAdapter(cmd);
            DataTable dt = new DataTable();
            dataAdapter.Fill(dt);
            int halfCount = dt.Rows.Count / 2;
            dtTopHalf = dt.AsEnumerable().Select(x => x).Take(halfCount).CopyToDataTable();
            dtBottomHalf = dt.AsEnumerable().Select(x => x).Skip(halfCount).CopyToDataTable();
        }

        // Each value in the Repeater indicates if it is the top half or not
        repeater1.DataSource = new List<bool>() { true, false };
        repeater1.DataBind();
    }
}

然后可以为Repeater的ItemDataBound事件处理程序中的每个GridView设置特定数据源:

protected void repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        bool isTopHalf = (bool)e.Item.DataItem;
        GridView gvHalf = e.Item.FindControl("gvHalf") as GridView;
        gvHalf.DataSource = isTopHalf ? dtTopHalf : dtBottomHalf;
        gvHalf.DataBind();
    }
}

注1 :Repeater允许为两个GridView共享相同的标记。如果您愿意,可以在标记中声明两个单独的GridView,并将特定数据源应用于Page_Load中的每个。

注意2 :您需要在项目中引用System.Data.DataSetExtensions以使用上面提到的一些LINQ方法。

答案 1 :(得分:0)

感谢ConnorsFan的帮助。他的回答是做我想做的正确方法。

由于我需要在数据输入之前选择日期,因此我将Connor的代码写入Textbox OnTextChanged事件的if (Weekday.Value == "<day of the week>")语句中。

这是解决方案:

ASPX:

更新:仅推荐使用一个GridView:

<asp:Repeater ID="repeater1" runat="server" OnItemDataBound="repeater1_ItemDataBound">
                <HeaderTemplate>
                    <table cellspacing="200px" cellpadding="0">
                        <tr style="vertical-align: top;">
                </HeaderTemplate>
                <ItemTemplate>
                    <td>
                        <asp:GridView ID="gvHalf" runat="server" BackColor="White" AutoGenerateColumns="False" HeaderStyle-CssClass="tituloshandoff" RowStyle-CssClass="contenidohandoffbatch">
                            <Columns>
                                <asp:BoundField HeaderText="IDBATCH" DataField="IDBatch" SortExpression="IDBatch" HeaderStyle-CssClass="TituloInvisible" ItemStyle-CssClass="TituloInvisible" />
                                <asp:BoundField HeaderText="BATCH" DataField="Nombre" SortExpression="Nombre" />
                                <asp:BoundField HeaderText="DEALER" DataField="DealerCodigo" SortExpression="DealerCodigo" />
                                <asp:BoundField HeaderText="CT TIME" DataField="CTStart" SortExpression="CTStart" />
                                <asp:BoundField HeaderText="STATUS" DataField="Estado" SortExpression="Estado" />
                                <asp:TemplateField HeaderText="CONTROL">
                                    <ItemTemplate>
                                        <asp:Button ID="Button1" CssClass="botongrid" runat="server" Text="Select" Width="100px" /></ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </ItemTemplate>
                <SeparatorTemplate>
                    <td style="width: 100px;" />
                </SeparatorTemplate>
                <FooterTemplate>
                    </tr> 
        </table>
                </FooterTemplate>
            </asp:Repeater>

C#:

我将在示例中仅显示一天:

string LaConexion = @"<My Connection String>";
    private DataTable dtTopHalf;
    private DataTable dtBottomHalf;

    protected void TextDate_TextChanged(object sender, EventArgs e)
    {
        if (diasemana.Value == "Monday")
        {
            using (SqlConnection conexion = new SqlConnection(LaConexion))
            using (SqlCommand comando = new SqlCommand("SELECT [1Monday].IDBatch, Batch.Nombre, Dealer.DealerCodigo, Batch.CTStart, BatchDatos.Estado FROM [1Monday] INNER JOIN Batch ON [1Monday].IDBatch = Batch.IDBatch INNER JOIN Dealer ON Batch.IDDealer = Dealer.IDDealer LEFT OUTER JOIN BatchDatos ON [1Monday].ID = BatchDatos.ID ORDER BY Batch.CTStart", conexion))
            {
                comando.CommandType = CommandType.Text;
                SqlDataAdapter dataAdapter = new SqlDataAdapter(comando);
                DataTable dt = new DataTable();
                dataAdapter.Fill(dt);
                int halfCount = dt.Rows.Count / 2;
                dtTopHalf = dt.AsEnumerable().Select(x => x).Take(halfCount).CopyToDataTable();
                dtBottomHalf = dt.AsEnumerable().Select(x => x).Skip(halfCount).CopyToDataTable();
            }
            // Each value in the Repeater indicates if it is the top half or not
            repeater1.DataSource = new List<bool>() { true, false };
            repeater1.DataBind();
        }
    } 

A Picture:

enter image description here