我需要从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。
答案 0 :(得分:10)
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。
答案 1 :(得分:4)
@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是模板字段中的按钮)。
答案 2 :(得分:3)
试试这个......
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int rowIndex=gvRow.RowIndex
答案 3 :(得分:2)
int index = Convert.ToInt32(e.CommandArgument);
只有在Gridview中使用按钮时才能使用
<Columns>
<asp:ButtonField ButtonType="Button" Text="Save" CommandName="Select" />
</Columns>
我不确定这是否正确,但它对我有用
答案 4 :(得分:1)
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
}
}
答案 5 :(得分:0)
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;
}