首页 > 解决方案 > ASP.NET C# - 维护 DataGrid 列宽

问题描述

我有一个数据源,每次运行时它可能有不同数量的列。我尝试使用 RowDataBound 事件根据标题设置每列的大小(如下所示)。

问题是,如果有很多列,宽度会缩小。不管我有多少列,有没有办法强制这个宽度?

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {

        for (int j = 0; j < NoOfColumns; j++)
        {
            string row = e.Row.Cells[j].Text;

            if (row.Contains("Date"))
            {
                e.Row.Cells[j].Width = new Unit(150);
            }
            else if (row.Contains("Transaction"))
            {
                e.Row.Cells[j].Width = new Unit(250);
            }
            else if (row.Contains("+"))
            {
                e.Row.Cells[j].Width = new Unit(300);
            }
            else if (row.Contains("Balance"))
            {
                e.Row.Cells[j].Width = new Unit(400);
            }

        }


    }

标签: c#htmlasp.netgridview

解决方案


推荐阅读