在gridview和datagrid里设置列宽(转)

<script>function StorePage(){d=document;t=d.selection?(d.selection.type!='None'?d.selection.createRange().text:''):(d.getSelection?d.getSelection():'');void(keyit=window.open('http://www.365key.com/storeit.aspx?t='+escape(d.title)+'&u='+escape(d.location.href)+'&c='+escape(t),'keyit','scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes'));keyit.focus();}</script>

无论是gridview还是datagrid,在绑定数据后,列宽都不是固定的,在设计时是没法设定的,只能通过绑定是触发的事件来重新设定。参考http://msdn2.microsoft.com/zh-cn/library/ms178296(VS.80).aspx 的解释。

 

gridview的代码:

 

protected intwidestData;
protectedvoidGridView1_RowDataBound(objectsender,
GridViewRowEventArgse)
{
System.Data.DataRowViewdrv;
drv
=(System.Data.DataRowView)e.Row.DataItem;
if(e.Row.RowType==DataControlRowType.DataRow)
{
if(drv!=null)
{
StringcatName
=drv[1].ToString();
Response.Write(catName
+?¡ã/?¡À);
intcatNameLen=catName.Length;
if(catNameLen>widestData)
{
widestData
=catNameLen;
GridView1.Columns[
2].ItemStyle.Width=
widestData
*30;
GridView1.Columns[
2].ItemStyle.Wrap=false;
}
}
}
}
protectedvoidPage_Load(objectsender,EventArgse)
{
widestData
=0;
}

 

datagrid 的代码:

 

protectedvoiddatagrid_ItemCreated(objectsender,DataGridItemEventArgse)
{
ListItemTypeitemType
=e.Item.ItemType;
if(itemType==ListItemType.Header)
{
for(inti=0;i<e.Item.Cells.Count;i++)
{
e.Item.Cells[i].Width
=Unit.Pixel(80);
e.Item.Cells[i].Wrap
=false;
}
}
}

http://cjc.javaeye.com/blog/402726

posted @ 2010-05-14 00:39  lalab  阅读(91)  评论(0)    收藏  举报