Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为rowDataBound方法设置行的高度_C#_Asp.net_Sql - Fatal编程技术网

C# 为rowDataBound方法设置行的高度

C# 为rowDataBound方法设置行的高度,c#,asp.net,sql,C#,Asp.net,Sql,我有以下代码: protected void exampleGridView_RowDataBound(object o, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Cells[0].Width = new Unit("150px"); e.Row.Cells[1].Width = ne

我有以下代码:

protected void exampleGridView_RowDataBound(object o, GridViewRowEventArgs e)


  {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Cells[0].Width = new Unit("150px");
            e.Row.Cells[1].Width = new Unit("5px");
            e.Row.Cells[2].Width = new Unit("150px");
            e.Row.Cells[3].Width = new Unit("150px");
            e.Row.Cells[4].Width = new Unit("150px");
            e.Row.Cells[5].Width = new Unit("150px");
            e.Row.Cells[6].Width = new Unit("150px");
            // and so on
        }
    }
是否可以设置单元格的高度?
谢谢

您正在设置RowDataBound()事件的宽度。不能这样做,请在databind()发生之前尝试columns属性

范例

GridView1.CellPadding = 20;
GridView1.RowStyle.Height = 80;
或 你可以试试这个样品

在aspx中:

 <asp:GridView ID="GridView1" OnRowDataBound="GridView1_RowDataBound" AutoGenerateColumns="false"
        runat="server">
        <Columns>
            <asp:BoundField DataField="CatID" HeaderText="ID" />
            <asp:BoundField DataField="CatName" HeaderText="Name" />
        </Columns>
    </asp:GridView>

在.cs中:

protected void Page_Load(object sender, EventArgs e)
{
    List<Category> _lstCategory = new List<Category>{new Category { CatID = 1, CatName = "Cat1" }, 
                                                        new Category { CatID=2,CatName="Cat2" }};
    //GridView1.CellPadding = 20;
    //GridView1.RowStyle.Height = 80;
    GridView1.DataSource = _lstCategory;
    GridView1.DataBind();

}
public class Category
{
    public int CatID { get; set; }
    public string CatName { get; set; }
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
    e.Row.Cells[1].Width = 100;
    e.Row.Cells[0].Width = 1;
}
受保护的无效页面加载(对象发送方,事件参数e)
{
列表lstCategory=new List{new Category{CatID=1,CatName=“Cat1”},
新类别{CatID=2,CatName=“Cat2”};
//GridView1.CellPadding=20;
//GridView1.RowStyle.Height=80;
GridView1.DataSource=lstCategory;
GridView1.DataBind();
}
公共类类别
{
公共int CatID{get;set;}
公共字符串CatName{get;set;}
}
受保护的void GridView1_RowDataBound(对象发送方,GridViewRowEventArgs e)
{
e、 行。单元格[1]。宽度=100;
e、 行。单元格[0]。宽度=1;
}

它对我有用

我认为它的ASP.NET不是标记的ASP classic,我尝试使用e.Row.Cells[0].Height=new Unit(“25px”);但是它并没有真正改变行的高度。请尝试:e.Row.Cells[0]。height=25;对不起,我是asp新手,该怎么做?@user1781830我刚刚用代码更新了我之前的回复。请查收。