一、GridView表头的合并
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
switch (e.Row.RowType)
{
case DataControlRowType.Header:
TableCellCollection tcHeader = e.Row.Cells;
tcHeader.Clear();
tcHeader.Add(new TableHeaderCell());
tcHeader[0].Attributes.Add("colspan", "1");
tcHeader[0].HorizontalAlign = HorizontalAlign.Center;//表头居中显示
tcHeader[0].Text = "<input type='checkbox' id='chk' name='chk' οnclick='SelectAll(this.checked);' />";//表头绑定复选框
tcHeader.Add(new TableHeaderCell());
tcHeader[1].Attributes.Add("colspan", "1");
tcHeader[1].HorizontalAlign = HorizontalAlign.Center;
tcHeader[1].Text = "群组名称";
tcHeader.Add(new TableHeaderCell());
tcHeader[2].Attributes.Add("colspan", "1");
tcHeader[2].HorizontalAlign = HorizontalAlign.Center;
tcHeader[2].Text = "人数";
tcHeader.Add(new TableHeaderCell());
tcHeader[3].Attributes.Add("colspan", "4");
tcHeader[3].HorizontalAlign = HorizontalAlign.Center;
tcHeader[3].Text = "操作";
break;
}
}
二、GridView绑定复选框后实现全选/反选
function SelectAll(sa){ /* 选中或未选中(true or false) */
var objCheckBox;
chs = document.getElementById("GridView1").getElementsByTagName("INPUT");
for(var i=0;i<chs.length;i++)
{
if(chs[i].type=="checkbox")
{
objCheckBox=chs[i];
objCheckBox.checked=sa;
}
}
}
三、GridView中Eval跟Bind的使用
Eval:绑定的是只读数据的显示;Bind:可以绑定只读数据也可以绑定更新数据,Bind方法还把字段和控件的绑定属性联系起来,使得
数据控件(比如GridView等)的Update、Insert和Delete等方法可以使用这种联系来作出相应的处理。
Eval 和 Bind绑定的数据在<%# %> 显示, 若对要显示的数据作修改或者操作也在<%# %> 中,例如:
1, 对显示的字符串作字符显示:<%# (Eval("Address")).ToString().SubString(0,10)%>,显示10位数的地址。
2, 对显示的信息作判断:<%# (Eval("if_delete")).ToString == "yes" ?"已删除":"未删除"%>
3, 绑定后台的方法:<%# GetInfo(Eval("绑定字段").ToString())%>
后台方法:
public string GetInfo(string str){
//操作过程
}
四、GridView自定义分页(部分代码省略)
HTML:
<div>
<asp:GridView ID="GridView1" runat="server" Width="594px" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" OnRowCreated="GridView1_RowCreated" PageSize="15" AllowPaging="True" EmptyDataText="没有数据记录" ></asp:GridView>
</div>
<div>
<table>
<tr>
<td><asp:LinkButton ID="btnFirst" CommandArgument="first" OnClick="PagerButtonClick" runat="server">首页</asp:LinkButton></td>
<td><asp:LinkButton ID="btnPrev" CommandArgument="prev" OnClick="PagerButtonClick" runat="server">上页</asp:LinkButton></td>
<td><asp:LinkButton ID="btnNext" CommandArgument="next" OnClick="PagerButtonClick" runat="server">下页</asp:LinkButton></td>
<td><asp:LinkButton ID="btnLast" CommandArgument="last" OnClick="PagerButtonClick" runat="server">尾页</asp:LinkButton></td>
<td><asp:Label ID="LblCurrentIndex" runat="server"></asp:Label></td>
<td><asp:Label ID="LblPageCount" runat="server"></asp:Label></td>
<td><asp:Label ID="LblRecordCount" runat="server"></asp:Label>跳转到第</td>
<td><asp:DropDownList ID="ddlCurrentPage" runat="server" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"></asp:DropDownList></td>
<td>页</td>
</tr>
</table>
</div>
.CS
DataSet ds = new DataSet();
ds = people.GetCustomerInfo();
this.GridView1.DataSource = ds.Tables[0];
this.GridView1.DataBind();
LblCurrentIndex.Text = "第" + (GridView1.PageIndex + 1).ToString() + "页 ";
LblPageCount.Text = "共" + GridView1.PageCount.ToString() + "页 ";
LblRecordCount.Text = "总共" + ds.Tables[0].Rows.Count.ToString() + "条 ";
if (ds.Tables[0].Rows.Count == 0)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
LblCurrentIndex.Visible = false;
LblPageCount.Visible = false;
LblRecordCount.Visible = false;
}
else if (GridView1.PageCount == 1)
{
btnFirst.Visible = false;
btnPrev.Visible = false;
btnNext.Visible = false;
btnLast.Visible = false;
}
// 计算生成分页页码,分别为:"首 页" "上一页" "下一页" "尾 页"
btnFirst.CommandName = "1";
btnPrev.CommandName = (GridView1.PageIndex == 0 ? "1" : GridView1.PageIndex.ToString()); //为第一页后不再能往前翻(相当于首页)
btnNext.CommandName = (GridView1.PageCount == 1 ? GridView1.PageCount.ToString() : (GridView1.PageIndex + 2).ToString());//
btnLast.CommandName = GridView1.PageCount.ToString();
this.ddlCurrentPage.Items.Clear();
for (int i = 1; i <= this.GridView1.PageCount; i++)
{
this.ddlCurrentPage.Items.Add(i.ToString()); //下拉框的绑定
}
this.ddlCurrentPage.SelectedIndex = this.GridView1.PageIndex; //跳转到指定页
对应事件
//对应LinkButton事件
protected void PagerButtonClick(object sender, EventArgs e)
{
GridView1.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandName) - 1;
BindCustomerInfo();
}
//对应DropDownList事件
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.GridView1.PageIndex = this.ddlCurrentPage.SelectedIndex;
BindCustomerInfo();
}