Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
382 views
Welcome To Ask or Share your Answers For Others

1 Answer

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...