Gridview控件使用详解

//一加载就填充Gridview控件
    public void Bin()
    {
        string strsql = "select id,title,context,datet From db_guanggao";
        com = new SqlCommand(strsql, con);
        com.CommandType = CommandType.Text;
        da = new SqlDataAdapter();
        da.SelectCommand = com;
        ds = new DataSet();
        try
        {
            da.Fill(ds, "table");
            GridView1.DataSource = ds;
            GridView1.AllowPaging = true;//启动分页
            GridView1.DataBind();
            string info = "页次:<font color=red>";
            info += (this.GridView1.PageIndex + 1) + "</font>/" + this.GridView1.PageCount + "页";
            this.Label1.Text = info;//当前页/总页如:1/5的形式显示
            //记算dataset的初始值
            //计算数据集的下标,数据集的下标也是从0开始的,PageIndex * 2是每页显示2条数据,PageIndex下标也是从0开始的
            int yeshu = this.GridView1.PageIndex * 2;//2是每页要显示的数量是可以随便改的
            //记算最后一个值
            int jie = 0;
            if ((ds.Tables[0].Rows.Count - yeshu) > 2)
            {
                jie = yeshu + 2;
            }
            else
            {
                jie = ds.Tables[0].Rows.Count;//读出数据的总行数
            }
            for (int wu = yeshu; wu < jie; wu++)
            {
                int i = 0;
                if (wu >= 2)
                {
                    int tempNew = wu % 2;
                    i = tempNew;
                }
                else
                {
                    i = wu;
                }
            }
            this.DropDownList1.Items.Clear();
            for (int j = 0; j < this.GridView1.PageCount; j++)
            {
                this.DropDownList1.Items.Add(Convert.ToString(j + 1));
            }
        }
        catch (SqlException ex)
        {
            myLabel.Text = "数据库出错了";
        }
        finally
        {
            ds.Clone();
            com.Clone();
            con.Close();
        }
//下面是把GvridView控件的分页事件,删除事件已经更新事件

    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GridView1.PageIndex = e.NewPageIndex;
        this.Bin();
    }

    protected void Button1_Click(object sender, EventArgs e)//跳到某一页的button事件
    {
        if (DropDownList1.Items.Count != 0)
        {
            string class1 = this.DropDownList1.SelectedValue.ToString();
            GridView1.PageIndex = Convert.ToInt32(class1)-1;
            this.Bin();
        }
    }
//删除事件
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string del = "delete from db_fclass where classname='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
        com = new SqlCommand(del,con);
        try
        {
            con.Open();
            if (com.ExecuteNonQuery() > 0)
            {
                myLabel.Text = "删除成功!";
                Bin();
            }
            else
            {
                myLabel.Text = "删除失败!";
            }
        }
        catch (SqlException ex)
        {
            myLabel.Text = "数据库失败!" + ex.Message;
        }
        finally
        {
            com.Clone();
            con.Close();
        }
    }
//更新事件
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string up_str = Convert.ToString(GridView1.DataKeys[e.RowIndex].Value.ToString().Trim());
        string classm = Convert.ToString(GridView1.Rows[e.RowIndex].Cells[0].Text.ToString().Trim());
        if (up_str.Equals(classm))
        {
            myLabel.Text = "没有更改名字";
        }else{
            string up_class = "update db_fclass set classname='"
                              + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[0].Controls[0])).Text.ToString().Trim() + "' where classname='"+GridView1.DataKeys[e.RowIndex].Value.ToString()+"'";
            com = new SqlCommand(up_class, con);
            try
            {
                con.Open();
                if (com.ExecuteNonQuery() > 0)
                {
                    GridView1.EditIndex = -1;
                    myLabel.Text = "更新成功!";
                    Bin();
                }
                else
                {
                    myLabel.Text = "更新失败!";
                }
            }
            catch (SqlException ex)
            {
                myLabel.Text = "数据库错误" + ex.Message;
            }
            finally
            {
                com.Clone();
                con.Close();
            }
        }
    }
//和更新事件一起用的事件,这是必须的
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
        this.Bin();
    }
//和更新事件一起用的事件,这是必须的
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        this.Bin();
    }
    }
//用选择按钮激发事件做更新数据
 protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        con.Open();
        SqlTransaction stran = con.BeginTransaction();//用到事物
        string up_str = "update db_Theme set CurrenShow=0";
        com = new SqlCommand(up_str, con,stran);
        try
        {
            if (com.ExecuteNonQuery() > 0)
            {
                com.CommandText = "update db_Theme set CurrenShow=1 where id="
                      + GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
                if (com.ExecuteNonQuery() > 0)
                {
                    Page.RegisterStartupScript("key", "<script>alert('指定成功!');</script>");
                }
                else
                {
                    Page.RegisterStartupScript("key", "<script>alert('指定失败!');</script>");
                }
            }
            stran.Commit();//提交事务
        }
        catch (SqlException ex)
        {
            stran.Rollback();//回滚事务
            Response.Write("数据库错误!" + ex.Message);
        }
        finally
        {
            com.Clone();
            con.Close();
        }
    }
 /// <summary>
    /// 用GRIDVIEW中的选择按钮进行参看用户上传的文件然后更新
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        string f_name=GridView1.Rows[e.NewSelectedIndex].Cells[2].Text.ToString();//得到要查看的文件名
        Response.Redirect("../second/file_sc/"+f_name,false);//跳转查看此文件
        //GridView1.DataKeys[e.NewSelectedIndex].Value.ToString()是得到ID值进行更新
        //查看后更新
        string up_sql = "update ja_file_sc set f_zt=1 where id="+GridView1.DataKeys[e.NewSelectedIndex].Value.ToString();
        if (objsql.RunSql(up_sql))
        {
            sel_c();//更新后刷新GRIDVIEW
        }
    }
//CheckBox多选框在Gridview中的使用,这里是被选中的删除
protected void LinkButton1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            //下面是用FindControl("CheckBox1");得到制定控件的ID值,
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox1");
            if (cbox.Checked == true)
            {
        string id=GridView1.DataKeys[i].Value.ToString();
        con = new SqlConnection(ConfigurationManager.ConnectionStrings["dns"].ConnectionString);
        string delete_str = "delete grbg_dzyj_postmail from where id=@id";
        com = new SqlCommand(delete_str, con);
        com.Parameters.Add("@id", SqlDbType.Int);
        com.Parameters["@id"].Value = id;
         con.open();
          com.com.ExecuteNonQuery();
         con.clone();
            }
        }
        Bin();//重新绑定Grideview保持数据更新
    }
//在grideview头模板中添加一个CheckBox当选中的时候下面表中的CheckBox全选
 protected void CheckBox1_CheckedChanged(object sender, EventArgs e)
    {

        CheckBox cbox_1 = (CheckBox)GridView1.HeaderRow.FindControl("CheckBox1");//("CheckBox1");//得到grideview头中的CheckBox控件
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox cbox = (CheckBox)GridView1.Rows[i].FindControl("CheckBox3");
            if (cbox_1.Checked == true)
            {
                cbox.Checked = true;
            }
            else
            {
                cbox.Checked = false;
            }
        }
    }
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值