locked
How to force set the gridview column width RRS feed

  • Question

  • User1948389125 posted

    Hi

    I created a gridview to display the attendance, I use pivot in the stored procedure so that it can show everyday for a staff.

    most of the columns are standard size but the staff and department i need to make it bigger size.

    I tried below codes but it failed to follow the width i set, all columns are set to same size. Any idea what have i missed??

    <div style="OVERFLOW-Y: scroll; OVERFLOW-X: auto; WIDTH: 99%; HEIGHT: 300px">
    <asp:GridView ID="gvAttendanceReport" runat="server" SkinID="Professional"
    CssClass="GridViewCSS"
    Cellpadding="4"
    HeaderStyle-BackColor="#444444"
    HeaderStyle-ForeColor="White"
    AlternatingRowStyle-BackColor="#dddddd"
    AutoGenerateColumns="false"
    Width="100%"
    ShowHeader="true">
    </asp:GridView>
    </div>

    gvAttendanceReport.DataSource = dr;
    for (int i = 0; i < dr.FieldCount; i++)
    {
    newColumnName = new BoundField();
    newColumnName.DataField = dr.GetName(i);
    newColumnName.HeaderText = dr.GetName(i);
    newColumnName.Visible = true;
    if (dr.GetName(i).ToString() == "CONSUMER_NAME")
    {
    newColumnName.HeaderText = "CARD OWNER";
    newColumnName.ItemStyle.Width = Unit.Pixel(1000);
    }
    else if (dr.GetName(i).ToString() == "DEPARTMENT_NAME")
    {
    newColumnName.HeaderText = "DEPARTMENT";
    newColumnName.ItemStyle.Width = Unit.Pixel(1000);
    }
    gvAttendanceReport.Columns.Add(newColumnName);
    }
    gvAttendanceReport.DataBind();

    Thursday, January 10, 2019 6:20 AM

All replies

  • User-1716253493 posted

    Try set rowstyle to spesific width

    <RowStyle Width="100px" />

    Or set width in code behind in else part

    If you want 100% gv width, set others columns small except card and dept name (auto)

    Thursday, January 10, 2019 7:12 AM
  • User283571144 posted

    Hi  arctura,

    According to your description and codes, I made a sample.  It works well on my side, the column width setting was valid.I guess the column width is useless is related with your gridview CSS.

     Could you post more details codes about the  GridViewCSS and code-behind?

    If you could post more details information, it will be more easily for us to reproduce the issue and find out the solution.

    More details about my test demo, you could refer to below codes:

    ASPX:

    <div style="overflow-y: scroll; overflow-x: auto; width: 99%; height: 300px">
                    <asp:GridView ID="GridView1" runat="server"
                        SkinID="Professional"
                        CellPadding="4"
                        CssClass="GridViewCSS"
                        HeaderStyle-BackColor="#444444"
                        HeaderStyle-ForeColor="White"
                        AlternatingRowStyle-BackColor="#dddddd"
                        Width="100%"
                        ShowHeader="true">
                    </asp:GridView>
    
                </div>
    

    Code-behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) { getData(); }
            }
            protected void getData()
            {
                DataTable dr = new DataTable();
                dr.Columns.Add("department");
                dr.Columns.Add("staff");
                dr.Rows.Add("a", "jack");
                dr.Rows.Add("B","susam");
                this.GridView1.DataSource = dr;
                var newColumnName = new BoundField();
                newColumnName.HeaderText = "DEPARTMENT";
                newColumnName.ItemStyle.Width = Unit.Pixel(300);
                GridView1.Columns.Add(newColumnName);
                this.GridView1.DataBind();
            }
    

    Code-behind:

     protected void Page_Load(object sender, EventArgs e)
            {
                if (!IsPostBack) { getData(); }
            }
            protected void getData()
            {
                DataTable dr = new DataTable();
                dr.Columns.Add("department");
                dr.Columns.Add("staff");
                dr.Rows.Add("a", "jack");
                dr.Rows.Add("B","susam");
                this.GridView1.DataSource = dr;
                var newColumnName = new BoundField();
                newColumnName.HeaderText = "DEPARTMENT";
                newColumnName.ItemStyle.Width = Unit.Pixel(300);
                GridView1.Columns.Add(newColumnName);
                this.GridView1.DataBind();
            }
    

    Result:

    Best Regards,

    Brando

    Friday, January 11, 2019 9:14 AM