【问题标题】:GridView is showing same column twiceGridView 两次显示同一列
【发布时间】:2015-02-10 22:53:57
【问题描述】:

我正在以这种方式截断 GridView 中的 Description 列文本:

 <asp:GridView ID="GridView1" CssClass="truncated" runat="server" AllowPaging="true" BackColor="White" 
                    BorderColor="#CCCCCC" BorderWidth="2px" CellPadding="2" CellSpacing="5" ForeColor="#000066" 
                    GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
                    <RowStyle BackColor="#F7F7F7" />
                    <AlternatingRowStyle BackColor="#E7E7FF" />
                    <FooterStyle BackColor="White" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="White" ForeColor="#000066" 
                        HorizontalAlign="Center" />
                    <SortedAscendingCellStyle BackColor="#F1F1F1" />
                    <SortedAscendingHeaderStyle BackColor="#007DBB" />
                    <SortedDescendingCellStyle BackColor="#CAC9C9" />
                    <SortedDescendingHeaderStyle BackColor="#00547E" />
                    <Columns>
                      <asp:CommandField ShowSelectButton="true" ControlStyle-ForeColor="Red" SelectText="Select" HeaderText="Select" />

                      <asp:TemplateField HeaderStyle-ForeColor="White" HeaderText="Description">

                      <ItemTemplate>
                      <div style="overflow:hidden;text-overflow:ellipsis;white-space:nowrap;width:100px">
                       <asp:Label ID="review" runat="server" Text='<%# Bind("Description") %>' Tooltip='<%#Bind("Description")%>'></asp:Label>
                      </div>
                      </ItemTemplate>
                      </asp:TemplateField>

                    </Columns>

 </asp:GridView>

现在的问题是Book tabel 有 10 列,如果我像这样从数据库中选择所有列:

SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Book", con);
    DataSet ds = new DataSet();
    da.Fill(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();

Description 列在 GridView 中显示了两次。第一个Description 显示带有工具提示的截断文本,第二个Description 显示全文。

我知道我在 GridView 中 Bind-ing 它并在后面代码中的 SELECT 命令中进行选择,但我只需要浏览器上 GridView 中带有工具提示的那个。

【问题讨论】:

    标签: c# asp.net gridview


    【解决方案1】:

    您必须向 Gridview 添加属性:

    Autogeneratecolumns=false
    

    这将解决您的问题。

    以下是属性:

    http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.autogeneratecolumns(v=vs.110).aspx

    如下:

    <asp:GridView ID="GridView1" AutoGenerateColumns="false" CssClass="truncated" runat="server" AllowPaging="true" BackColor="White" 
                        BorderColor="#CCCCCC" BorderWidth="2px" CellPadding="2" CellSpacing="5" ForeColor="#000066" 
                        GridLines="None" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
    

    【讨论】:

    • 通过使用 Autogeneratecolumns,您必须定义要显示的每一列(在 部分中) - 这可能是可取的,只是说。
    • 是的,他必须定义他想在项目模板和行数据绑定中显示的每一列
    【解决方案2】:

    我的问题是我在代码中以编程方式设置列,所以我绑定了已经设置的列。解决方案是在像这样设置它们之前检查列是否存在:

    if (gvViewStudents.Columns.Count <= 0) 
    

    这解决了我的列显示两次的问题。

    【讨论】:

      猜你喜欢
      • 2012-12-28
      • 1970-01-01
      • 1970-01-01
      • 2019-04-11
      • 2015-01-09
      • 1970-01-01
      • 2013-10-03
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多