首现我们拖入一个DataGridView控件到.aspx页面中,然后绑定你需要显示的列,具体代码如下。
复制代码 代码如下:
<asp:GridView runat="server" AutoGenerateColumns="False"
OnRowDeleting="gvDepartList_RowDeleting" RowDataBound="gvDepartList_RowDataRound">
<Columns>
<asp:TemplateField HeaderText="部门名称" >
<ItemTemplate>
<asp:Label runat="server" Text='<%# Eval("DepartName") %>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="机构" DataField="BranchId" />
<asp:BoundField HeaderText="负责人" DataField="PrincipalUser" />
<asp:BoundField HeaderText="联系电话" DataField="ConnectTelNo" />
<asp:BoundField HeaderText="移动电话" DataField="ConnectMobileTelNo"/>
<asp:BoundField HeaderText="传真" DataField="Faxes" />
<asp:TemplateField HeaderText="修改">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/edit.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:ImageButton ImageUrl="../images/delete.gif" CommandArgument='<%#Eval("DepartId") %>' CommandName="delete" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
二:在这个.aspx页面后台的Page_load事件中绑定数据。
复制代码 代码如下:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
gvDepartList.DataSource= new DepartInfoManager().GetDepartInfos(-1);
gvDepartList.DataBind();
}
}
如果我们想添加一个DataGridView的光棒效果,就是每一行鼠标悬浮上去变动背景色啦。
复制代码 代码如下:
/// <summary>
/// 动态注册脚本(在GridView控件呈现之前) 光棒效果
/// </summary>
/// <param></param>
/// <param></param>
protected void gvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
{
//此处判断只有在数据行在进行脚本注册
if (e.Row.RowType == DataControlRowType.DataRow)
{
//光棒效果
e.Row.Attributes.Add("onmouseover","currentcolor=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
e.Row.Attributes.Add("onmouseout ", "this.style.backgroundColor=currentcolor");
LinkButton lnkbtnDel = e.Row.FindControl("lnkbtnDel") as LinkButton;
lnkbtnDel.Attributes.Add("onclick", "return confirm('确定删除吗?')");
}
}