GridView控件如何在CheckBox选择之后将该行数据隐藏

qq_35280214 2017-05-23 02:52:56
后台代码
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.Data.SqlClient;
using System.Linq;
using System.Xml.Linq;
using Club.Model;

public partial class GL_HDYY : PageBase
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
Get_Article();
BindResult();
}
}
private void Get_Article()
{
try
{


gvData.DataSource = GetCodeBy(0);
gvData.DataBind();
}
catch
{

}

}
public DataSet GetCodeBy(int iCount)
{
PubConstant date = new PubConstant();

string strTop = "";

if (iCount > 1)
{
strTop = "top " + iCount.ToString();
}
string sql = "select " + strTop + " * from Activity where [open]='是' ";
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);

SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = null;
try
{
con.Open();
ds = new DataSet();
da.Fill(ds);

}
catch (SqlException ex)
{
throw ex;
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return ds;
}

#region 数据绑定
/// <summary>
///绑定列表
/// </summary>
private void BindResult()
{

//组合查询条件
string strSqlWhere = string.Empty;
if (this.KSCX.SelectedValue.Trim() != "全部")
{
if (this.txtCXNR.Text != "")
{
strSqlWhere += " and " + this.KSCX.SelectedValue.Trim() + " like '%" + this.txtCXNR.Text.Trim() + "%'";
}
}
T_HD modelList = new T_HD();
DataTable dt = modelList.GetListINFO(strSqlWhere).Tables[0];
if (dt != null)
{
this.gvData.DataKeyNames = new string[] { "ActID" };
this.gvData.DataSource = dt;
this.gvData.DataBind();
//ShowStats(dt.Rows.Count.ToString());
}
}
#endregion
#region 设置翻页按钮事件
public void PagerButtonClick(object sender, EventArgs e)
{
string arg = ((LinkButton)sender).CommandArgument.ToString();
switch (arg)
{
case "next":
if (gvData.PageIndex < (this.gvData.PageCount - 1))
{
gvData.PageIndex += 1;
}
break;
case "prev":
if (gvData.PageIndex > 0)
{
gvData.PageIndex -= 1;
}
break;
case "last":
if (gvData.PageCount != 0)
{
gvData.PageIndex = (gvData.PageCount - 1);
}
else
{
gvData.PageIndex = 0;
}
break;
default:
gvData.PageIndex = System.Convert.ToInt32(arg);
break;
}
//绑定列表
BindResult();

}
#endregion

#region 按钮事件
/// <summary>
/// 查询
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
protected void BtnSearch_Click(object sender, ImageClickEventArgs e)
{
BindResult();
}
protected void btnSelectAll_Click(object sender, EventArgs e)
{

Button btn = (Button)sender;
bool isOK = false;
if (btn.Text == "全选")
{
isOK = true;
//找到PagerTemplate中的全选按钮
btnSelectAll.Text = "取消全选";
}
else
{
btnSelectAll.Text = "全选";
}

foreach (GridViewRow row in gvData.Rows)
{
((CheckBox)row.Cells[0].FindControl("chkSelect")).Checked = isOK;
}
}
protected void btnDel_Click(object sender, EventArgs e)
{
for (int rowIndex = 0; rowIndex < gvData.Rows.Count; rowIndex++)
{
if (((CheckBox)gvData.Rows[rowIndex].Cells[0].FindControl("chkSelect")).Checked)
{

if(DelCode(Convert.ToInt32(gvData.DataKeys[rowIndex].Value)))
{
gvData.Rows[rowIndex].Visible = false;
}

if (UpdateRenShu(Convert.ToInt32(gvData.DataKeys[rowIndex].Value)))
{

}
}
}
BindResult();
ResponseMessage(this.Page, "申请成功!");
}
public bool @DelCode(int id)
{
bool isOK = false;
//string sql = "insert into Reservation(ClassID,artID,artName,classname,classmajor,classphone)values('" + Session["ID"].ToString() + "','" + id + "','" + Session["Artname"].ToString() + "','" + Session["Classname"].ToString() + "','" + Session["Classpmajor"].ToString() + "','" + Session["Classphone"].ToString() + "')";
string sql = "insert into Reservation(UserID,actID)values('" + Session["ID"].ToString() + "','" + id + "')";
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);

