在RowCommand中获取GridView Cell的值
我需要从RowCommand事件中获取单元格的值,但该值不在GridView的PrimaryKeyNames参数中。
目前我有:
if (e.CommandName == "DeleteBanner") { GridViewRow row = gvCurrentPubBanner.SelectedRow; string BannerName = row.Cells[1].Text;
这不起作用(索引超出范围错误),我也尝试过:
int index = Convert.ToInt32(e.CommandArgument); GridViewRow row = gvCurrentBanners.Rows[index];
这不起作用,因为CommandArgument(标题ID)不是行ID。
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
然后获取密钥或获取单元格并转换为数据控制域。
Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text
备注:这仅适用于Boundfields。
试试这个……
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer); int rowIndex=gvRow.RowIndex
@Romil:您在C#中的代码:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); int id = (int)GridView1.DataKeys[row.RowIndex].Value; }
(LinkButton是模板字段中的按钮)。
int index = Convert.ToInt32(e.CommandArgument);
它只适用于在Gridview中使用按钮时
我不确定这是否正确,但它对我有用
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Edit_Mob") { GridViewRow row = (GridViewRow (((ImageButton)e.CommandSource).NamingContainer); int RowIndex = row.RowIndex; // this find the index of row int varName1 = Convert.ToInt32(((Label)row.FindControl("lbl_mobile_id")).Text.ToString()); //this store the value in varName1 } }
if(e.CommandName ==“EditDetails”)
{
mp1.Show(); //LinkButton LnkEdit = (LinkButton)sender; GridViewRow gvrow = (GridViewRow)((Control)e.CommandSource).NamingContainer; Panel1.Visible = true; Uploaddocs.Visible = false; Label lblmobile = (Label)gvrow.FindControl("lblN_MobileNo") ; Label lblDeathDate = (Label)gvrow.FindControl("lblDEATHDT"); Label lblCAUSEOFDEATH = (Label)gvrow.FindControl("lblCAUSEOFDEATH"); Label lblPLACEOFDEATH = (Label)gvrow.FindControl("lblPLACEOFDEATH"); // Label lblRemarks = (Label)gvrow.FindControl("lblpracticalcredits"); Label lblBank = (Label)gvrow.FindControl("lblBank"); Label lblBranch = (Label)gvrow.FindControl("lblBranch"); Label lblIFSC = (Label)gvrow.FindControl("lblIFSC"); txtMobile.Text = Convert.ToInt64(lblmobile.Text).ToString(); //May be BigInt txtdate.Text = lblDate.Text; //ddlReasons.SelectedIndex = Convert.ToInt32(lblCAUSEOFDEATH.Text); //ddlReasons.SelectedValue = lblCAUSEOFDEATH.Text; txtcausedeath.Text = lblCAUSEOFDEATH.Text; txtPlaceofdeath.Text = lblPLACEOFDEATH.Text; BindBank(); ddlbank.SelectedItem.Text = lblBank.Text; //Sdents txtAddrBank.Text = lblBranch.Text; txtIFSC.Text = lblIFSC.Text; }