GridView中分页和导出数据.docx
- 文档编号:30119841
- 上传时间:2023-08-05
- 格式:DOCX
- 页数:17
- 大小:178.72KB
GridView中分页和导出数据.docx
《GridView中分页和导出数据.docx》由会员分享,可在线阅读,更多相关《GridView中分页和导出数据.docx(17页珍藏版)》请在冰豆网上搜索。
GridView中分页和导出数据
shiyusingSystem;
usingSystem.Data;
usingSystem.Configuration;
usingSystem.Collections;
usingSystem.Web;
usingSystem.Web.Security;
usingSystem.Web.UI;
usingSystem.Web.UI.WebControls;
usingSystem.Web.UI.WebControls.WebParts;
usingSystem.Web.UI.HtmlControls;
usingSystem.Data.SqlClient;
usingSystem.IO;
publicpartialclassGridView:
System.Web.UI.Page
{
protectedstringconnString=ConfigurationSettings.AppSettings["ConnectionString"];
protectedintrowCount=0;
protectedintpageCount=0;
protectedintpageSize=0;
protectedintpageNow=0;
protectedvoidPage_Load(objectsender,EventArgse)
{
if(!
Page.IsPostBack)
{
//connString=ConfigurationSettings.AppSettings["ConnectionString"];
//获取记录的总条数
SqlConnectioncon=newSqlConnection();
con.ConnectionString=connString;
con.Open();
SqlCommandcmd=newSqlCommand();
/********************************修改其中的数据表名称**************************/
cmd.CommandText="selectcount(*)fromBMSK_GB";
cmd.Connection=con;
rowCount=Convert.ToInt32(cmd.ExecuteScalar());
con.Close();
//设置和获取每页显示几条记录
DropDownList1.Items.Clear();
for(inti=1;i<=rowCount;i++)
{
DropDownList1.Items.Add(i.ToString());
}
if(rowCount<5)
{
DropDownList1.SelectedIndex=rowCount-1;
}
else
{
DropDownList1.SelectedIndex=4;
}
pageSize=Convert.ToInt32(DropDownList1.SelectedValue);
if((rowCount%pageSize)==0)
{
pageCount=Convert.ToInt32(rowCount/pageSize);
}
else
{
pageCount=Convert.ToInt32(rowCount/pageSize)+1;
}
//获取跳转到哪一页的信息
DropDownList2.Items.Clear();
for(intj=1;j<=pageCount;j++)
{
DropDownList2.Items.Add(j.ToString());
}
Label1.Text=rowCount.ToString();
Label3.Text=pageCount.ToString();
//保存控件数据
ViewState["RowCount"]=rowCount;
ViewState["PageSize"]=pageSize;
ViewState["PageCount"]=pageCount;
//当前页
pageNow=1;
ViewState["PageNow"]=pageNow;
}
rowCount=Convert.ToInt32(ViewState["RowCount"]);
pageCount=Convert.ToInt32(ViewState["PageCount"]);
pageSize=Convert.ToInt32(ViewState["PageSize"]);
pageNow=Convert.ToInt32(ViewState["PageNow"]);
if(pageCount<0||rowCount<0)
{
Response.Write("发生错误");
Response.End();
}
if(pageNow<=1)
{
pageNow=1;
btnFirst.Enabled=false;
btnPrevious.Enabled=false;
}
Label2.Text=pageNow.ToString();
if(!
Page.IsPostBack)
{
BindData();
}
}
//进行数据绑定
publicvoidBindData()
{
/********************************修改其中的数据表名称**************************/
Stringsql="selectSKBMas水库编码,SKMCas水库名称,SZSas所在市,SZXas所在县,LXas类型,SFWXas是否危险fromBMSK_GB";
SqlConnectioncon=newSqlConnection();
con.ConnectionString=connString;
con.Open();
SqlDataAdapterda=newSqlDataAdapter(sql,con);
DataSetds=newDataSet();
/********************************修改其中的数据表名称**************************/
da.Fill(ds,(pageNow-1)*pageSize,pageSize,"BMSK_GB");
GridView1.DataSource=ds;
GridView1.DataBind();
ds.Clear();
con.Close();
Label2.Text=pageNow.ToString();
}
protectedvoidDropDownList1_SelectedIndexChanged(objectsender,EventArgse)
{
pageSize=Convert.ToInt32(DropDownList1.SelectedValue);
if(Convert.ToInt32(rowCount%pageSize)==0)
{
pageCount=Convert.ToInt32(rowCount/pageSize);
}
else
{
pageCount=Convert.ToInt32(rowCount/pageSize);
}
DropDownList2.Items.Clear();
for(inti=1;i<=pageCount;i++)
{
DropDownList2.Items.Add(i.ToString());
}
Label3.Text=pageCount.ToString();
ViewState["PageSize"]=pageSize;
ViewState["PageCount"]=pageCount;
if(pageNow>pageCount)
{
pageNow=1;
ViewState["PageNow"]=pageNow.ToString();
}
btnFirst.Enabled=true;
btnPrevious.Enabled=true;
btnNext.Enabled=true;
btnLast.Enabled=true;
if(pageNow==1)
{
btnFirst.Enabled=false;
btnPrevious.Enabled=false;
}
if(pageNow==pageCount)
{
btnNext.Enabled=false;
btnLast.Enabled=false;
}
BindData();
}
protectedvoidDropDownList2_SelectedIndexChanged(objectsender,EventArgse)
{
pageNow=Convert.ToInt32(DropDownList2.SelectedValue);
ViewState["PageNow"]=pageNow.ToString();
BindData();
}
//显示第一页
privatevoidmoveFirst()
{
pageNow=1;
ViewState["PageNow"]=pageNow.ToString();
BindData();
btnFirst.Enabled=false;
btnPrevious.Enabled=false;
btnNext.Enabled=true;
btnLast.Enabled=true;
}
//显示上一页
privatevoidmovePrevious()
{
pageNow=pageNow-1;
ViewState["PageNow"]=pageNow.ToString();
BindData();
if(pageNow==1)
{
btnFirst.Enabled=false;
btnPrevious.Enabled=false;
}
btnNext.Enabled=true;
btnLast.Enabled=true;
}
//显示下一页
privatevoidmoveNext()
{
pageNow=pageNow+1;
ViewState["PageNow"]=pageNow.ToString();
BindData();
btnFirst.Enabled=true;
btnPrevious.Enabled=true;
if(pageNow==pageCount)
{
btnNext.Enabled=false;
btnLast.Enabled=false;
}
}
//显示尾页
privatevoidmoveLast()
{
pageNow=pageCount;
ViewState["PageNow"]=pageNow.ToString();
BindData();
btnFirst.Enabled=true;
btnPrevious.Enabled=true;
btnNext.Enabled=false;
btnLast.Enabled=false;
}
protectedvoidbtnFirst_Click(objectsender,EventArgse)
{
moveFirst();
}
protectedvoidbtnPrevious_Click(objectsender,EventArgse)
{
movePrevious();
}
protectedvoidbtnNext_Click(objectsender,EventArgse)
{
moveNext();
}
protectedvoidbtnLast_Click(objectsender,EventArgse)
{
moveLast();
}
//excel导入gridview完整实例
//--------------源代码--------------------
protectedvoidbtnUpload_Click(objectsender,EventArgse)//上传文件
{
if(FileUpload1.HasFile)
{
//try
//{
stringpath=Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString())+"xls\\";//获取程序根目录
path+=Path.GetFileName(FileUpload1.FileName);
FileUpload1.PostedFile.SaveAs(path);
//BindGrid(lblCurrentPath.Text);
//stringxlsPath=path;//绝对物理路径
stringdbName=Path.GetFileName(FileUpload1.FileName).Replace("xls","");
//查询语句
stringxlsPath=AppDomain.CurrentDomain.BaseDirectory.ToString()+"xls\\"+Path.GetFileName(FileUpload1.FileName);
this.GvData.DataSource=GetExcelContent(xlsPath,dbName);
this.GvData.DataBind();
//}
//catch
//{
//ClientScript.RegisterStartupScript(typeof(Page),"aa","alert('文件上传失败!
')",true);
//}
}
}
privateDataSetGetExcelContent(stringfilepath,stringdbName)
{
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+filepath+";ExtendedProperties='Excel8.0;HDR=No;IMEX=1'";
System.Data.OleDb.OleDbConnectionmyConn=newSystem.Data.OleDb.OleDbConnection(strCon);
stringstrCom="SELECT*FROM["+dbName+"$]";
myConn.Open();
System.Data.OleDb.OleDbDataAdaptermyCommand=newSystem.Data.OleDb.OleDbDataAdapter(strCom,myConn);
//创建一个DataSet对象
DataSetmyDataSet=newDataSet();
//得到自己的DataSet对象
myCommand.Fill(myDataSet);
//关闭此数据链接
myConn.Close();
returnmyDataSet;
}
先上传EXCEL文件,然后读入DATASET放入GridView,然后从GridView提交到数据库,这样就没有问题。
文件上传控件:
Form里不需要enctype="multipart/form-data"
文件上传代码:
if(myFile.PostedFile.FileName!
="")
{
//上传文件的绝对路径
stringsFile=myFile.PostedFile.FileName;
//获取文件全名
sFile=sFile.Substring(sFile.LastIndexOf("\\")+1);
//获取后缀名
sFile=sFile.Substring(sFile.LastIndexOf("."));
if(sFile.ToLower()!
=".xls")
{
Response.Write("请选择Excel文件!
");
Response.End();
}
//为了防止重名,获得日期为文件名年月日时分秒毫秒
stringdatatime=System.DateTime.Now.ToString("yyyMMddHHmmssffff");
//上传后文件的新名
sFile=datatime+sFile;
//AppDomain.CurrentDomain.BaseDirectory.ToString()获取此项目的根目录
//sPath获取上传后的路径
stringsPath=AppDomain.CurrentDomain.BaseDirectory.ToString()+"ExcelFiles\\"+sFile;
//上传文件
myFile.PostedFile.SaveAs(sPath);
this.myGridView.DataSource=GetExcelContent(sPath);
this.myGridView.DataBind();
}
读取EXCEL到DATASET代码:
privateDataSetGetExcelContent(stringfilepath)
{
stringstrCon="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="+filepath+";ExtendedProperties='Excel8.0;HDR=No;IMEX=1'";
System.Data.OleDb.OleDbConnectionmyConn=newSystem.Data.OleDb.OleDbConnection(strCon);
stringstrCom="SELECTF1asresno,F2asresnameFROM[Sheet1$]";
myConn.Open();
System.Data.OleDb.OleDbDataAdaptermyCommand=newSystem.Data.OleDb.OleDbDataAdapter(strCom,myConn);
//创建一个DataSet对象
DataSetmyDataSet=newDataSet();
//得到自己的DataSet对象
myCommand.Fill(myDataSet);
//关闭此数据链接
myConn.Close();
returnmyDataSet;
}
最后是数据提交到数据库代码:
stringstresno="";
stringstresname="";
foreach(GridViewRowrowinthis.myGridView.Rows)
{
Labeltxtesno=(Label)row.FindControl("labresno");
stresno+=txtesno.Text.ToString().Trim().Replace("'","''")+";";
Labeltxtresname=(Label)row.FindControl("labresname");
stresname+=txtresname.Text.ToString().Trim().Replace("'","''")+";";
}
Response.Write(stresno+"
"+stresname);
Response.End();
导出excel
//将页面数据导出到excel表格中去
//导出按钮单击事件
protectedvoidbtndaochu_Click(objectsender,EventArgse)
{
Export("application/ms-excel","教务维护表.xls");
}
privatevoidExport(stringFileType,stringFileName)
{
myGridView.AllowPaging=false;//清除分页
myGridView.AllowSorting=false;//清除排序
this.myGridView.Columns[1].Visible=false;//隐藏编辑列
this.myGridView.Columns[0].Visible=false;//隐藏删除列
bind();//绑定数据源
Response.Clear();
Response.Buffer=true;
Response.Charset="GB2312";
Response.ContentEncoding=System.Text.Encoding.UTF7;
Response.AppendHeader("Content-Disposition","attachment;filename="+HttpUtility.UrlEncode(FileName,Encoding.UTF8).ToString());
Response.ContentType=FileType;
this.EnableViewState=false;
StringWritertw=newStringWriter();
HtmlTe
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- GridView 分页 导出 数据