try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
isOK = true;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
return isOK;

}
public bool UpdateRenShu(int id)
{
bool isOK = false;
string sql = "update Activity set ActNumber=ActNumber+1 where id=" + id;
SqlConnection con = new SqlConnection(PubConstant.ConnectionString);
SqlCommand cmd = new SqlCommand(sql, con);
try
{
con.Open();
if (cmd.ExecuteNonQuery() > 0)
isOK = true;
}
catch (SqlException ex)
{
throw ex;
}
finally
{
con.Close();
}
return isOK;
}

protected void gvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
gvData.PageIndex = e.NewPageIndex;
BindResult();
}
}







...全文
209 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
水哥阿乐 2017-05-25
  • 打赏
  • 举报
回复
类似于这种判断 ((CheckBox)row.Cells[0].FindControl("chkSelect")).Checked = isOK;再作 一次数据筛选再将基赋给dt就行了,dt变了,数据自然变了
  • 打赏
  • 举报
回复
假设你打勾(选择)的目的就是确认其“申请成功”,那么你应该数据设计中就有这个对应的业务字段,并且在你的 BindResult 中就只是查询申请尚未成功的那些记录。不应该“申请成功”只在界面 GridView 上做设计,应该从界面一直更新到底层数据库。
dqzzzz1 2017-05-25
  • 打赏
  • 举报
回复
触发更新事件后,然后重新绑定GridView
紫魂一号 2017-05-23
  • 打赏
  • 举报
回复
直接前台脚本控制吧。通过checkbox 找到它的父对象的父对象(row), 然后对这个对象写样式 display:none
  • 打赏
  • 举报
回复
数据源里直接过滤掉不就行了?增加个标志位对应复选框
文档包内容: 1.通过自己建立DataTable对象,往数据源中插入数据 4.通过泛型来获取数据 (这个需要好好的理解) 5.使用SqlDataSource,ObjectDataSource作为数据控件的用法 6.通过使用缓存来处理数据 7.关于FormView控件的方法 8.数据控件的分页 9.在Gridview中对数据进行排序 10.关于如何处理Checkboxlist的方法 11.在Gridview中使用DropDownLIst的问题。 12.鼠标移动变色 13.关于在Gridview中的单元格中实现用“...”代替超长字符串 14.数据控件数据强制换行显示 15.关于隐藏Gridview中的某一列数据 16.通过checkbox实现全选和反选的功能 17.关于数据突出显示 18.使用xml作为数据源的方法(内联,外联,数据库读取)(treeview,listbox,gridview,repeater) 19.GridView中的方法使用(格式,DataFormatString),设置行列属性 当满足条件后,隐藏某些ImageButton 20.GridView控件的模板列使用方法 21.当数据的值小于?时,显示不同的图标 22.关于在GridView中显示摘要(这个没有搞懂) 23.使用主从来显示数据GridView,DetailView) 24.显示主题 25.通过编程的方式创建DataSet对象 26.采取独立提供程序的方法执行动态查询 27.异步执行命令概述(这个暂时没有弄懂,需要一些时间搞懂) 28.多活动结果集(MARS,解释为在单个连接上执行多个查询工作,而不是重复的开关数据库的连接,这个在查询多个数据的时候很有用,它的功能并不能并行执行查询,只是串行执行多个查询而已) 29.关于普通和异步执行方式获取xml数据 30. 将xml文件作为参数传递给数据库表中 31.Eval和Bind绑定数据 32.启用DataTable中Autoincrementing字段 33. DataSet找到某一行的对该行中的第几列进行修改 34.创建DataTable,并获取某一行数据 35.将xml数据读入DataTable 36.DataTable中数据记录的统计 37.合并两个结构一样的DataTable 38.在DataTable中排序 39.将数据写入excel中,从excel读取数据 40.页眉模板列隐藏GridView控件的行 41.泛型与类结合使用来绑定数据,并添加到数据控件中 42.数据读取到treeview中,指定日期下添加相应时间 这些都是我自己学习积累的宝贵经验,希望对大家能有所帮助!
控件使用
1、鼠标经过行的时候改变该行的样式,鼠标离开行的时候恢复该行的样式
使用方法(设置属性):
MouseOverCssClass - 鼠标经过行时行的 CSS 类名

