C# 根据条件将GridView列一分为二
我有一个C# 根据条件将GridView列一分为二,c#,asp.net,gridview,data-binding,sqldatasource,C#,Asp.net,Gridview,Data Binding,Sqldatasource,我有一个GridView,只有SqlDataSource。在我的表中,有两种类型的数据存储在一列中。第一,在我校发行图书,第二,向其他分公司发行图书。 我需要分开一列,如上图所示 这是我现在的代码 <asp:GridView ID="gvJournalReg" runat="server" DataSourceID="sdsJournalReg" AutoGenerateColumns="False" DataKeyName
GridView
,只有SqlDataSource
。在我的表中,有两种类型的数据存储在一列中。第一,在我校发行图书,第二,向其他分公司发行图书。我需要分开一列,如上图所示
这是我现在的代码
<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/>");
}
受保护的字符串GetDistribution(对象dataItem)
{
string distribution=DataBinder.Eval(dataItem,“distribution”).ToString();
返回分配。替换(“,”,“
”);
}
我很高兴听到任何建议 我通过在
HeaderTemplate
和ItemTemplate
中添加一个表解决了这个问题。同样在代码隐藏中,我通过发送必要的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/>");
}
受保护的字符串GetStringWithBreaks(对象数据项,字符串表达式)
{
string distribution=DataBinder.Eval(dataItem,expression).ToString();
返回分配。替换(“,”,“
”);
}