2015-09-21 45 views
1

我的GridView:的GridView - 集列的寬度在後面的代碼(的AutoGenerateColumns = 「真」)

<asp:GridView ID="GridView1" runat="server" 
    OnRowDataBound="GridView1_RowDataBound" 
    AutoGenerateColumns="true" 
    DataKeyNames="Role_id"> 
</asp:GridView> 

在後面的代碼:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    if (e.Row.RowType == DataControlRowType.Header) 
    { 
     //e.Row.Cells[2].Width = 120;      // isn't working.... 
    } 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     e.Row.Cells[2].Width = new Unit(120);    // isn't working.... 
     TableCell cell = e.Row.Cells[2]; 
     cell.HorizontalAlign = HorizontalAlign.Right;   //**** does work! 
     cell.BackColor = Color.LightGray;      //**** does work! 
     //cell.Width = 120;        // isn't working.... 
     //e.Row.Cells[2].Width = new Unit("120px") ;  // isn't working.... 
     //e.Row.Cells[2].CssClass = "myGV_Cell_Width"; // isn't working.... 
    } 
} 

GridView控件被成功填充。
請注意,我可以設置列的對齊和背景顏色,但不是它的寬度。
我嘗試了許多解決方案,但都沒有工作。
它總是將列的大小調整爲最長的內容。
它可以完成...?

+0

想一想,單個單元格在表格中如何具有不同的寬度。列可以有寬度,而不是單元格。 –

+0

行不能有不同的列寬。所有這些都應該使用樣式表進行設置。 – wolfeh

+0

是的。我應該練習英語。與此同時,我正在尋找一種方法來設置代碼後面的列寬。 (我會改變標題,並感謝評論)。 – gadi

回答

2

我能夠與RowCreated事件做它來設置列寬,但我不得不設置網格的寬度和也將網格設置爲表格佈局:固定

<asp:GridView ID="GridView1" runat="server" 
    style="table-layout:fixed;" Width="1000px" 
    OnRowCreated = "GridView1_RowCreated" 
    OnRowDataBound="GridView1_RowDataBound" 
    AutoGenerateColumns="true" 
    DataKeyNames="Role_id"> 
</asp:GridView> 

protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) 
{ 

e.Row.Cells[0].Width = New Unit("220px"); 

} 
+0

是的!那就是訣竅。 – gadi

0

ASP:GridView控件呈現到HTML表,所以你只需要使用CSS

th, td { 
    width: 120px; 
} 
+0

我有myStyleSheet.css我把我的風格。添加你提到的風格並沒有幫助(儘管它對分頁數做了一些奇怪的事情,第三列[2]仍然調整到最長的內容)。 – gadi

+0

您可以使用Firefox(https://developer.mozilla.org/en-US/docs/Tools/Page_Inspector)或Chrome(https://developer.chrome.com/devtools/docs/dom-and-styles)頁面檢查人員瞭解哪些樣式適用於您的HTML元素。 –

相關問題