我的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控件被成功填充。
請注意,我可以設置列的對齊和背景顏色,但不是它的寬度。
我嘗試了許多解決方案,但都沒有工作。
它總是將列的大小調整爲最長的內容。
它可以完成...?
想一想,單個單元格在表格中如何具有不同的寬度。列可以有寬度,而不是單元格。 –
行不能有不同的列寬。所有這些都應該使用樣式表進行設置。 – wolfeh
是的。我應該練習英語。與此同時,我正在尋找一種方法來設置代碼後面的列寬。 (我會改變標題,並感謝評論)。 – gadi