繁体   English   中英

在C#中设置gridview列宽

[英]set gridview column width in c#

我想在页面加载事件中修复grdview列宽。

我从c#绑定了这个gridview,还使用了c#的数据源

我想为“地址”列设置宽度,因为它的数据很长。我也想使用自动滚动到此gridview。

这是我的代码...

  protected void Page_Load(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection(str);
    con.Open();
    DataSet ds = new DataSet();
    SqlCommand cmd = new SqlCommand("Select DISTINCT FName+ ' ' +MName+ ' ' +LName as Name,"
             + " HomePhone,MobileNo1,"
             + " UPPER(ResAddr1) ++'  '+ UPPER(Resaddr2) ++'  '+ UPPER(ResAddr3) ++'  '+ UPPER(Resaddr4) ++'  '+ UPPER(Resaddr5) ++'  '+ UPPER(Resaddr6) ++'  '+ Pincode ++'  '+City as Address,"
             + " g.Category,f.GroupName as 'Group',Seats,"
             + " dbo.CONCATWTOTSHOW(d.MemberId,d.GID,d.CID)As SeatNo,"
             + " AmountExpected,AmountReceived,Discount,AmountPending,b.Remarks as Reference, (d.MemberId)"
             + " from Person_Master a INNER JOIN Member_Master b ON a.PersonId=b.PersonId"
             + " LEFT JOIN Payment_Master c ON b.MemberId = c.MemberId"
             + " INNER JOIN SeatAssign_Master d ON b.MemberId = d.MemberId"
             + " INNER JOIN Year_Master e ON b.Year = e.Id"
             + " INNER JOIN Group_Master f ON d.Gid=f.Gid"
             + " INNER JOIN Category_Master g ON d.Cid=g.Cid "
             + " where b.Year=2 and g.Cid=2 and b.Active=1 and d.Active=1 ", con);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    da.Fill(ds);

    GridView1.DataSource = ds;
    GridView1.DataBind();
}

在RowCreated事件中这样做

 GridView1.RowCreated += new GridViewRowEventHandler(GridView1_RowCreated);

      void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
        {
            e.Row.Cells[0].Width = 200;
           // GridView1.Columns[0].HeaderStyle.Width = 100;
        }

一种方法是创建一个Grid视图的RowCreated事件,然后在该事件中写入

e.Row.Cells[1].Width = Unit.Pixel(300);

其中[1]是列索引。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM