GridView- -事件总结(欧耶)

来源:互联网 发布:photoshop网络课程 编辑:程序博客网 时间:2024/05/21 07:08

--经过潜心单位修炼中有如下杰作慢慢看哇卡卡。

1.点击标题排序 2.分页

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

3编辑

   protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        BindGrid();
    }

4更新功能

//  更新(- -口水吧功能),设置了OnRowEditing="GridView1_RowEditing"。 
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
        
int id;
        
string  xiaozhu;            
        
// 下面得到主建
        id = int.Parse(GridView1.Rows[e.RowIndex].Cells[0].Text);
       
//下面得到更新的内容
        fxiaozhu; = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        SqlConnection cnn 
= new SqlConnection("保密");
        
try
       {
                  cnn.Open();
                  
//这里写更新
                 SqlCommand cmd = new SqlCommand("保密")
                cmd.ExecuteNonQuery();
      }
catch()
      {
      }
      
finally
      {
                   cnn.Close();
      }
        GridView1.EditIndex 
= -1;
        BindGrid();
}

 

5删除


 
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    
{
        
int index = e.RowIndex;
        DataKey key 
= GridView1.DataKeys[index];
         
int id = Convert.ToInt32(key.Value.ToString());
      
//省略..因为偶懒的写了~~呵呵
  
        GridView1.EditIndex 
= -1;
        BindGrid();
    }

 

//  为了实现每列都可以自动点击排序,可以设置allowsorting=true,然后设置OnSorting="GridView1_Sorting",或者直接点事件~~别告诉我你不知道在那里~~那来的砖头

  protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    
{
        ViewState[
"sortexpression"= e.SortExpression;
//设置viewsate来保存每次排序时的顺序
        
if (ViewState["sortdirection"== null)
        
{
            ViewState[
"sortdirection"= "asc";
        }

        
else
        
{
            
if (ViewState["sortdirection"].ToString() == "asc")
            
{
                ViewState[
"sortdirection"= "desc";
            }

            
else
            
{
                ViewState[
"sortdirection"= "asc";
            }

        }

        BindGrid();
    }
原创粉丝点击