将条件

时间:2016-01-26 06:33:43

标签: c# asp.net gridview data-binding sqldatasource

我有GridView且只有SqlDataSource。在我的表中,我有两种类型的数据存储在一列中。首先 - 在我们大学中分发书籍,然后在其他分支机构中分发书籍 enter image description here
我需要将一列分开,如上图所示 这是我现在的代码。

<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/>");
}

很高兴听到任何建议!

1 个答案:

答案 0 :(得分:0)

我通过在HeaderTemplateItemTemplate中添加一个表来解决这个问题。同样在后面的代码中,我通过发送必要的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/>");
    }