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

本文介绍了使用ASP.NET中GridView控件进行数据绑定的方法,并详细展示了如何通过编程方式设置GridView的样式,包括背景颜色和自动编号等。同时,还提供了自定义函数来处理文本显示长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;

    }

 

转载于:https://www.cnblogs.com/maxblog/archive/2010/05/08/1730553.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值