关闭

双向排序

586人阅读 评论(0) 收藏 举报

  private void DataGrid1_SortCommand(object source, System.Web.UI.WebControls.DataGridSortCommandEventArgs e)
  {
   if(ViewState["Order"]== null)
   {
    ViewState["Order"] = "ASC";
   }
   else
   {
    if(ViewState["Order"].ToString() == "ASC")
     ViewState["Order"] = "DESC";
    else
    {
     ViewState["Order"] = "ASC";
    }
   }

   //数据绑定
   SqlConnection con = DB.createCon();
   SqlCommand com = new SqlCommand("select * from sales", con);
   SqlDataAdapter da= new SqlDataAdapter(com);
   DataSet ds = new DataSet();
   da.Fill(ds,"sales");
   //
   DataView dv= ds.Tables["sales"].DefaultView;
   dv.Sort = e.SortExpression + " " + ViewState["Order"].ToString();
   //ds.Tables["sales"].DefaultView.Sort = e.SortExpression + " " + ViewState["Order"].ToString();
   DataGrid1.DataSource = dv;
   DataGrid1.DataBind();
  
  }

  private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
  {

            if(e.Item.ItemType == ListItemType.AlternatingItem || e.Item.ItemType == ListItemType.Item)
    //if(e.Item.ItemIndex != -1)或者
   {
    e.Item.Attributes.Add("onmouseover","this.style.backgroundColor='blue';");
    e.Item.Attributes.Add("onmouseout","this.style.backgroundColor='#ffffff';");
    ((LinkButton)e.Item.Cells[0].Controls[0]).Attributes.Add("onclick","return window.confirm('请确认删除?')");
   }
   
  }

  private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  {
    string stor_id = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

   //delete operator
   SqlConnection con = DB.createCon();
   con.Open();

   string comText = "delete sales where stor_id =" + stor_id;
   SqlCommand com= new SqlCommand(); 
   com.CommandText = comText;
   com.Connection = con;
   //SqlDataReader dr;
   com.ExecuteNonQuery();;
   //dr.Close();
   con.Close();

   dataBindToTest();
  }

 

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:1408755次
    • 积分:20227
    • 等级:
    • 排名:第413名
    • 原创:448篇
    • 转载:1067篇
    • 译文:4篇
    • 评论:143条
    文章存档
    最新评论