ぁwō'd溡緔*

.ㄧ┠愛.│是最後一次___". 傷.│也到此為止 _ ...ㄧ做個浪子..┻┕┅獨行天下!!

假设你的DataGrid有三列,现在想将前两列作为"大类1",第三列作为"大类2",现在,你可以在ItemDataBound事件中加入下面的代码:
if (e.Item.ItemType == ListItemType.Header)
{
e.Item.Cells[0].ColumnSpan = 2;
e.Item.Cells[0].Text = "大类1</td><td>大类2</td></tr><tr><td>" + e.Item.Cells[0].Text;
}
用这个方法可以为任意添加新行。

2、设置绑定列或者自动生成列的编辑框宽度
请在你的ItemDataBound事件中加入一下代码:
if (e.Item.ItemType == ListItemType.EditItem)
{
for (int i = 0; i < e.Item.Cells.Count; i++)
{
TextBox txt = (TextBox)e.Item.Cells.Controls[0];
txt.Width = Unit.Pixel(50);
}
}

3、处理在DataGrid中的DropDownList的事件
DropDownList没有CommandName属性,所以不能用ItemCommand事件,不过你可以这样试试:
在DataGrid的模板列中加入的DropDownList控件
<asp:DropDownList runat="server" id="ddl" AutoPostBack="True" OnSelectedIndexChanged="ddl_SelectedIndexChanged" />
然后你在.aspx.cs中加入一个函数
protected void ddl_SelectedIndexChanged(object sender, System.EventArgs e) //一定要声明成protected或者public,不能是private的。
{
//在这里就可以加入其他代码
}

posted on 2007-04-23 09:14  Top.Net  阅读(494)  评论(0编辑  收藏  举报