C# 在 RowCommand 中获取 GridView 单元格的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10783962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get value of GridView Cell in RowCommand
提问by DarrylGodden
I need to get the value of a cell from the RowCommand event, but the value is not in the PrimaryKeyNames parameter of the GridView.
我需要从 RowCommand 事件中获取单元格的值,但该值不在 GridView 的 PrimaryKeyNames 参数中。
Currently I have:
目前我有:
if (e.CommandName == "DeleteBanner")
{
GridViewRow row = gvCurrentPubBanner.SelectedRow;
string BannerName = row.Cells[1].Text;
This doesn't work (index out of range error), I've also tried:
这不起作用(索引超出范围错误),我也试过:
int index = Convert.ToInt32(e.CommandArgument);
GridViewRow row = gvCurrentBanners.Rows[index];
This doesn't work either as the CommandArgument (the banner ID) is not the row ID.
这也不起作用,因为 CommandArgument(横幅 ID)不是行 ID。
采纳答案by Romil Kumar Jain
Dim row As GridViewRow = CType(CType(e.CommandSource, Control).NamingContainer, GridViewRow)
Then get the key or get the cell and cast to a datacontrolfield.
然后获取密钥或获取单元格并转换为数据控制字段。
Dim id As Guid = GridView1.DataKeys(row.RowIndex).Value
Dim email As String = CType(row.Cells(2), DataControlFieldCell).Text
Remark: this only works with Boundfields.
备注:这只适用于 Boundfields。
回答by Kisan Patel
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
}
}
回答by Bijoy K Jose
Try out this one..
试试这个..
GridViewRow gvRow = (GridViewRow)(((ImageButton)e.CommandSource).NamingContainer);
int rowIndex=gvRow.RowIndex
回答by Grey Wolf
int index = Convert.ToInt32(e.CommandArgument);
int index = Convert.ToInt32(e.CommandArgument);
It only work when you use your button in Gridview is
它仅在您在 Gridview 中使用按钮时才有效
<Columns>
<asp:ButtonField ButtonType="Button" Text="Save" CommandName="Select" />
</Columns>
I not sure this is correct but it work for me
我不确定这是否正确,但它对我有用
回答by Ch?a bi?t
@Romil: your code in C#:
@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 is button in template field).
(LinkButton 是模板字段中的按钮)。
回答by RAJESH DINTAKURTHI
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;
}

