qq_38875363 2018-10-10 04:59 采纳率: 25%
浏览 407

gridview分页按条件查询时列名无效怎么办?分页没问题就是按主管查询就报UserName列名不存在

sql语句

 if exists (select * from sys.procedures where name='proc_page')
drop proc proc_page
go

create proc proc_page  @beginIndex int,@endIndex int,@where varchar(100)
as
    declare @sql varchar(1000)
    set @sql='select * from (select ROW_NUMBER() over(order by a.DeptID) rowIndex,a.*,b.UserName from Department a left join UserInfo b on a.ManagerID=b.UserID where 1=1 '+@where+') c where rowIndex between '+str(@beginIndex)+' and '+str(@endIndex)
    exec(@sql)
go

给gridview绑定数据

 public static DataTable SelectDepartment(int PageIndex, int PageSize, string DeptName,string UserName, out int totalCount)
        {
            string sql = "exec proc_page @begIndex,@endIndex,@where";
            string where = string.Empty;
            if (!string.IsNullOrEmpty(DeptName))
            {
                where += string.Format(" and DeptName='{0}'", DeptName);
            }
            if (UserName != "请选择")
            {
                where += string.Format(" and UserName='{0}'", UserName);
            }
            int begIndex = (PageIndex - 1) * PageSize + 1;
            int endIndex = PageSize * PageIndex;

            SqlParameter[] ps =
            {
                new SqlParameter("@begIndex",begIndex),
                new SqlParameter("@endIndex",endIndex),
                new SqlParameter("@where",where)
            };
            //string sql = string.Format("select * from (select ROW_NUMBER() over(order by a.DeptID) rowIndex,a.*,b.UserName from Department a left join UserInfo b on a.ManagerID=b.UserID where 1=1 {0}) c where rowIndex between {1} and {2}",where,begIndex,endIndex);
            string sqlCount = string.Format("select * from Department where 1=1 {0}",where);
            totalCount = int.Parse((DBHelper.GetDataTable1(sqlCount).Rows.Count.ToString()));

            return DBHelper.GetDataTable(sql,ps);
        }

后台绑定

 public void BindGv()
        {
            string DeptName = this.txtDeptName.Text.Trim();
            string UserName = this.DDLUserName.SelectedItem.ToString();
            int totalCount;
            this.GridView1.DataSource = BLL.Department_BLL.selectDepartment(PageIndex, PageSize, DeptName, UserName, out totalCount);
            this.GridView1.DataBind();
            this.Label1.Text = "页数:" + PageIndex.ToString();
            this.Label2.Text = "总记录行数:" + totalCount.ToString();
            ViewState["TotalCount"] = totalCount;
        }

图片说明

数据库

展开全部

  • 写回答

1条回答 默认 最新

  • qq1411090538 2018-10-10 09:38
    关注

    if (UserName != "请选择")
    {
    where += string.Format(" and UserName='{0}'", UserName);
    }
    这里出错 不懂加我

    评论
编辑
预览

报告相同问题?

悬赏问题

  • ¥15 宝塔面板一键迁移使用不了
  • ¥15 求一个按键录像存储到内存卡的ESP32CAM代码
  • ¥15 如何单独修改下列canvas推箱子代码target参数?,插入图片代替其形状,就是哪个绿色的圆圈每关用插入的图片替代
  • ¥20 四叉树的创建和输出问题
  • ¥15 javaweb连接数据库,jsp文件加载不出来
  • ¥15 matlab关于高斯赛德尔迭代的应用编撰。(相关搜索:matlab代码|迭代法)
  • ¥15 损失匹配问题,求解答
  • ¥15 3500常用汉字书法体检测数据集下载
  • ¥15 odoo17在制造模块或采购模块良品与次品如何分流和在质检模块下如何开发
  • ¥15 Qt音乐播放器的音乐文件相对路径怎么写