zoukankan      html  css  js  c++  java
  • 关于DataGrid最后一页只有一行记录时,删除此记录出错的问题

    一般在DataGrid中使用删除功能的时候,我们有时可能会遇到这样的情况:当我们执行一般的删除时可以正常删除,但是当最后一页只有一行记录时,这时我们删除此记录会出现下面的错误提示:
    Invalid CurrentPageIndex value. It must be >= 0 and < the PageCount.
    aa这是因为当我们删除后,DataGrid的总页数(PageCount)减少了,但是当前页的索引(CurrentPageIndex)还是以前的值,这样就出现了上面的异常。
    解决方法:
    我们可以不需要修改一些事件的代码,可以在绑定数据的时候,判断一下当前页的索引:
    private void GetData()
    {
        
        SqlConnection cn = new SqlConnection(ConfigurationSettings.AppSettings["con"]);
        string str = "select * from Table1";
        SqlDataAdapter da = new SqlDataAdapter(str, cn);
        DataSet ds = new DataSet();
        da.Fill(ds);
        this.DataGrid1.DataSource = ds;
        this.DataGrid1.DataKeyField = "id";
        try
        {
            this.DataGrid1.DataBind();
        }
        catch
        {
            //判断当前页的索引是否正确
            if(DataGrid1.CurrentPageIndex < 0 || DataGrid1.CurrentPageIndex>=DataGrid1.PageCount)
            {
                DataGrid1.CurrentPageIndex = DataGrid1.CurrentPageIndex -1;
            }
            this.DataGrid1.DataBind();
        }
    }
  • 相关阅读:
    linux下面安装maven
    go 安装
    linux scp 服务器远程拷贝
    git有merge时如何删除分支
    influxdb ERR: error parsing query: found -, expected
    influxDB学习总结
    influxdb Measurements
    go exec: "gcc": executable file not found in %PATH%
    php计算脚本执行时间
    安装nodejs和grunt以后出现 /usr/bin/env: node: No such file or directory
  • 原文地址:https://www.cnblogs.com/pingkeke/p/387423.html
Copyright © 2011-2022 走看看