在GridView中處理數據不使用Data Source Controls

已經有很多文章或指南介紹了在GridView中使用Data Source Controls處理數據,並且也很簡單,幾乎不要寫什麼代碼,就可以實現很多功能:新增、編輯、刪除、分頁、排序等等,如果不使用Data Source Controls,也能實現這些功能嗎?答案是肯定的。本文將演示不使用Data Source ControlsSQL中的“Northwind”數據庫中“Employees”表中的數據在GridView中顯示出來。
 
以下是詳細步驟:
 
1、從工具箱中拖拽一個GridView控件到“設計”頁面中,選擇“GridView任務”下面的“編輯列”,向列中增加三個BoundFields和一個CommandField,然後結合表1修改各自屬性,修改後形如表2
1
BoundField HeaderText DataField ReadOnly
Employee ID Employee ID EmployeeID true
First Name First Name FirstName false
Last Name Last Name LastName false


2




2、更改頁面的“Page_Load()”方法如下:
 protected void Page_Load(object sender, EventArgs e)
    
{
        
if (!IsPostBack)
        
{
            BindGrid();
        }

    }




3、在“Page_Load()”方法的後面添加BindGrid()方法:
private void BindGrid()
    
{
        DataSet ds 
= new DataSet();
        SqlDataAdapter da 
= new SqlDataAdapter("select * from employees"@"data source= .sqlexpress;initial catalog=northwind; integrated security=true");
        da.Fill(ds,
"employees");
        DataView dv 
= ds.Tables[0].DefaultView;

        
if (ViewState["sortexpression"!= null)
        
{
            dv.Sort 
= ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
        }


        GridView1.DataSource
=dv;
        GridView1.DataBind();
    }




4、實現分頁功能。
F4調出GridView的屬性頁,設置“AllowPaging”爲True,設置“PageSize”屬性爲3
設置“PageIndexChanging”事件如下代碼:
protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    
{
        GridView1.PageIndex 
= e.NewPageIndex;
        BindGrid();
    }




5、實現排序功能。
選定GridView選擇“GridView任務”下面的“編輯列”,設置三個“BoundFields”的“SortExpression”屬性值和“DataField”屬性相同。
設置GridView的“Sorting”事件代碼如下:
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
    
{
        ViewState[
"sortexpression"= e.SortExpression;

        
if (ViewState["sortdirection"== null)
        
{
            ViewState[
"sortdirection"= "asc";
        }

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

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

        }

        BindGrid();
    }






6、實現編輯功能。
設置GridView的“RowEditing”事件代碼如下:
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    
{
        GridView1.EditIndex 
= e.NewEditIndex;
        BindGrid();
    }


設置GridView的“RowCancelingEdit”事件代碼如下:
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    
{
        GridView1.EditIndex 
= -1;
        BindGrid();
    }


設置GridView的“ RowUpdating”事件代碼如下:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
{
        
int empid;
        
string fname, lname;
        empid 
= int.Parse(GridView1.Rows[e.RowIndex].
        Cells[
0].Text);
        fname 
= ((TextBox)GridView1.Rows[e.RowIndex].
        Cells[
1].Controls[0]).Text;
        lname 
= ((TextBox)GridView1.Rows[e.RowIndex].
        Cells[
2].Controls[0]).Text;

        SqlConnection cnn 
= new SqlConnection(@"data source= .sqlexpress; initial catalog=northwind; integrated security=true");
        cnn.Open();
        SqlCommand cmd 
= new SqlCommand("update employees set firstname=@fname,lastname=@lname where employeeid=@empid",cnn);
        cmd.Parameters.Add(
new SqlParameter("@fname",fname));
        cmd.Parameters.Add(
new SqlParameter("@lname", lname));
        cmd.Parameters.Add(
new SqlParameter("@empid", empid));
        cmd.ExecuteNonQuery();
        cnn.Close();

        GridView1.EditIndex 
= -1;
        BindGrid();
    }




7、保存所有文件。按F5運行一把,效果圖如下:

 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章