您的位置:首页 > 编程语言 > ASP

ASP.NET实现增删改查等功能(Access版)系统之二-gridview绑定数据及行绑定

2010-05-07 13:18 851 查看
GridView绑定数据.
//绑定
    public void bind(string str)
    {
        string oledbstr = "";
        if (str != null && str != "")
        {
            oledbstr = str;
        }
        else
        {
           oledbstr = "select ID, 姓名,性别,出生日期,工作年限,证件类型,证件号,居住地,Email,手机号码,家庭电话,自我评价 from MResume order by id desc";
        }
        OleDbConnection oledbcon = new OleDbConnection(GetConnection());
        OleDbDataAdapter myda = new OleDbDataAdapter(oledbstr, oledbcon);
        DataSet myds = new DataSet();
        oledbcon.Open();
        myda.Fill(myds, "MResume");
        GridView1.DataSource = myds.Tables[0];     //重点要记住的地方,看这个地方是DataSet or DataTable
        GridView1.DataKeyNames = new string[] {"id"};//主键
        GridView1.DataBind();
        oledbcon.Close();
    }
GridView行绑定
/// <summary>
    /// 改变背景色,第二个方法
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void GridView1_RowDataBound2(object sender, GridViewRowEventArgs e)
    {
        int i;
        //执行循环,保证每条数据都可以更新
        for (i = 0; i < GridView1.Rows.Count; i++)
        {
            //首先判断是否是数据行
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //当鼠标停留时更改背景色
                //  e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");
                //当鼠标移开时还原背景色
                //  e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
            }
        }
        //如果是绑定数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            //鼠标经过时,行背景色变 00A9FF  E6F5FA
            //  e.Row.Attributes.Add("onmouseover", "this.style.backgroundColor='#00A9FF'");
            //鼠标移出时,行背景色变
            //  e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor='#FFFFFF'");
        }
        //如果是绑定数据行
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            if (e.Row.RowState == DataControlRowState.Normal || e.Row.RowState == DataControlRowState.Alternate)
            {
                //删除提示操作
                 ((LinkButton)e.Row.Cells[13].Controls[0]).Attributes.Add("onclick", "javascript:return confirm('你确认要删除:/"" + e.Row.Cells[1].Text + "/"吗?')");
                e.Row.Cells[11].Text = SubStr(e.Row.Cells[11].Text, 20);  //用...代替长文本
            }
        }
        //GridView实现自动编号:
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
        }
        //设置编辑状态文本框长度
        //if ((e.Row.RowState == (DataControlRowState.Edit | DataControlRowState.Alternate)) || (e.Row.RowState == DataControlRowState.Edit))
        //{
        //    TextBox curText;
        //    for (int j = 4; j < 12; j++)
        //    {
        //        curText = (TextBox)e.Row.Cells[j].Controls[0];
        //        curText.Width = Unit.Pixel(60);   //设置长度
        //        curText.Font.Size = FontUnit.Point(11);//设置字号
        //    }
        //}
    }
    public string SubStr(string sString, int nLeng)
    {
        if (sString.Length <= nLeng)
        {
            return sString;
        }
        string sNewStr = sString.Substring(0, nLeng);
        sNewStr = sNewStr + "...";
        return sNewStr;
    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息