I have a gridview with autogenerated columns that I set programmatically I want to format the column width. This is my code for my gridview in code behind…
If Not Page.IsPostBack Then
Dim budgetTable As New DataTable("Budgets")
budgetTable.Columns.Add("Approval Date", GetType(Date))
budgetTable.Columns.Add("Total Amount", GetType(String))
budgetTable.Columns.Add("Comments", GetType(String))
budgetTable.Columns.Add("Initials", GetType(String))
Try
For i As Integer = 0 To 0
Dim tableRow As DataRow = budgetTable.NewRow()
tableRow("Approval Date") = Date.Today
tableRow("Total Amount") = ""
tableRow("Comments") = ""
tableRow("Initials") = ""
budgetTable.Rows.Add (tableRow)
Next
Session("BudgetsTable") = budgetTable
BindData()
Catch ex As Exception
End Try
End If
And this is the gridview on the html side:
<asp:GridView ID="gvOLIAdj" runat="server" CssClass="td8" CellPadding="4" ForeColor="#333333" PageSize="2" ViewStateMode="Enabled">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:CommandField EditText="Add" ShowEditButton="True" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
— EDIT —
(removed old contents after reading comments from OP, since that won’t help)
Try using the RowDataBound event to set the widths (or any other properties).
UNTESTED Code:
If the above doesn’t work, try setting it for
e.Row.RowType = DataControlRowType.DataRow.