用条件将 GridView 列一分为二

Separate GridView column in two with condition

我有一个 GridView 但只有 SqlDataSource。在我的 table 中,我在一列中存储了 2 种类型的数据。首先 - 在我们大学分发书籍,其次 - 向其他分支机构分发书籍。

如上图所示,我需要分隔一栏
这是我现在的代码。

<asp:GridView ID="gvJournalReg"
        runat="server"
        DataSourceID="sdsJournalReg"
        AutoGenerateColumns="False"
        DataKeyNames="idManual"
        OnRowDataBound="gvJournalReg_RowDataBound"
        PageSize="10">
        <Columns>
            <asp:BoundField DataField="editionYear" HeaderText="Year" SortExpression="authors" />
            <asp:BoundField DataField="authors" HeaderText="Author" SortExpression="authors" />
            <asp:TemplateField HeaderText="Distridution">
                <ItemTemplate>
                    <asp:Label ID="lb1" runat="server" Text="<%# GetDistribution(Container.DataItem) %>"> 
                    </asp:Label>
                </ItemTemplate>
                <ItemStyle Wrap="false" />
            </asp:TemplateField>
        </Columns>
        <PagerStyle CssClass="asp-gv-pager" />
        <SelectedRowStyle CssClass="info" />
    </asp:GridView>
    <asp:SqlDataSource ID="sdsJournalReg" runat="server"
        ConnectionString="<%$ ConnectionStrings:bukepConnect %>"
        SelectCommand="Select * from met.GetJournalReg (@idChair) ">
        <SelectParameters>
                <asp:ControlParameter ControlID="ddlChair" Name="idChair"
                PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

和代码隐藏

protected string GetDistribution(object dataItem)
{
    string distribution = DataBinder.Eval(dataItem, "distribution").ToString();
    return distribution.Replace(",", "<br/>");
}

很高兴听到任何建议!

我通过在 HeaderTemplateItemTemplate 中添加 table 解决了这个问题。同样在后面的代码中,我通过发送必要的 srting 来分离选择两种类型的数据。这是我的解决方案:

<asp:GridView ID="gvJournalReg"
        runat="server"
        DataSourceID="sdsJournalReg"
        AutoGenerateColumns="False"
        DataKeyNames="idManual"
        OnRowDataBound="gvJournalReg_RowDataBound"
        PageSize="10">
        <Columns>
            <asp:BoundField DataField="editionYear" HeaderText="Year" 
SortExpression="authors" />
                <asp:BoundField DataField="authors" HeaderText="Author" 
SortExpression="authors" />
            <asp:TemplateField>
                <HeaderTemplate>
                   <table class=""  style="width: 261px">
                        <tr>
                            <th colspan="2" style="text-align: center">Distribution</th>
                        </tr>
                        <tr>
                            <th style="width: 130px; text-align: center">Our uni</th>

                            <th style="width: 130px; text-align: center">Branches</th>

                        </tr>
                    </table>
                </HeaderTemplate>
                <ItemTemplate>
                    <table class="" style="width: 261px">
                        <tr>
                            <td style="width: 130px">
                                <%# GetStringWithBreaks(Container.DataItem, "distribution") %>
                            </td>
                            <td style="width: 130px">
                                <%# GetStringWithBreaks(Container.DataItem, "branchDistribution") %>
                            </td>
                        </tr>
                    </table>
                </ItemTemplate>
                <ItemStyle Wrap="false" />
            </asp:TemplateField>
</Columns>
        <PagerStyle CssClass="asp-gv-pager" />
        <SelectedRowStyle CssClass="info" />
    </asp:GridView>
    <asp:SqlDataSource ID="sdsJournalReg" runat="server"
        ConnectionString="<%$ ConnectionStrings:bukepConnect %>"
        SelectCommand="Select * from met.GetJournalReg (@idChair)">
        <SelectParameters>
            <asp:ControlParameter ControlID="ddlChair" Name="idChair" PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
    </asp:SqlDataSource>

后面的代码:

protected string GetStringWithBreaks(object dataItem, string expression)
    {
        string distribution = DataBinder.Eval(dataItem, expression).ToString();
        return distribution.Replace(",", "<br/>");
    }