GridView的RowUpdating中DataKeys與非DataKeys

今天在使用GridView的RowUpdating來修改數(shù)據(jù)時(shí),開始總是提示Index超出范圍,反復(fù)查看了DataKeyNames的設(shè)置,發(fā)現(xiàn)它只設(shè)置了一個(gè)字段,因?yàn)橹耙恢辈皇翘靼譊ataKeyNames的用途,所以才出了這個(gè)錯(cuò)誤。原來我把DataKeyNames只設(shè)置了“UserID"這個(gè)字段,但后面的代碼卻如下:

創(chuàng)新互聯(lián)于2013年創(chuàng)立,是專業(yè)互聯(lián)網(wǎng)技術(shù)服務(wù)公司,擁有項(xiàng)目做網(wǎng)站、網(wǎng)站建設(shè)網(wǎng)站策劃,項(xiàng)目實(shí)施與項(xiàng)目整合能力。我們以讓每一個(gè)夢想脫穎而出為使命,1280元泉港做網(wǎng)站,已為上家服務(wù),為泉港各地企業(yè)和個(gè)人服務(wù),聯(lián)系電話:18982081108

string UserID=GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string Username=GridView1.DataKeys[e.RowIndex].Values[1].ToString();
string Department=GridView1.DataKeys[e.RowIndex].Values[2].ToString();
string Company=GridView1.DataKeys[e.RowIndex].Values[3].ToString();

這樣,因?yàn)樗辉O(shè)置了UserID這個(gè)字段,自然第二行的時(shí)候它就出錯(cuò)了,即是說,雖然在GridView里有四列,UserID,Username,Department,Company,但其中其實(shí)DataKeys集合中卻只有一列UserID。

于是就改變思路,要怎么樣去得到其它不是主鍵列(即不在DataKeyNames里)的值從而把它放入更新語句呢?對于我這樣的初學(xué)者來說,其實(shí)對于GridView的一些屬性,方法還不是很熟的情況下,這是一個(gè)問題。

于是在網(wǎng)上百度,Google了很多前輩們的文章,各種答案層出不窮,其中有很多都是要把對應(yīng)的那個(gè)列做顯示轉(zhuǎn)換如(Label)GridView1.....,而且后面有用DataKeys[e.RowIndex]的,有用Rows[e.RowIndex]的,現(xiàn)在DataKeys明顯已經(jīng)不行,所以我想應(yīng)該是用Rows,但怎么用Rows集合來表示行中其它列的值呢?經(jīng)過一番搜索,加上用Response.Write來嘗試輸出值(使用ClassicASP時(shí)養(yǎng)成的習(xí)慣),最后得出應(yīng)該是如下格式:

string Username=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Department=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Company=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

因?yàn)槲也]有使用任何模板,用的只是最簡單,也是默認(rèn)的BoundField,所以(我自己想當(dāng)然,是不是還要以后驗(yàn)證)我想應(yīng)該把它顯示轉(zhuǎn)換成了TextBox.這樣終于算是成功了。

整個(gè)過程中,開始我認(rèn)為是第一行(stringUserID=GridView1.DataKeys[e.RowIndex].Values[0].ToString();)的問題,弄了很久,后來通過Response.Write來嘗試輸出第一列的值時(shí)才發(fā)現(xiàn),原來第一行是能正確輸出當(dāng)前UserID值的,只是后面的三列都不行。所以發(fā)現(xiàn)原來一些以前的老習(xí)慣有時(shí)也是很有幫助的。

前臺GridView代碼如下:

<asp:GridView ID="GridView1" runat="server" Width="70%" CellPadding="4" ForeColor="#333333"
AutoGenerateColumns="False" AllowPaging="True" PageSize="20"
OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating"
DataKeyNames="UserID"  OnPageIndexChanging="GridView1_PageIndexChanging" OnRowDataBound="GridView1_RowDataBound"
GridLines="None">
<Columns>
<asp:BoundField HeaderText="User ID" DataField="UserID" />
<asp:BoundField HeaderText="User Name" DataField="Username" />
<asp:BoundField HeaderText="Department" DataField="Department" />
<asp:BoundField HeaderText="Division" DataField="Division" />
<asp:CommandField HeaderText="OPT" ShowDeleteButton="True" ShowEditButton="True" />
</Columns>

RowDating代碼如下:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//  Response.Write(GridView1.DataKeys[e.RowIndex].Values[0].ToString()+"<br />");     //通過這兩行才發(fā)現(xiàn)問題的。
//  Response.Write(((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text);
string UserID=GridView1.DataKeys[e.RowIndex].Values[0].ToString();
string Username=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
string Department=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
string Division=((TextBox)this.GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;
string connstr="Server=

趕緊記下供以后參考。

文章標(biāo)題:GridView的RowUpdating中DataKeys與非DataKeys
標(biāo)題URL:http://bm7419.com/article12/pssjdc.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供響應(yīng)式網(wǎng)站、網(wǎng)站設(shè)計(jì)、關(guān)鍵詞優(yōu)化網(wǎng)頁設(shè)計(jì)公司、網(wǎng)站改版、做網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)