在網路搜尋到的,來源不明 (我猜應該是直接從外文翻譯過來的)

void BindData()
{
    string sql = "select top 10 ID,jobno,jobfixno,jobtype from jobse";
    SqlDataAdapter myda = new SqlDataAdapter(sql, conn);
    DataSet ds = new DataSet();
    myda.Fill(ds, "RusTable");
    GridView1.DataSource = ds.Tables["RusTable"].DefaultView;
    GridView1.DataBind();
    conn.Close();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    GridView1.EditIndex = e.NewEditIndex;

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
    GridView1.EditIndex = -1;
    BindData();
}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{

    TextBox tb = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox001");
    string dkvalue = GridView1.DataKeys[e.RowIndex].Value.ToString();
    string sql1 = "update jobse set jobno='" + tb.Text.ToString() + "' where id='" + dkvalue + "'";
    conn.Open();
    SqlCommand cmd = new SqlCommand(sql1, conn);
    cmd.ExecuteNonQuery();
    conn.Close();
    GridView1.EditIndex = -1;
    BindData();
}

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
    GridViewRow row = GridView1.Rows[e.RowIndex];
    BindData();
}

protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;


四、自定義列中的數據更新

假設數據庫中有一個"權限"字段,值為0代表未審核用戶,為1代表一般用戶,為9則代表管理員用戶。 根據前面所說的自定義列的辦法,通過對DropListDown的綁定,在網頁中顯示權限為"管理員",而不是顯示數字9。 問題產生了,如果我們調整用戶權限的話,比如把一般用戶更改為管理員,在編輯模版中的用戶權限的下拉列表,如何把它的值返回給數據源來完成更新操作。

我們在EditItemTemplate中設置的DropListDown控件,必須選中了Two Way DataBinding,也就是數據雙向幫定,這樣才能返回數據。 前面我們談到,在GridView中,事件不是單個的,是兩個,一個是發生前,一個是發生後,因為我們需要在數據更新前把下拉列表的權限值傳送出去,所以我們需要對GridView1_RowUpdating進行編碼,編碼如下:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    //當前編輯的是哪行?
    int index = GridView1.EditIndex;

    //取得當前編輯行的GridViewRow對象
    GridViewRow gvr = GridView1.Rows[index];

    //在當前行中,尋找DropListDown控件
    DropDownList dp = (DropDownList)gvr.FindControl("editdrop");

    //將DropListDown的值賦給NewValues 集合中的權限字段。
    e.NewValues ["rights"] = dp.SelectedValue;
}


2、RowDataBound事件

在創建gridView控件時,必須先為GridView的每一行創建一個GridViewRow對象,創建每一行時,將引發一個RowCreated事件;當行創建完畢,每一行GridViewRow就要綁定數據源中的數據,當綁定完成後,將引發RowDataBound事件。 如果說我們可以利用RowCreated事件來控制每一行綁定的控件,那麼我們同樣可以利用RowDataBound事件來控制每一行綁定的數據,也就是讓數據如何呈現給大家。

還舉同樣的例子,在數據表中,存在性別列,上面我們用DropListDown控件的DataBounding來表示出了中文的性別,但是畢竟不太美觀,我們現在可以利用Label控件和RowDataBound事件來實現完美的中文性別顯示。 RowDataBound,

首先,還是把性別列,設置為模板列,並添加一個Label控件,將Label控件綁定到數據源的性別段,然後我們在GridView控件屬性的事件列表中雙擊RowDataBound,生成如下事件:

Example:

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)

{

//判斷當前行是否是數據行
    if (e.Row.RowType == DataControlRowType.DataRow)
    { 

//用FindControl方法找到模板中的Label控件
        Label lb1= (Label)e.Row.FindControl("Label1");

//因為RowDataBound是發生在數據綁定之後, 所以我們可以
//判斷Label綁定的數據,如果是True,就更改其text屬性為男

        if (lb1.Text== "True")
            lb1.Text = "男";
        else
            lb1.Text = "female";

    }

}


3、RowType

RowType可以確定GridView中行的類型,RowType是玫舉變量DataControlRowType中的一個值。 RowType可以取值包括DataRow、Footer、Header、EmptyDataRow、Pager、Separator。 很多時候,我們需要判斷當前是否是數據行,通過如下代碼來進行判斷:

if (e.Row.RowType == DataControlRowType.DataRow)


4、RowDeleting和RowDeleted事件

RowDeleting發生在刪除數據之前,RowDeleted發生在刪除數據之後。

使用RowDeleting事件,可以在真正刪除前再次確認是否刪除,可以通過設置GridViewDeleteEventArgs.Cancel=True來取消刪除;也可以用於判斷當前數據庫記錄數,如果只剩一條記錄且數據庫不能為空則提示並取消刪除操作。

使用RowDeleted事件,可以在刪除後,通過GridViewDeletedEventArgs的Exception屬性判斷刪除過程中是否產生異常,如無異常,則可以顯示類似於” 1 Records deleted” 之類的提示信息。

Example:

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

{

//取得當前行號,並取得當前行的GridViewRow對象

    int index=e.RowIndex ;

    GridViewRow gvr=GridView1.Rows[index];

//取得當前行第二個單元格中的文字

    str1 = gvr.Cells[1].Text;

//進行提示

    Message.Text ="您將刪除一個用戶,其姓名為"+str1 ;

 }

protected void GridView1_RowDeleted(object sender, GridViewDeletedEventArgs e)

{

//如果沒有產生異常,則提示成功刪除,否則提示刪除失敗

    if (e.Exception == null)

        Message.Text += "<br>您成功刪除了"+str1 ;

    else

        Message.Text += "刪除失敗,請聯繫管理員";

}

5、RowEditing事件

在GridView中的行進入編輯模式之前,引發RowEditing事件,如果您需要在編輯記錄前進行某些預處理,可以在這裡操作。 如果想取消對當前行的編輯,可以把GridViewEditEventArgs 對象的Cancel 屬性設置為true即可。

Example:

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)

{

//用NewEidIndex取得當前編輯的行號,然後獲取gridviewrow對象

    GridViewRow gvr = GridView1.Rows[e.NewEditIndex];


//判斷,如果當前編輯行姓名欄為admin用戶,則取消對當前行的編輯

    if (gvr.Cells[1].Text =="admin")

        e.Cancel = true;

}


6、RowUpdating和RowUpdated事件

RowUpdating事件發生在更新數據源之前,RowUpdated發生在更新數據源之後。

我們可以在記錄更新前利用RowUpdating做一些預處理工作,比如修改密碼時,因為密碼在數據庫中不是明文存儲,進行了hash,所以在更新密碼前,應該生成其hash值,再進行更新操作。 RowUpdated則可以檢驗更新是否成功。

Example:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

    GridViewRow gvr = GridView1.Rows[GridView1 .EditIndex ];

//尋找輸入密碼的控件

    TextBox tb1 = (TextBox)gvr.FindControl("tb_password");

//將此控件中的文本hash後,把password存入NewValues 這個字典中

    e.NewValues ["password"] =tb1.Text .GetHashCode().ToString () ;


}

protected void GridView1_RowUpdated(object sender, GridViewUpdatedEventArgs e)

{

//如無異常,則更新成功

    if (e.Exception == null)

        Message.Text += "更新成功!";

}


7、Keys、OldValues 、NewValues 集合

Keys字典中一般存放的是數據源中的主鍵字段的key和value的對應值,如果主鍵由多個字段組成,那麼Keys為每個鍵字段添加其字段名稱和值。 OldValues 中存放的是要更新的行的字段名和原始值,每個字段為其中的一項。 NewValues 中存放的是要更新的行的字段名和修改後的值,每個字段為其中的一項。 注意,主鍵字段只存放於keys集合中。

這三個集合中的每一項都是DictionaryEntry類型的對象,我們可以用DictionaryEntry.Key來確定一個項的字段名稱,用DictionaryEntry.Value來確定某項的值。

在上面的例子中,為了把密碼明文加密後再存入數據庫,我們利用了NewValues 字段,重新設置key為password的項的值。 為了保證安全性,我們在更新數據前對NewValues 中的所有值進行html編碼:

Example1:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{

//遍歷NewValues ,取得其中每一對DictionaryEntry對象

    foreach (DictionaryEntry de in e.NewValues )


//de.key就是字段名,如果此處單獨更新某字段的話,也可以直接填寫字段名,//比如e.NewValues [“password”]


    e.NewValues [de.Key] = Server.HtmlEncode(de.Value.ToString());

}


Example2:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)

{
//分別利用Keys、OldValues 、NewValues 取得主鍵名、原始數據和更新後數據
    Message .Text = e.Keys["username"] + "的email地址從" + e.OldValues ["email"] + "變更為" + e.NewValues ["email"];


arrow
arrow
    全站熱搜

    伊 發表在 痞客邦 留言(0) 人氣()