2、对多个字段进行复合排序;升序、降序的排序状态提示
使用方法(设置SmartSorting复合属性):
AllowSortTip - 是否启用排序提示
AllowMultiSorting - 是否启用复合排序
SortAscImageUrl - 升序提示图片的URL(不设置则使用默认图片)
SortDescImageUrl - 降序提示图片的URL(不设置则使用默认图片)
SortAscText - 升序提示文本
SortDescText - 降序提示文本

3、根据按钮的CommandName设置其客户端属性
使用方法(设置ClientButtons集合属性):
BoundCommandName - 需要绑定的CommandName
AttributeKey - 属性的名称
AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text)
Position - 属性的值的位置

4、联动复选框(复选框的全选和取消全选)。选中指定的父复选框,则设置指定的所有子复选框为选中状态;取消选中指定的父复选框,则设置指定的所有子复选框为取消选中状态。如果指定的所有子复选框为均选中状态,则设置指定的父复选框为选中状态;如果指定的所有子复选框至少有一个为取消选中状态,则设置指定的父复选框为取消选中状态
使用方法(设置CascadeCheckboxes集合属性):
ParentCheckboxID - 模板列中 父复选框ID
ChildCheckboxID - 模板列中 子复选框ID
YYControls.Helper.SmartGridView中的静态方法
List GetCheckedDataKey(GridView gv, int columnIndex)
List GetCheckedDataKey(GridView gv, string checkboxId)

5、固定指定行、指定列,根据RowType固定行,根据RowState固定行
使用方法(设置FixRowColumn复合属性):
FixRowType - 需要固定的行的RowType(用逗号“,”分隔)
FixRowState - 需要固定的行的RowState(用逗号“,”分隔)
FixRows - 需要固定的行的索引(用逗号“,”分隔)
FixColumns - 需要固定的列的索引(用逗号“,”分隔)
TableWidth - 表格的宽度
TableHeight - 表格的高度

6、响应行的单击事件和双击事件,并在服务端处理
使用方法(设置属性):
BoundRowClickCommandName - 行的单击事件需要绑定的CommandName
BoundRowDoubleClickCommandName - 行的双击事件需要绑定的CommandName

7、行的指定复选框选中的时候改变该行的样式,行的指定复选框取消选中的时候恢复该行的样式
使用方法(设置CheckedRowCssClass复合属性):
CheckBoxID - 模板列中 数据行的复选框ID
CssClass - 选中的行的 CSS 类名

8、导出数据源的数据为Excel、Word或Text(应保证数据源的类型为DataTable或DataSet)
使用方法:
为SmartGridView添加的方法
Export(string fileName)
Export(string fileName, ExportFormat exportFormat)
Export(string fileName, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, ExportFormat exportFormat, Encoding encoding)
Export(string fileName, int[] columnIndexList, string[] headers, ExportFormat exportFormat, Encoding encoding)

9、给数据行增加右键菜单,响应服务端事件或超级链接
使用方法(设置ContextMenus集合属性):
Text - 菜单的文本内容
BoundCommandName - 需要绑定的CommandName
NavigateUrl - 链接的URL
Target - 链接的目标窗口或框架
SmartGridView的属性ContextMenuCssClass - 右键菜单的级联样式表 CSS 类名(右键菜单的结构div ul li a)

