Gridview的一些基本设置
Gridview里如果要设置PagerTemplate属性的话,必须要开启分页,必须要声明gridview的时候设置AllowPaging="True"
<AlternatingRowStylebackcolor="PaleTurquoise"/>
设置数据中每隔一行显示的颜色
用Gridview绑定非sqlserver、access的数据库时微软自己的那套绑定数据库代码自动生成的代码就没法用了,本人在开发中使用的是mysql数据库,在gridview中要显示数据,只能通过重新自己写代码绑定数据了。
Gridview绑定mysql数据库
准备驱动
Gridview是无法直接连接mysql的,需要下载驱动程序,网上下载一个就行了,需要的就是一个动态链接库,我用的是MySql.Data.dll,下载后,在网站里添加下引用,把dll引入项目,后台编写代码的时候需要using声明。
连接数据库
本人是写了一个类来连接数据库,专门用户执行读写数据库的各种操作。
首先在网站的配置文件web.config里添加连接语句,
<addname="localMySqlServer"connectionString="DataSource=localhost;UserID=root;Password=123456;DataBase=brainwave;port=3306;Charset=gbk"/>
所有代码我都打包放到csdn上了,DBO类就是连接数据库的类。链接如下:
http://download.csdn.net/detail/diaoliangwang/5090133
Gridview绑定数据
GridView的绑定方法一定要在!ISPOSTBACK中,否则编辑状态下可编辑的列的值是没有初值的,而且无法更新数据。
核心代码如下,mygrid是gridview的id。输入sql语句就可以实现绑定数据。
public void GridBind(string sql)
{
String strSql = sql;
DataSet dataSet = obj.GetDataSet(strSql);
myGrid.DataSource = dataSet.Tables[0];
myGrid.DataBind();
}
效果如下:
绑定数据库搞定了。
Gridview数据的删除和修改
修改操作会触发编辑更新和取消三个函数,删除操作会触发删除函数,在声明gridview时需要声明,
OnRowDeleting="GridView1_RowDeleting"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit="GridView1_RowCancelingEdit"
然后分别编写各个函数:
删除函数:GridView1_RowDeleting
protected void GridView1_RowDeleting(objectsender, GridViewDeleteEventArgs e)
{
String ownerid = GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
string strSQL = "delete from owner where ownerid = '" +ownerid + "'";
try
{
if (obj.ExecuteSql(strSQL) == 1)
{
Response.Write("<script>window.alert('删除成功!')</script>");
GridBind("select * fromowner ");
}
else
{
Response.Write("<script>window.alert('删除失败!')</script>");
GridBind("select * fromowner ");
}
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
}
编辑函数;GridView1_RowEditing
protected void GridView1_RowEditing(objectsender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;//设置gridview中选中那行为可编辑状态
GridBind("select * fromowner");
}
更新函数:GridView1_RowUpdating
更新函数里先判断一下是否为新值,是的话就保存,需要注意的是如何获取到gridview中更新的数据。对于不可编辑的列,可以直接通过去字符串的值的方式取值,即:
GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
但是,重要的是要取回编辑过的列的值,通过直接取字符串的值的方式取出的值永远都是空的,因为编辑状态下gridview是对文本域使用了一个textbox控件来获取新的值,所以需要强制类型装换一下,再取出控件的值,即;
((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
protected voidGridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (e.NewValues != e.OldValues)
{
string ownerid =GridView1.Rows[e.RowIndex].Cells[0].Text.ToString();
string name =((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text.ToString();
string birthday =((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text.ToString();
string sex =((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
string rank =((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
string username =((TextBox)GridView1.Rows[e.RowIndex].Cells[5].Controls[0]).Text.ToString();
string password =((TextBox)GridView1.Rows[e.RowIndex].Cells[6].Controls[0]).Text.ToString();
string strsql = "update ownerset name='" + name + "'," +
"rank='"+ rank + "'," +
"birthday='" + birthday + "'," +
"sex='" +sex + "'," +
"username='" + username + "'," +
"password='" + password+ "' " +
" whereownerid = " + ownerid;
//mydiv.InnerHtml = strsql;
try
{
if (obj.ExecuteSql(strsql) >0)
{
Response.Write("<script>window.alert('修改成功!')</script>");
GridView1.EditIndex = -1;//取消编辑状态
GridBind("select *from owner");
}
else
{
Response.Write("<script>window.alert('修改失败!')</script>");
}
}
catch (System.Exception ex)
{
Response.Write(ex.Message);
}
}
}
Gridview分页设置
Gridview中本身的分页对于非微软数据库来说却是很好用,可是要收费啊,用mysql的话就要自己重新写分页函数了。
配置gridview的分页按钮
效果如下:
代码如下:
<PagerTemplate>
<table align="center" border="0"cellpadding="0" cellspacing="2"class="infolistpage"
width="100%">
<tr>
<td class="page" style="width: 40%;">
<spanclass="fleft">共有 <font>
<asp:LabelID="LabelSumCount" runat="server"></asp:Label></font> 条记录 每页显示 <font>
<asp:LabelID="LabelPageCount" runat="server" Text='<%#((GridView)Container.Parent.Parent).PageSize %>'></asp:Label></font>
条 共 <font>
<asp:LabelID="lblPageCount" runat="server" Text='<%#((GridView)Container.Parent.Parent).PageCount %>'></asp:Label></font>
页</span>
</td>
<td class="page" style="width: 60%;">
<spanclass="fright">
<asp:LinkButtonID="btnFirst" runat="server"CausesValidation="False" CommandArgument="First"
CommandName="Page"Text="首 页"οnclick="PagerButtonClick"></asp:LinkButton>
<asp:LinkButtonID="btnPrev" runat="server"CausesValidation="False" CommandArgument="Prev"
CommandName="Page" Text="上一页"οnclick="PagerButtonClick"></asp:LinkButton>
<asp:LinkButtonID="btnNext" runat="server"CausesValidation="False" CommandArgument="Next"
CommandName="Page" Text="下一页"οnclick="PagerButtonClick"></asp:LinkButton>
<asp:LinkButtonID="btnLast" runat="server"CausesValidation="False" CommandArgument="Last"
CommandName="Page" Text="尾 页"οnclick="PagerButtonClick"></asp:LinkButton>
</span>
</td>
</tr>
</table>
</PagerTemplate>
给分页按钮赋值
在PagerTemplate中声明的控件在后台代码里是无法直接通过其ID获取控件的,需要用findcontrol函数来查找,例如后台给总页数赋值时需要查找到LabelSumCount控件,核心代码如下:
Label LabelSumCount =(System.Web.UI.WebControls.Label)GridView1.BottomPagerRow.Cells[0].FindControl("LabelSumCount");
LabelSumCount.Text =dataSet.Tables[0].Rows.Count.ToString();
需要注意的是,设置控件的值的操作必须要在gridview绑定操作以后,否则gridview里是找不到控件的,系统会提示你未引用的错误。
另外,通过gridview1.Rows.Count获取的值只是gridview当前页的数据行数,不是所有记录的条数,要获取所有记录的行数必须在绑定函数中通过读取dataset中table的行数来实现,而且还要在绑定gridview之后,因此绑定函数的核心代码如下:
//设置 GridView 数据源绑定
public void GridBind(string sql)
{
String strSql = sql;
DataSet dataSet = obj.GetDataSet(strSql);
GridView1.DataSource = dataSet.Tables[0];
GridView1.DataBind();
Label LabelSumCount =(System.Web.UI.WebControls.Label)GridView1.BottomPagerRow.Cells[0].FindControl("LabelSumCount");
LabelSumCount.Text = dataSet.Tables[0].Rows.Count.ToString();
}
添加分页函数的响应函数
函数主要是首页 上一页 下一页 尾页,四个,也可以再增加一个跳转到某一页的函数,在声明按钮时设置了一个叫CommandArgument的属性,而且设置的不同的值,这些按钮在后台通过一个函数来统一处理,即声明的PagerButtonClick,在该函数中根据获取到的不同的CommandArgument属性的值来分别设置gridview的pageindex属性,然后重新绑定gridview的数据源,需要注意的是,我之前思考的方法是写不同的数据库查询语言去查询mysql中某些段的数据,比如写个sql语句去查询mysql数据库中的第10条到第20条数据,需要用到ROWNUMBER()函数,但是,遗憾的是,我纠结了好久,sql语句语法没有问题,可是mysql就是报错,原因是mysql不支持ROWNUMBER()函数,查了下,也可以在mysql里设置变量等手段来实现这一功能,但是方法比较复杂。其实在gridview的绑定机制里,通常是先把数据库里所有符合要求的数据读出来了在显示,这种方法对于数据量比较大的数据库来说开销有点大。在项目中,我还是采用了直接通过设置gridview的pageindex属性的值,然后重新绑定数据源的方式来实现分页,而不是通过写不同的sql语句。但是在每次重新设置了pageindex的值以后,都要重新绑定数据源,而不是简单的调用一下gridview的databind函数。核心代码如下:
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "Next":
if (GridView1.PageIndex <(GridView1.PageCount - 1))
{
GridView1.PageIndex+= 1;
}
break;
case "Prev":
if (GridView1.PageIndex > 0)
{
GridView1.PageIndex -= 1;
}
break;
case "First":
GridView1.PageIndex= 0;
break;
case "Last":
GridView1.PageIndex= (GridView1.PageCount - 1);
break;
default:
GridView1.PageIndex= 0;
break;
}
String strSql = "select * fromowner";
DataSet dataSet =obj.GetDataSet(strSql);
GridView1.DataSource =dataSet.Tables[0];
GridView1.DataBind();
}