如何在Dynamic Gridview中設置列寬?當的AutoGenerateColumns = 「真」如何在動態網格視圖中設置列寬?
回答
你需要做這樣的
protected void gvData_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
e.Row.Cells(0).Width = new Unit("200px");
e.Row.Cells(1).Width = new Unit("500px");
}
}
您的標記
<asp:GridView id="gvData" runat="server"
OnRowDataBound="gvData_RowDataBound">
</asp:GridView>
默認情況下autogeneratecolumn變化的GridView的RowDataBound事件是假的所以沒有必要指定autogeneratecolumn = 「true」
上面這兩個解決方案不工作在我的情況:(默認情況下 –
您可以有如下serverside
方法:
private void GV_RowDataBound(object o, GridViewRowEventArgs e)
{
// apply custom formatting to data cells
if (e.Row.RowType == DataControlRowType.DataRow)
{
// set formatting for the category cell
TableCell cell = e.Row.Cells[0];
cell.Width = new Unit("120px");
cell.Style["border-right"] = "2px solid #666666";
// set formatting for value cells
for(int i=1; i<e.Row.Cells.Count; i++)
{
cell = e.Row.Cells[i];
// right-align each of the column cells after the first
// and set the width
cell.HorizontalAlign = HorizontalAlign.Right;
cell.Width = new Unit("90px");
// alternate background colors
if (i % 2 == 1)
cell.BackColor
= System.Drawing.ColorTranslator.FromHtml("#EFEFEF");
// check value columns for a high enough value
// (value >= 8000) and apply special highlighting
}
}
// apply custom formatting to the header cells
if (e.Row.RowType == DataControlRowType.Header)
{
foreach (TableCell cell in e.Row.Cells)
{
cell.Style["border-bottom"] = "2px solid #666666";
cell.BackColor=System.Drawing.Color.LightGray;
}
}
}
}
你aspx
頁
<asp:GridView id="myList" runat="server"
AutoGenerateColumns="true"
OnRowDataBound="GV_RowDataBound"
. . .
>
</asp:GridView>
有關詳細信息,您可以檢查here
AutoGenerateColumns是「true」。所以沒有提及 – Alex
我同意,但我認爲你需要檢查你的答案,因爲你已經提到_By默認autogeneratecolumn是** false **所以不需要指定autogeneratecolumn =「true」_ :) –
- 1. 如何在MVC視圖中設置表格列寬?
- 2. 設置網格視圖寬度Android
- 3. Android視圖 - 動態設置寬度
- 4. 如何爲動態下拉列表設置表格列寬?
- 5. 如何設置位圖網格視圖
- 6. 在我的表格的數據網格視圖中設置列寬
- 7. Highcharts - 動態設置列寬
- 8. 動態設置網格列/行寬/高度
- 9. 如何動態設置列表視圖中的列數?
- 10. 如何在BootStrap中設置最大寬度和網格列?
- 11. dgrid /網格設置動態列字段
- 12. 網格視圖動態生成的列
- 13. 如何在Android網格視圖中設置單元格大小?
- 14. 如何設置WPF中列表視圖中列的寬度?
- 15. 如何在視圖中放置網格
- 16. 如何在android tablelayout中動態設置列的寬度?
- 17. 如何在Oracle Apex中動態設置列寬
- 18. 如何在動態jasper excel報告中設置列寬dynamicaly?
- 19. 設置網格視圖單元格的寬度?
- 20. 設置網格列寬=自動和最大寬度
- 21. Android動態網格視圖
- 22. 設置根視圖寬度等於動態標籤的寬度
- 23. 如何在列表視圖上動態設置rgb顏色?
- 24. 爲交錯網格視圖動態設置固定高度
- 25. 如何在Android中動態設置視圖的位置?
- 26. 如何在代碼中設置位置動態文本視圖?
- 27. 如何設置CSS網格佈局中列的最大寬度?
- 28. 如何將數據網格中的列的寬度設置爲*
- 29. 如何在C#中設置數據網格視圖列的默認列大小(高度,寬度)
- 30. 設置表格列寬視爲了
添加一些代碼..? –
@Ameer:你有沒有嘗試過[某事從這裏](http://www.dotnetgallery.com/kb/resource69-Set-Gridview-column-width-dynamically-using-C.aspx) – BNN
感謝Nadeem它的工作:) –