简要的GRIDVIEW

来源:互联网 发布:怎么更新电脑软件 编辑:程序博客网 时间:2024/05/22 03:12

    private static string Ids="";

    protected void Page_Load(object sender,EventArgs e)
    {
        if (!this.IsPostBack)
            BindData("select * from myTable");
    }

    private void BindData(string sql)
    {
        DataSet ds = GetDataSet(sql);
        DataView dv = ds.Tables[0].DefaultView;
        //dv.Sort = "dt desc";
        GridV.DataSource = dv;// GetDataSet(sql);
        GridV.DataBind();
    }

    protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridV.EditIndex = e.NewEditIndex;
        BindData("select * from myTable");

        SetBindedClientScript(e);
    }

    protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridV.EditIndex = -1;
        BindData("select * from myTable");
    }

    private void SetBindedClientScript(GridViewEditEventArgs e)
    {
        string js = @"<script language=javascript>";
        js += "<!-- function SetJs() {";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
        js += "};";
        js += "setTimeout(SetJs,1);";
        js += "//--></script>";

        if (!this.IsStartupScriptRegistered("clientjs"))
            this.RegisterStartupScript("clientjs", js);
    }

    protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
            if(del.Text.ToString()=="删除")
               del.Attributes["onclick"] = "return confirm('确定要删除?')";
        }
    }

    protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
        if (Ids == "")
            Ids += aId;
        else
            Ids += "," + aId;
        string sql = "select * from myTable where id not in(" + Ids.ToString()+")";

        BindData(sql);
    }

    /// <summary>
    ///得到DATASET
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    protected DataSet GetDataSet(string sql)
    {
        string StrCon = "";
        StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
        OleDbConnection Conn = new OleDbConnection(StrCon);
        OleDbCommand Cmd = Conn.CreateCommand();
        Cmd.CommandText =sql;

        Conn.Open();
        //Cmd.ExecuteNonQuery();

        OleDbDataAdapter Adpt = new OleDbDataAdapter();
        Adpt.SelectCommand = Cmd;
        DataSet dst = new DataSet();
        Adpt.Fill(dst);
        Conn.Close();
        return dst;
    }

    protected void btnFirstPage_Click(object sender, EventArgs e)
    {

        GridV.PageIndex =0;
        BindData("select * from myTable");
    }
    protected void btnPreviousPage_Click(object sender, EventArgs e)
    {
        if ((GridV.PageIndex-1) < 0)
            return;
       
        GridV.PageIndex-=1;
        BindData("select * from myTable");
    }
    protected void btnNextPage_Click(object sender, EventArgs e)
    {
        if (GridV.PageIndex > GridV.PageCount)
            return;
       
        GridV.PageIndex += 1;
        BindData("select * from myTable");
    }
    protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridV.PageIndex =GridV.PageCount-1;
        BindData("select * from myTable");
    }
    protected void GridV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridV.PageIndex = e.NewPageIndex;
        BindData("select * from myTable");
    }
    protected void GridV_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataSet ds = GetDataSet("select * from myTable");
        DataView dv = ds.Tables[0].DefaultView;
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "asc";
        else
        {
            if (ViewState["SortDirection"].ToString() == "asc")
                ViewState["SortDirection"] = "desc";
            else
                ViewState["SortDirection"] = "asc";
        }
        dv.Sort = e.SortExpression.ToString() + " " + ViewState["SortDirection"].ToString();
        GridV.DataSource = dv;
        GridV.DataBind();
    private static string Ids="";

    protected void Page_Load(object sender,EventArgs e)
    {
        if (!this.IsPostBack)
            BindData("select * from myTable");
    }

    private void BindData(string sql)
    {
        DataSet ds = GetDataSet(sql);
        DataView dv = ds.Tables[0].DefaultView;
        //dv.Sort = "dt desc";
        GridV.DataSource = dv;// GetDataSet(sql);
        GridV.DataBind();
    }

    protected void GridV_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridV.EditIndex = e.NewEditIndex;
        BindData("select * from myTable");

        SetBindedClientScript(e);
    }

    protected void GridV_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridV.EditIndex = -1;
        BindData("select * from myTable");
    }

    private void SetBindedClientScript(GridViewEditEventArgs e)
    {
        string js = @"<script language=javascript>";
        js += "<!-- function SetJs() {";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".focus();";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".size=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[1].Controls[0].UniqueID + ".maxlength=10;";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[2].Controls[0].UniqueID + ".attachEvent('onclick',isOK);";
        js += "document.all." + GridV.Rows[e.NewEditIndex].Cells[3].Controls[0].UniqueID + ".size=10;";
        js += "};";
        js += "setTimeout(SetJs,1);";
        js += "//--></script>";

        if (!this.IsStartupScriptRegistered("clientjs"))
            this.RegisterStartupScript("clientjs", js);
    }

    protected void GridV_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            LinkButton del=(LinkButton)e.Row.Cells[4].Controls[2];
            if(del.Text.ToString()=="删除")
               del.Attributes["onclick"] = "return confirm('确定要删除?')";
        }
    }

    protected void GridV_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string aId=GridV.DataKeys[e.RowIndex].Value.ToString();
        if (Ids == "")
            Ids += aId;
        else
            Ids += "," + aId;
        string sql = "select * from myTable where id not in(" + Ids.ToString()+")";

        BindData(sql);
    }

    /// <summary>
    ///得到DATASET
    /// </summary>
    /// <param name="sql">sql语句</param>
    /// <returns></returns>
    protected DataSet GetDataSet(string sql)
    {
        string StrCon = "";
        StrCon = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:/Web/FileSite/aSite/DB/aDb.mdb;Persist Security Info=False";
        OleDbConnection Conn = new OleDbConnection(StrCon);
        OleDbCommand Cmd = Conn.CreateCommand();
        Cmd.CommandText =sql;

        Conn.Open();
        //Cmd.ExecuteNonQuery();

        OleDbDataAdapter Adpt = new OleDbDataAdapter();
        Adpt.SelectCommand = Cmd;
        DataSet dst = new DataSet();
        Adpt.Fill(dst);
        Conn.Close();
        return dst;
    }

    protected void btnFirstPage_Click(object sender, EventArgs e)
    {

        GridV.PageIndex =0;
        BindData("select * from myTable");
    }
    protected void btnPreviousPage_Click(object sender, EventArgs e)
    {
        if ((GridV.PageIndex-1) < 0)
            return;
       
        GridV.PageIndex-=1;
        BindData("select * from myTable");
    }
    protected void btnNextPage_Click(object sender, EventArgs e)
    {
        if (GridV.PageIndex > GridV.PageCount)
            return;
       
        GridV.PageIndex += 1;
        BindData("select * from myTable");
    }
    protected void btnLastPage_Click(object sender, EventArgs e)
    {
        GridV.PageIndex =GridV.PageCount-1;
        BindData("select * from myTable");
    }
    protected void GridV_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridV.PageIndex = e.NewPageIndex;
        BindData("select * from myTable");
    }
    protected void GridV_Sorting(object sender, GridViewSortEventArgs e)
    {
        DataSet ds = GetDataSet("select * from myTable");
        DataView dv = ds.Tables[0].DefaultView;
        if (ViewState["SortDirection"] == null)
            ViewState["SortDirection"] = "asc";
        else
        {
            if (ViewState["SortDirection"].ToString() == "asc")
                ViewState["SortDirection"] = "desc";
            else
                ViewState["SortDirection"] = "asc";
        }
        dv.Sort = e.SortExpression.ToString() + " " + ViewState["SortDirection"].ToString();
        GridV.DataSource = dv;
        GridV.DataBind();
 

原创粉丝点击