2009-08-11 89 views
0

我有一個gridview,我已經將datasource設置爲datatable。無論我對gridview進行什麼更新,它都應該僅反映在數據表中,而不反映到數據庫。當我點擊更新時,我必須得到我想要更新的特定行的行號。如何獲取gridview的rowupdating事件中的行號以更新數據表?假設我有一個數據鍵,如果我得到這個唯一的ID,我如何更新數據源是數據源?任何人都可以幫忙嗎?獲取gridview中的行號

+1

你可以把你的網格視圖的代碼? – Ali 2013-09-10 06:29:18

回答

0

如果應用了排序,那麼gridview和datatable中的行號將不匹配。

所以,如果你嘗試更新gridview中的行號,它可能會以不正確的值結束。

更新應該基於gridview數據源中的唯一ID

+0

如果soting不存在,我該如何獲得rownumber – user42348 2009-08-11 06:54:32

1

這是讓你更新的行的行ID的方式:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
    string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString(); 
} 

也許你只想要e.RowIndex。但上面的代碼返回該行的指定唯一數據鍵。

0
<asp:TemplateField HeaderText="Slno"> 
    <ItemTemplate> 
    <div style="margin-top: 0px; color: black;"> 
    <asp:Label ID="txtid" runat="server" Font-Size="8.5pt" ForeColor="#000f9f" Text='<%# Container.DataItemIndex+1 %>'></asp:Label> 
    </div> 
    </ItemTemplate> 
    <ItemStyle HorizontalAlign="Left" Width="30px" /> 
    </asp:TemplateField> 

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 
Label id = (Label) GridView1.Rows[e.RowIndex].findcontrol("slno"); 
}