2011-03-23 85 views
1

我一直在尋找一個答案,並沒有找到我在找什麼。我有一個5列的GridView。其中一列是用「 」代替「」的非常長的字符串。我需要能夠固定列的寬度,並使字符包裝處理字符串直到它結束。我已經嘗試了gridview上的所有屬性來獲得我需要的東西,但跨度總是橫向伸展並且從不包裝。這裏是我的GridView控件代碼修復Gridview與Word Wrap的寬度列

  <asp:GridView ID="resultsGrid" AutoGenerateColumns="False" runat="server" AllowPaging="True" 
      AllowSorting="True" PageSize="20" OnPageIndexChanging="gridView_PageIndexChanging" 
      OnSorting="gridView_Sorting" PagerSettings-Position="TopAndBottom" PagerStyle-HorizontalAlign="Center"> 
      <PagerSettings Position="TopAndBottom" /> 
      <Columns> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <%# Container.DataItemIndex + 1 + "." %> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField Visible="false" SortExpression="record_id"> 
        <ItemTemplate> 
         <asp:Label ID="lblRecordID" runat="server" Text='<%# Bind("RecordID") %>' Visible="false"></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Note Type" SortExpression="business_content_type_cd"> 
        <ItemTemplate> 
         <asp:Label ID="lblNoteType" runat="server" Text='<%# Bind("NoteType") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Author" SortExpression="author_user_name"> 
        <ItemTemplate> 
         <asp:Label ID="lblAuthor" runat="server" Text='<%# Bind("Author") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField HeaderText="Date" SortExpression="content_dttm"> 
        <ItemTemplate> 
         <asp:Label ID="lblDate" runat="server" Text='<%# Bind("Date") %>'></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
       <asp:TemplateField> 
        <ItemTemplate> 
         <asp:Label Width="100px" ID="lblData" runat="server" Text='<%# Bind("NoteContent") %>'></asp:Label> 
         <asp:HyperLink ID="linkMore" runat="server" /> 
        </ItemTemplate> 
        <FooterStyle Wrap="true" Width="100px" /> 
        <HeaderStyle Wrap="true" Width="100px" /> 
        <ItemStyle Wrap="true" Width="100px" /> 
       </asp:TemplateField> 
       <asp:TemplateField SortExpression="size" Visible="false"> 
        <ItemTemplate> 
         <asp:Label ID="lblSize" runat="server" Text='<%# Bind("Size") %>' Visible="false"></asp:Label> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
      <PagerStyle HorizontalAlign="Center" /> 
     </asp:GridView> 

我沒有什麼,我必須做一個迷,但是客戶希望客戶想要什麼(我需要模擬大型機屏幕的用戶界面)。感謝您的幫助

回答

0

我最終打破了由寬文字與
標籤......不是我想做的事,但找不到答案

1

試試這個

  <asp:TemplateField> 
       <ItemTemplate> 
        <asp:Label ID="lblData" runat="server" 
          Text='<%# Bind("NoteContent") %>'></asp:Label> 
       </ItemTemplate> 
       <ItemStyle Width="200px" /> 
      </asp:TemplateField> 
+0

我已經在我的代碼中那樣做了。我究竟做錯了什麼? – 2011-03-24 15:37:24

+0

我也一直在尋找這個確切的答案,現在2天... – clamchoda 2011-12-09 22:19:39

0

我當然,這仍在討論之中,但在ItemDataBound事件中添加「單詞換行」,「分詞」等操作可能會起作用。

1

換行有問題時,文本太長(沒有文字的空間),我的解決方案是使用CSS:隱藏的文本,如果它太長

這是示例代碼

   <Columns> 
        <asp:TemplateField HeaderText="Parameter path"> 
         <ItemTemplate> 
          <div class="paraGraphtext"> 
           <asp:Label ID="lblId" runat="server" Text='<%# Eval("tableField") %>'></asp:Label> 
          </div> 
         </ItemTemplate> 
        </asp:TemplateField> 
       </Columns> 

和css

.paraGraphtext 
     { 
      overflow: hidden; 
      text-overflow: ellipsis; 
      white-space: nowrap; 
      width:150px; 
     } 
0
  <Columns> 
       <asp:TemplateField HeaderText="Parameter path"> 
        <ItemTemplate> 
         <div style="white-space:normal;"> 
          <asp:Label ID="lblId" runat="server" Text='<%# Eval("tableField") %>'></asp:Label> 
         </div> 
        </ItemTemplate> 
       </asp:TemplateField> 
      </Columns> 
+1

歡迎來到[so]。你可以通過不只是包含代碼來改進你的答案 - 最好也提供一個解釋它的功能。有關改進答案的更多提示,請參見[答案]。謝謝! – 2013-12-31 08:06:58

0

設置樣式= 「表格的佈局:固定」,並在GridView的字段寬度= 「1000像素」 ... 這對我有用。

的代碼如下:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" style="table-layout: fixed" Width="900px" onprerender="GridView1_PreRender"> 

添加GridView1_PreRender事件,如果你想刪除默認的gridview的設置: 風格=「邊界崩潰:崩潰」

爲GridView1_PreRender事件代碼如下:如果您有任何疑問

protected void GridView1_PreRender(object sender, EventArgs e) 
{ 
    GridView1.CellSpacing = -1; 
    GridView1.Style["border-collapse"] = "seperate"; 
} 

請不要回復..