您的位置:首页 > 其它

【转载】gridview修改,添加,删除…

2014-08-07 13:22 465 查看
cs代码:








using System;


using System.Data;


using System.Configuration;


using System.Collections;


using System.Web;


using System.Web.Security;


using System.Web.UI;


using System.Web.UI.WebControls;


using System.Web.UI.WebControls.WebParts;


using System.Web.UI.HtmlControls;




using System.Globalization;


using System.Data.OracleClient;


using System.IO;


using System.Text;


using System.Text.RegularExpressions;




using Hhxx.App_Code.DatabaseOperate;


using Hhxx.App_Code.Database;


using Hhxx.App_Code.Common;






public partial class InterFace_RenShi_PeiXunJinXiu : System.Web.UI.Page


{




//SELECT 建立日期,员工编号,姓名,性别,民族,籍贯,出生日期,政治面貌,婚姻状况,学历,户口,身份证号,职称,所属部门,岗位,入厂时间,转正时间,合同到期,用工形式,档案资料,工作状态,是否涉密,薪金,电话,手机,电子邮件,QQ号,简介,备注,经办人,SN


private String querySql = "SELECT 建立日期,员工编号,姓名,性别,民族,籍贯,出生日期,政治面貌,婚姻状况,学历,户口,身份证号,职称,所属部门,岗位,入厂时间,转正时间,合同到期,用工形式,档案资料,工作状态,是否涉密,薪金,电话,手机,电子邮件,QQ号,经办人,SN FROM renshixinxi ";




protected void Page_Load(object sender, EventArgs e)


{


this.MenuCaidan.Items[0].Enabled = true;


this.MenuCaidan.Items[1].Enabled = false;




if (!Page.IsPostBack)


{




dropDownListInit();


// 初始化默认Gridview的SQL语句




fillGridView(querySql);


}




// gridview 换行控制


this.GridViewXinXiLan.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");


}




/// <summary>


/// 点击菜单


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


protected void MenuCaidan_MenuItemClick(object sender, MenuEventArgs e)


{


switch (MenuCaidan.SelectedItem.Text.Trim())


{


case "人事栏":


{


MultiViewXinXiGuanLi.ActiveViewIndex = 0;




fillGridView(querySql);


break;


}




case "培训进修": MultiViewXinXiGuanLi.ActiveViewIndex = 1;


break;




default: break;


}


}




/// <summary>


/// 条件查询


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


protected void ButtonTiJianChaXun_Click(object sender, EventArgs e)


{


String strCondition = "";


String strChoose = "";




if (TextBoxOne.Text.Trim().Length != 0)


{


String tempStr = DropDownListTiaoJianOne.SelectedItem.Value;


switch (tempStr)


{


case "包含": strChoose = " LIKE ";


break;


case "不包含": strChoose = " NOT LIKE ";


break;


}




if (strCondition.Length == 0)


{


strCondition = strCondition + DropDownListLeiBeiOne.SelectedItem.Value + strChoose + "'%" + TextBoxOne.Text.Trim() + "%'";


}


else


{


strCondition = strCondition + "AND " + DropDownListLeiBeiOne.SelectedItem.Value + strChoose + "'%" + TextBoxOne.Text.Trim() + "%'";


}


}




if (TextBoxTwo.Text.Trim().Length != 0)


{


String tempStr = DropDownListTiaoJianTwo.SelectedItem.Value;


switch (tempStr)


{


case "包含": strChoose = " LIKE ";


break;


case "不包含": strChoose = " NOT LIKE ";


break;


}




if (strCondition.Length == 0)


{


strCondition = strCondition + DropDownListLeiBeiTwo.SelectedItem.Value + strChoose + "'%" + TextBoxTwo.Text.Trim() + "%'";


}


else


{


strCondition = strCondition + "AND " + DropDownListLeiBeiTwo.SelectedItem.Value + strChoose + "'%" + TextBoxTwo.Text.Trim() + "%'";


}


}




if (TextBoxThree.Text.Trim().Length != 0)


{


String tempStr = DropDownListTiaoJianThree.SelectedItem.Value;


switch (tempStr)


{


case "等于": strChoose = "=";


break;


case "大于": strChoose = ">";


break;


case "小于": strChoose = "<";


break;


}




if (strCondition.Length == 0)


{


strCondition = strCondition + DropDownListLeiBeiThree.SelectedItem.Value + strChoose + TextBoxThree.Text.Trim();


}


else


{


strCondition = strCondition + "AND " + DropDownListLeiBeiThree.SelectedItem.Value + strChoose + TextBoxThree.Text.Trim();


}


}




if (TextBoxFour.Text.Trim().Length != 0)


{


String tempStr = DropDownListTiaoJianFour.SelectedItem.Value;


switch (tempStr)


{


case "等于": strChoose = "=";


break;


case "大于": strChoose = ">";


break;


case "小于": strChoose = "<";


break;


}


if (strCondition.Length == 0)


{


strCondition = strCondition + DropDownListLeiBeiFour.SelectedItem.Value + strChoose + TextBoxFour.Text.Trim();


}


else


{


strCondition = strCondition + "AND " + DropDownListLeiBeiFour.SelectedItem.Value + strChoose + TextBoxFour.Text.Trim();


}


}




if (strCondition.Length != 0)


{


strCondition = " where " + strCondition;


}




strCondition = strCondition + " ORDER BY " + DropDownListPaiXuFangShi.SelectedItem.Value.Trim();


String strSql = querySql + strCondition;


String mQuerySql = strSql;


// 填充表格


fillGridView(mQuerySql);


// 保存SQL语句


ViewState["sql"] = mQuerySql;


}






/// <summary>


/// 初始化DropDownList


/// </summary>


private void dropDownListInit()


{




GetTableInformation getTableInformation = new GetTableInformation();




// 获取表结构信息


Hashtable tableInfor = getTableInformation.getTableInfor("renshixinxi");




// 移除SN


tableInfor.Remove("SN");




foreach (DictionaryEntry de in tableInfor)


{


String key = de.Key.ToString();


String value = de.Value.ToString();




switch (value)


{


case "CHAR":


DropDownListLeiBeiOne.Items.Add(key);


DropDownListLeiBeiTwo.Items.Add(key);


// 排序方式下拉框添加数据字段


DropDownListPaiXuFangShi.Items.Add(key);


break;




case "VARCHAR2":


DropDownListLeiBeiOne.Items.Add(key);


DropDownListLeiBeiTwo.Items.Add(key);


// 排序方式下拉框添加数据字段


DropDownListPaiXuFangShi.Items.Add(key);


break;




case "NUMBER":


DropDownListLeiBeiThree.Items.Add(key);


DropDownListLeiBeiFour.Items.Add(key);


// 排序方式下拉框添加数据字段


DropDownListPaiXuFangShi.Items.Add(key);


break;


}




}




DropDownListTiaoJianOne.Items.Add("包含");


DropDownListTiaoJianOne.Items.Add("不包含");




DropDownListTiaoJianTwo.Items.Add("包含");


DropDownListTiaoJianTwo.Items.Add("不包含");




DropDownListTiaoJianThree.Items.Add("等于");


DropDownListTiaoJianThree.Items.Add("小于");


DropDownListTiaoJianThree.Items.Add("大于");




DropDownListTiaoJianFour.Items.Add("等于");


DropDownListTiaoJianFour.Items.Add("小于");


DropDownListTiaoJianFour.Items.Add("大于");




if (DropDownListLeiBeiOne.Items.Count == 0)


{


this.DropDownListLeiBeiOne.Visible = false;


this.DropDownListTiaoJianOne.Visible = false;


this.TextBoxOne.Visible = false;




}




if (DropDownListLeiBeiTwo.Items.Count == 0)


{


this.DropDownListLeiBeiTwo.Visible = false;


this.DropDownListTiaoJianTwo.Visible = false;


this.TextBoxTwo.Visible = false;




}




if (DropDownListLeiBeiThree.Items.Count == 0)


{


this.DropDownListLeiBeiThree.Visible = false;


this.DropDownListTiaoJianThree.Visible = false;


this.TextBoxThree.Visible = false;


}




if (DropDownListLeiBeiFour.Items.Count == 0)


{


this.DropDownListLeiBeiFour.Visible = false;


this.DropDownListTiaoJianFour.Visible = false;


this.TextBoxFour.Visible = false;




}




}




/// <summary>


/// 填充GridView


/// </summary>


private void fillGridView(String querySql)


{


DataSet ds = new DataSet();


ds = GetData(querySql);




// 清空以前的数据源


GridViewXinXiLan.DataSource = null;




// 将ds作为信息栏GridView的数据源


GridViewXinXiLan.DataSource = ds;




// 绑定数据


GridViewXinXiLan.DataBind();




// 分页的相关参数设置


getRefSet(ds);




// 保存数据源 防止丢失 VS的破Bug


ViewState["GridViewXinXiLan_DataSource"] = GridViewXinXiLan.DataSource;


// 保存SQL语句


ViewState["sql"] = querySql;


}






/// <summary>


/// 填充GridView


/// </summary>


private void fillGridViewTianJia(String querySql)


{


DataSet ds = new DataSet();


ds = GetData(querySql);




// 清空以前的数据源


GridViewTianJia.DataSource = null;




// 将ds作为信息栏GridView的数据源


GridViewTianJia.DataSource = ds;




// 绑定数据


GridViewTianJia.DataBind();




// 分页的相关参数设置


getRefSet(ds);




// 保存数据源 防止丢失 VS的破Bug


ViewState["GridViewTianJia_DataSource"] = this.GridViewTianJia.DataSource;


// 保存SQL语句


ViewState["sql"] = querySql;


// gridview 换行控制


this.GridViewTianJia.Attributes.Add("style", "word-break:keep-all;word-wrap:normal");


}




/// <summary>


/// 转换SQL查询结果为DataSet


/// </summary>


/// <returns>ds</returns>


private DataSet GetData(String querySql)


{


String connStr = OracleHelper.DatabaseConnStr;




// 创建数据集


DataSet ds = OracleHelper.ExeSqlForDataSet(connStr, CommandType.Text, querySql, null);




return ds;


}






/// <summary>


/// 首页,上一页,下一页,尾页公用的点击程序


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


protected void PagerButtonClick(object sender, EventArgs e)


{




// CommandArgument对应该对象在页面初始化后的设定值


// 减1 是因为系统默认首页的PageIndex为0,为了符合人们的正常思维而使用


// 重新绑定


GridViewXinXiLan.DataSource = ViewState["GridViewXinXiLan_DataSource"];


GridViewXinXiLan.PageIndex = Convert.ToInt32(((LinkButton)sender).CommandArgument) - 1;


GridViewXinXiLan.DataBind();


// 更新当前页面显示值


lblCurrentIndex.Text = "第 " + (GridViewXinXiLan.PageIndex + 1).ToString() + " 页";




// 更新参数


lnkbtnPre.CommandArgument = (GridViewXinXiLan.PageIndex == 0 ? "1" : GridViewXinXiLan.PageIndex.ToString());


lnkbtnNext.CommandArgument = (GridViewXinXiLan.PageCount == 1 ? GridViewXinXiLan.PageCount.ToString() : (GridViewXinXiLan.PageIndex + 2).ToString());




}




/// <summary>


/// 分页的相关参数设置


/// </summary>


/// <param name="ds"></param>


protected void getRefSet(DataSet ds)


{


lblCurrentIndex.Text = "第 " + (GridViewXinXiLan.PageIndex + 1).ToString() + " 页";


lblPageCount.Text = "共 " + GridViewXinXiLan.PageCount.ToString() + " 页";


lblRecordCount.Text = "总共 " + ds.Tables[0].Rows.Count.ToString() + " 条";




if (ds.Tables[0].Rows.Count == 0)


{


lnkbtnFirst.Visible = false;


lnkbtnPre.Visible = false;


lnkbtnNext.Visible = false;


lnkbtnLast.Visible = false;




lblCurrentIndex.Visible = false;


lblPageCount.Visible = false;


lblRecordCount.Visible = false;


this.lnkbtnJumpPage.Visible = false;


this.txtJumpPage.Visible = false;






}


else if (GridViewXinXiLan.PageCount == 1)


{


lnkbtnFirst.Visible = false;


lnkbtnPre.Visible = false;


lnkbtnNext.Visible = false;


lnkbtnLast.Visible = false;




}




// 设置LinkButton此时对应的参数


lnkbtnFirst.CommandArgument = "1";


lnkbtnPre.CommandArgument = (GridViewXinXiLan.PageIndex == 0 ? "1" : GridViewXinXiLan.PageIndex.ToString());


lnkbtnNext.CommandArgument = (GridViewXinXiLan.PageCount == 1 ? GridViewXinXiLan.PageCount.ToString() : (GridViewXinXiLan.PageIndex + 2).ToString());


lnkbtnLast.CommandArgument = GridViewXinXiLan.PageCount.ToString();




}




/// <summary>


/// 跳转到指定页面,页面参数由txtJumpPage获取


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


protected void lnkbtnJumpPage_Click(object sender, EventArgs e)


{


// 减1 是因为系统默认首页的PageIndex为0,为了符合人们的正常思维而使用


// 重新绑定


GridViewXinXiLan.DataSource = ViewState["GridViewXinXiLan_DataSource"];


GridViewXinXiLan.PageIndex = int.Parse(txtJumpPage.Text) - 1;


GridViewXinXiLan.DataBind();


// 更新当前页面显示值


lblCurrentIndex.Text = "第 " + (GridViewXinXiLan.PageIndex + 1).ToString() + " 页";




// 更新参数


lnkbtnPre.CommandArgument = (GridViewXinXiLan.PageIndex == 0 ? "1" : GridViewXinXiLan.PageIndex.ToString());


lnkbtnNext.CommandArgument = (GridViewXinXiLan.PageCount == 1 ? GridViewXinXiLan.PageCount.ToString() : (GridViewXinXiLan.PageIndex + 2).ToString());


}




/// <summary>


/// 解决数据源不能分页的问题


/// </summary>


/// <param name="sender"></param>


/// <param name="e"></param>


protected void GridViewXinXiLan_PageIndexChanging(object sender, GridViewPageEventArgs e)


{


// 读取数据源 设定显示值 并绑定


GridViewXinXiLan.DataSource = ViewState["GridViewXinXiLan_DataSource"];


GridViewXinXiLan.PageIndex = e.NewPageIndex;


GridViewXinXiLan.DataBind();


}



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: