zoukankan      html  css  js  c++  java
  • Datagrid 控件的使用

    分页


    protected void DataGrid1_PageIndexChanged(object source, DataGridPageChangedEventArgs e)
        {
            this.DataGrid1.CurrentPageIndex = e.NewPageIndex;
            this.datagridview();
        }

    隐藏列

     this.DataGrid1.Columns[2].Visible = false;// 隐藏第三列(只是表面上的隐藏)
      this.datagridview();

    鼠标经过每行时高亮显示:

      protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
            {
                e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");
            }
        }

    双向排序:

    DataGrid1 的 SortCommand 事件激活排序事件
     
    protected void DataGrid1_SortCommand(object source, DataGridSortCommandEventArgs e)
        {
            if (ViewState["order"] == null)
            {
                ViewState["order"] = "ASC";//viewstate[""]同session[""] 差不多,储存在客户端
            }
            else
            {
                if (ViewState["order"].Tostring() == "ASC")
                {
                    ViewState["order"] = "DESC";
                }
                else
                {
                    ViewState["order"] = "ASC";
                }
           
            }
            SqlConnection con = connecttion.ado.sqldb();
            con.Open();
            SqlDataAdapter sda = new SqlDataAdapter();
            sda.SelectCommand = new SqlCommand("select * from person", con);
            DataSet ds = new DataSet();
            sda.Fill(ds, "person");
            ds.Tables["person"].DefaultView.Sort = e.SortExpression + " " + ViewState["order"].ToString();
            //默认视图的Sort属性就是排序属性 e.SortExpression 就是属性生成器中排序表达式的字段
            this.DataGrid1.DataSource = ds.Tables["person"].DefaultView;//
            this.DataGrid1.DataBind();
        }

    绑定查看详细信息列




    在show.aspx中用 string id = Request.QueryString["id"] 接收。

    datagrid中 DataKeyfield="id"  按照索引取的,使用时可以取得当前行的id 

    当要删除或者修改每一行时通过  this.DataGrid1.DataKeys[e.Item.ItemIndex]来获得当前行的i


    datagrid删除的实现

    首先在属性生成器中添加删除按钮,然后在dadagrid的DeleteCommand 事件中编写代码即可

    protected void DataGrid1_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
            string id = this.DataGrid1.DataKeyField[e.Item.ItemIndex];
            SqlConnection con = connecttion.ado.sqldb();
            con.Open();
            SqlCommand cmd=new SqlCommand("delete from person where pid='"+id+"'",con);
            cmd.ExecuteNonQuery();
            this.datagridview();
        }

    如果要在删除时显示确认后 再删除功能
    在DataGrid1_ItemDataBound的事件中 加第三行就可以了
      protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
        {
            if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)//普通行和交错行的事件
            {
                e.Item.Attributes.Add("onmouseover","c=this.style.backgroundColor;this.style.backgroundColor='#6699ff'");//鼠标在的时候
                e.Item.Attributes.Add("onmouseout","this.style.backgroundColor=c");//鼠标离开的时候
                ((LinkButton)(e.Item.Cells[4].Controls[0])).Attributes.Add("onclick", "return confirm('确认删除吗?')");//点任一行的第5列时触发的事件
            }
        }


    Datagrid 的编辑功能:

    更新:
           string id = this.DataGrid1.DataKeys[e.Item.ItemIndex].ToString();取的更新行的id
            string pname = ((TextBox)(e.Item.Cells[1].Controls[0])).Text; 取的本行第二个字段文本框的值
            string psex = ((TextBox)(e.Item.Cells[2].Controls[0])).Text;

    下面加入数据库就可以了,

    在更新时可使用验证控件进行验证,首先把那一列在属性生成器中 转换成模板列,然后在模板中添加验证控件就可以了。

     



     

  • 相关阅读:
    20188477 编程作业
    原型设计
    案例分析
    编程作业
    阅读任务
    准备工作
    原型设计作业
    案例分析作业
    编程作业
    阅读任务
  • 原文地址:https://www.cnblogs.com/gergro/p/361613.html
Copyright © 2011-2022 走看看