关于DataGridView的详细用法

1:DataGridView在编辑时绑定下拉框

前如代码: <asp:DropDownList ID="DropDownList1" runat="server" DataSource=<%# GetDropBind() %> DataTextField="DepName" DataValueField="Depid">
                        </asp:DropDownList>

后台代码:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
       string oldDep= (this.GridView1.Rows[e.NewEditIndex].FindControl("Label1") as Label).Text.Trim();

        this.GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();

        DropDownList dp = this.GridView1.Rows[e.NewEditIndex].FindControl("DropDownList1") as DropDownList;
        foreach (ListItem li in dp.Items)
        {
            if (li.Text == oldDep)
                li.Selected=true;
        }
    }

=======================================

2更新:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string cardid = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        string name = (this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text.Trim();
      }

======================================

3编辑:

this.GridView1.EditIndex = e.NewEditIndex;
        GridViewBind();

=================================

4翻页:

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

===============================

5取消:

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        this.GridView1.EditIndex = -1;
        GridViewBind();
    }

=================================

6删除:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        string s = this.GridView1.DataKeys[e.RowIndex].Value.ToString();
        bool bl = DABase.UpdateData("delete from Emp where cardid='"+s+"'");

========================================================

7特殊效果

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)//数据行
        {
            //自动编号
            int num = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = num.ToString();

            //光棒效果
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#00A9FF'");//鼠标移到数据行时
            e.Row.Attributes.Add("onmouseout","this.style.backgroundColor=c");//当鼠标移开数据行

            //实现用“...”代替超长字符串
            string val=e.Row.Cells[5].Text.Trim();
            if (val.Length > 12)
                val = val.Substring(0, 6)+"...";
            e.Row.Cells[5].Text = val;

            //实现突出显示某一单元格
            string oldmoney = e.Row.Cells[6].Text.Trim();
            float money = float.Parse(oldmoney.Split('¥')[1].ToString());
            if (money > 3000)
                e.Row.Cells[6].ForeColor = System.Drawing.Color.Red;

            //求总工资
            tolmoney = tolmoney + money;  
        }
        if (e.Row.RowType == DataControlRowType.Footer)
        {
            e.Row.Cells[6].Text = "总工资为:¥" + tolmoney.ToString();
        }
    }

===========================

8后台运行前台脚本:

this.Button2.Attributes.Add("onclick", " OpenWindow()");

============================

9绑定数据

public void GridViewBind()
    {
        this.GridView1.DataSource = DABase.SelectData("select * from View_Dep_Emp");
        this.GridView1.DataBind();
    }

============================

10批量删除

protected void Button1_Click(object sender, EventArgs e)
    {
        int count=this.GridView1.Rows.Count;
        for (int i = 0; i < count; i++)
        {
            CheckBox ch = this.GridView1.Rows[i].FindControl("CheckBox1") as CheckBox;
           if (ch.Checked)
           {
               string s = this.GridView1.DataKeys[i].Value.ToString();
               Response.Write(s);
           }
        }
    }

============================

11显示页脚:
改属性:ShowFooter:为True

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值