日期:2014-05-18  浏览次数:20918 次

GridView中,取所选行的某列数据的值的问题!50分!
aspx代码 
<asp:GridView ID="gvEmpInfo" runat="server" > 
  <HeaderStyle CssClass="headcenter" BackColor="#DDE8FF" / > 
  <Columns > 
  <asp:ButtonField DataTextField="TitleName" CommandName="XianShi" HeaderText="标题" / > 
  <asp:BoundField DataField="BranchName" HeaderText="部门" / > 
  <asp:BoundField DataField="DateTime" HeaderText="日期" / > 
  <asp:ButtonField CommandName="Delete" Text="删除 " HeaderText="删除" / > 
  <asp:BoundField DataField="id" Visible="False" / > 
  <asp:BoundField DataField="BranchID" Visible="False" / > 
  <asp:BoundField DataField ="TitleName" Visible="False" / > 
  </Columns > 
</asp:GridView > 
数据已经正常绑定了!就是删除问题。我想在点击“删除”按钮时,得到id和BranchID的值! 
cs代码: 
protected void gvEmpInfo_RowDeleting(object sender, GridViewDeleteEventArgs e) 

  string a = gvEmpInfo.Rows[e.RowIndex].Cells[4].Text; 
  string b = gvEmpInfo.Rows[e.RowIndex].Cells[5].Text; 
  msgError(a); 

cs代码这么写后,得到的值是空的!
msgError是我自己的一个显示窗口~这个不用管~ 

哪位高手能帮忙解决一下!谢谢!

------解决方案--------------------
设置GridView1的DataKeys
GridView1.DataKeyNames = new string[] { "Id" };
取得ID号
Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString())

------解决方案--------------------
干吗不用GRIDVIEW自带的删除功能呢(VB代码)

Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
Dim item As String
item = GridView1.Rows(e.RowIndex).Cells(1).Text
TextBox1.Text = item
item = "资产编号='" & item & "'"
DEP1_ASSET.DeleteCommand = Replace(sqlclass.ELEC_DEP1_ASSET_DEL, "$ITEM$", item)
End Sub


DEP1_ASSET 是SQLDATESOURCE类型
------解决方案--------------------
HTML code

<asp:GridView   ID="gvEmpInfo"   runat="server" DataKeyNames="id,BranchID"  >

------解决方案--------------------
一楼的用的是ROW_ITEMCOMMAND,二楼用的是ROW_DELETING都可以,楼主自己考虑下,UP
------解决方案--------------------
楼上很请弓虽大
------解决方案--------------------
CommandArgument与CommandName是作什么用的?