Asp.net datagrid, given column widths

I have the following asp.net table created using datagrid:

<%@ Page Language="vb" MasterPageFile="~/Masterpage.master" AutoEventWireup="false" MaintainScrollPositionOnPostback="true"  CodeFile="View.aspx.vb" Inherits="MDGRenewals.page_views"  %>
<%@ Register Src ="~/Webcontrols/Admin/Users/RoleManager.ascx" tagprefix="mdg" TagName="rolemanager" %>

<asp:Content ID="Content1" ContentPlaceHolderID="contentMain" runat="server">
        <form id="Form1" method="post" runat="server">
            <p><asp:datagrid id="DataGrid1" runat="server" AutoGenerateColumns="False" CellPadding="3">

                <ItemStyle CssClass="DGR_ITEM"></ItemStyle>
                <HeaderStyle CssClass="DGR_HEADER"></HeaderStyle>

                    <Columns>

                        <asp:TemplateColumn HeaderText="Username">
                            <ItemTemplate>
                                <asp:Label id="Label1" runat="server" Text='<%#Eval("user_id")%>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateColumn>

                        <asp:TemplateColumn HeaderText="Page">
                            <ItemTemplate>
                                <asp:Hyperlink id="Link1" runat="server" Text='<%#Container.DataItem("page_name")%>'></asp:Hyperlink>
                            </ItemTemplate>
                        </asp:TemplateColumn>

                        <asp:TemplateColumn HeaderText="View Count">
                            <ItemTemplate>
                                <asp:Label id="Label3" runat="server" Text='<%#Container.DataItem("Count")%>'></asp:Label>
                            </ItemTemplate>

                        </asp:TemplateColumn>

                    </Columns>

                </asp:datagrid></P>
        </form>
</asp:Content> 

      

My problem is that I want to be able to set the column widths, but I couldn't do that.

As you can see from the code, I've tried:

                <ItemStyle Width="200px"></ItemStyle>
                <HeaderStyle  Width="200px"></HeaderStyle>

      

but he doesn't answer that.

I edited the post to contain the css and comment out the use of css:

.DGR_HEADER
{
    font-weight: bolder;
    font-size: 11px;
    vertical-align: baseline;
    width: 200px;
    cursor: default;
    color: black;
    font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
    background-color: LightSteelBlue;
    text-align: center;
    text-decoration: none;
    height:15px;
}


.DGR_ITEM
{
    font-weight: normal;
    font-size: xx-small;
    width: 200px;
    cursor: default;
    color: black;
    font-family: Tahoma, Arial, Verdana, Helvetica, sans-serif;
    height: 20px;
    background-color: silver;
    text-align: left;
}

      

+3


source to share


3 answers


Try it. You can set the column width like this



ItemStyle-Width="30"
<asp:TemplateColumn HeaderText="Username" ItemStyle-Width="30">

      

+4


source


Both ItemStyle-Width and HeaderStyle didn't work for me ... until I made my grid wide enough to have "room" to expand the columns in question.

I kept setting the column width to 400px and nothing changed!



Then I set my TOTAL GRID to 4000px and oila, now this column is actually 400px. All the columns were flattened together, so he could not grow this column.

These probably aren't all the answers, but try it if you can't get these properties to work.

+1


source


Since datagrids are just displaying plain html <table>

, you can use css:

table td {
    width: 200px;
}

      

For a single table reference, you can put it directly in a file aspx

(or ascx

or whaterver)

#<%= this.DataGrid1.ClientID %> td {
    width: 200px;
}

      

0


source







All Articles