在c#中设置gridview列宽

时间:2011-10-23 07:41:55

标签: c# javascript jquery asp.net gridview

我想在页面加载事件中修复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();
}

2 个答案:

答案 0 :(得分:0)

在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;
        }

答案 1 :(得分:0)

一种方法是创建一个网格视图 RowCreated 事件,并在该事件中写入

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

其中[1]是列索引。