GridView的RowUpdating中DataKeys與非DataKeys

今天在使用GridView的RowUpdating來修改數據時,開始總是提示Index超出範圍,反覆查看了DataKeyNames的設置,發現它只設置了一個字段,因爲之前一直不是太明白DataKeyNames的用途,所以纔出了這個錯誤。原來我把DataKeyNames只設置了“UserID"這個字段,但後面的代碼卻如下:

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();

這樣,因爲它只設置了UserID這個字段,自然第二行的時候它就出錯了,即是說,雖然在GridView裏有四列,UserID,Username,Department,Company,但其中其實DataKeys集合中卻只有一列UserID。

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

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

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;

因爲我並沒有使用任何模板,用的只是最簡單,也是默認的BoundField,所以(我自己想當然,是不是還要以後驗證)我想應該把它顯示轉換成了TextBox.這樣終於算是成功了。

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

前臺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 />");     //通過這兩行才發現問題的。
//  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=服務器爲;UID=用戶名;PWD=密碼;Database=test;";
string sqlstr="UPDATE UserInfo SET Username='"+Username+"',Department='"+Department+"',Company='"+Company+"' WHERE UserID='"+UserID+"'";
try
{
SqlConnection conn=new SqlConnection(connstr);
if(conn.State.ToString()=="Closed") conn.Open();
SqlCommand comm=new SqlCommand(sqlstr,conn);
comm.ExecuteNonQuery();
comm.Dispose();
if(conn.State.ToString()=="Open") conn.Close();
GridView1.EditIndex=-1;
GridViewBind();
}
catch(Exception ex)
{
Response.Write("Database Error, Reason: "+ex.Message);
}
}

趕緊記下供以後參考。

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