10、自定义分页样式。显示总记录数、每页记录数、当前页数、总页数、首页、上一页、下一页、末页和分页按钮
使用方法(设置CustomPagerSettings复合属性):
PagingMode - 自定义分页的显示模式
TextFormat - 自定义分页的文本显示样式(四个占位符:{0}-每页显示记录数;{1}-总记录数;{2}-当前页数;{3}-总页数)

11、合并指定列的相邻且内容相同的单元格
使用方法(设置属性):
MergeCells - 需要合并单元格的列的索引(用逗号“,”分隔)
控件使用 1、鼠标经过行的时候改变该行的样式,鼠标离开行的时候恢复该行的样式 使用方法(设置属性): MouseOverCssClass - 鼠标经过行时行的 CSS 类名 2、对多个字段进行复合排序;升序、降序的排序状态提示 使用方法(设置SmartSorting复合属性): AllowSortTip - 是否启用排序提示 AllowMultiSorting - 是否启用复合排序 SortAscImageUrl - 升序提示图片的URL(不设置则使用默认图片) SortDescImageUrl - 降序提示图片的URL(不设置则使用默认图片) SortAscText - 升序提示文本 SortDescText - 降序提示文本 3、根据按钮的CommandName设置其客户端属性 使用方法(设置ClientButtons集合属性): BoundCommandName - 需要绑定的CommandName AttributeKey - 属性的名称 AttributeValue - 属性的值(两个占位符:{0} - CommandArgument;{1} - Text) Position - 属性的值的位置 4、联动复选框(复选框的全选和取消全选)。选中指定的父复选框,则设置指定的所有子复选框为选中状态;取消选中指定的父复选框,则设置指定的所有子复选框为取消选中状态。如果指定的所有子复选框为均选中状态,则设置指定的父复选框为选中状态;如果指定的所有子复选框至少有一个为取消选中状态,则设置指定的父复选框为取消选中状态 使用方法(设置CascadeCheckboxes集合属性): ParentCheckboxID - 模板列中 父复选框ID ChildCheckboxID - 模板列中 子复选框ID CashBox.Helper.WebGrid中的静态方法 List GetCheckedDataKey(GridView gv, int columnIndex) List GetCheckedDataKey(GridView gv, string checkboxId) 5、固定指定行、指定列,根据RowType固定行,根据RowState固定行 使用方法(设置FixRowColumn复合属性): FixRowType - 需要固定的行的RowType(用逗号“,”分隔) FixRowState - 需要固定的行的RowState(用逗号“,”分隔) FixRows - 需要固定的行的索引(用逗号“,”分隔) FixColumns - 需要固定的列的索引(用逗号“,”分隔) TableWidth - 表格的宽度 TableHeight - 表格的高度 6、响应行的单击事件和双击事件,并在服务端处理 使用方法(设置属性): BoundRowClickCommandName - 行的单击事件需要绑定的CommandName BoundRowDoubleClickCommandName - 行的双击事件需要绑定的CommandName 7、行的指定复选框选中的时候改变该行的样式,行的指定复选框取消选中的时候恢复该行的样式 使用方法(设置CheckedRowCssClass复合属性): CheckBoxID - 模板列中 数据行的复选框ID CssClass - 选中的行的 CSS 类名 8、导出数据源的数据为Excel、Word或Text(应保证数据源的类型为DataTable或DataSet) 使用方法: 为WebGrid添加的方法 Export(string fileName) Export(string fileName, ExportFormat exportFormat) Export(string fileName, ExportFormat exportFormat, Encoding encoding) Export(string fileName, int[] columnIndexList, ExportFormat exportFormat, Encoding encoding) Export(string fileName, int[] columnIndexList, string[] headers, ExportFormat exportFormat, Encoding encoding) Export(string fileName, string[] columnNameList, ExportFormat exportFormat, Encoding encoding) Export(string fileName, strin

62,025

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