GridView操作

来源:互联网 发布:v5团队淘宝兼职平台 编辑:程序博客网 时间:2024/05/17 05:13

1、鼠标悬停变色

 protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");

            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c;");
            //为特定的数改变行样式这也是在这个事件里面,因为这个事件是在数据被绑定的时候执行的
        for (int i = 0; i < GridView1.Rows.Count; i++)
        
//为了对全部数据行都有用,我们使用循环
            string lbl = Convert.ToString(DataBinder.Eval(e.Row.DataItem,"state"));//我们得取出行中state字段绑定的值,用他作为判断条件
            if (lbl == "BB")
            
//如果他的值等于BB,那么
                e.Row.BackColor = Color.LimeGreen;//           

               }
        }

        }
    }

2、隐藏、显示列

protected void Button1_Click(object sender, EventArgs e)
    
{//隐藏特定的列
        int r = int.Parse(TextBox1.Text);
        GridView1.Columns[r].Visible 
= false;
    }

    
protected void Button2_Click(object sender, EventArgs e)
    
{//显示全部被隐藏的列
        for (int j = 0; j < GridView1.Columns.Count; j++)
        
{//循环所有的列,然后检察属性是否是否,如果是就改变成是就可以了
            if (GridView1.Columns[j].Visible == false)//刚才错在判断这里了,呵呵
            {
                GridView1.Columns[j].Visible 
= true;
            }

        }

    3、分页

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

 public void DataBind1()
    {
        SqlConnection con = DB.createCon();
        con.Open();

        SqlDataAdapter sda = new SqlDataAdapter();
        sda.SelectCommand = new SqlCommand("select * from province", con);
        DataSet ds = new DataSet();
        sda.Fill(ds, "province");

        this.GridView1.DataSource = ds.Tables["province"];
        this.GridView1.DataBind();
    }

设置属性AllowPaging为true