0

Say I have only two columns in my GridView (ID & Name) and I need to get the value of ID whenever I click on it. For example, if I click on 123, I should get 123. But here... that's not the case... when I click on any ID it returns the name beside that ID right (eg; in case of 123, I get Tony Stark), but when I get the value of ID on click, it always returns me an empty string.

In other words, I get the name in id right (when X=1), but when X=0, id becomes an empty string... (int X and string id : see C# code below)

sample GridView1

XAML Code in my GridView:

                        <asp:ButtonField
                            DataTextField="ID"
                            HeaderText="ID">
                        </asp:ButtonField>

                        <asp:BoundField
                            DataField="Name"
                            HeaderText="NAME">
                        </asp:BoundField>

C#:

    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string id = GridView1.Rows[Convert.ToInt32(e.CommandArgument)].Cells[X].Text.ToString();
        Response.Write(id);
    }

What's going wrong?!! And how to fix this? Thanks!

AFract
  • 8,868
  • 6
  • 48
  • 70
aayani
  • 333
  • 9
  • 19
  • What is X supposed to be? – StingyJack Sep 26 '14 at 19:40
  • X is the index for cells (see C# code). It's 1 for Name column, and should be 0 for ID column, but that's not the case. Having 0 always gives me an empty string. – aayani Sep 26 '14 at 19:43
  • 2
    Generally speaking with .net grids, the Cell will have another control inside it. If you check Cells[0].Controls or .Children while debugging, are there any instances of this? Also, are you sure you have the correct row index? – StingyJack Sep 26 '14 at 19:46
  • Yes the row is correct, so is the cell. I get this output when I try string id = EmployeeList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls.ToString(); "System.Web.UI.ControlCollection" – aayani Sep 26 '14 at 19:51
  • What is the type of EmployeeList.Rows[Convert.ToInt32(e.CommandArgument)].Cells[0].Controls[0] – StingyJack Sep 26 '14 at 20:35

3 Answers3

2

The more complex but better solution is:

In GridView, use DataKeyNames tag and RowCommand event and put the bind value in DataKeyName, example:

DataKeyNames="ID" OnRowCommand="GridView1_RowCommand" 

Use a Template Field in your gridview (you do not need use only imagebutton like as example below):

<asp:TemplateField HeaderText="Action">
                                <HeaderStyle HorizontalAlign="Center" />
                                <ItemStyle HorizontalAlign="Left" Wrap="false" />
                                <ItemTemplate>
                                    <div class="GridViewOpc">
                                        <asp:ImageButton ID="buttonDelete" runat="server" Width="17" Height="17" AlternateText="Update"
                                            CausesValidation="False" Visible="false" CommandName="Delete" Enabled="true"
                                            ImageUrl="~/yourDeletepicture.gif"  />

                                        <asp:ImageButton ID="buttonUpdate" runat="server" AlternateText="Update" Width="17"
                                            Height="17" CausesValidation="False" Visible="false" CommandName="Update" ImageUrl="~/yourUpdatePicture.gif" />
                                    </div>
                                </ItemTemplate>
                            </asp:TemplateField>

And get it on CodeBehind:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{

            Control ctl = e.CommandSource as Control;
            GridViewRow CurrentRow = ctl.NamingContainer as GridViewRow;

                if (e.CommandName == "Update")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);
                }

               if (e.CommandName == "Delete")
                {

                    int ID = Convert.ToInt32(GridView1.DataKeys[CurrentRow.RowIndex][0]);

                }
    }
0

Without using templateField you can also get the value from ButtonField. In your ButtonField you need to give it a ButtonType.

XML:

<asp:ButtonField ButtonType="Link" CommandName="PanelName" DataTextField="PANEL_NAME" HeaderText="Panel Name" runat="server" SortExpression="PANEL_NAME" text="PanleName"/>

Code:

if (e.CommandName == "PanelName")
{
    string PanleName;
    int index = Convert.ToInt32(e.CommandArgument);
    GridViewRow selectedRow = dgSearchResult.Rows[index];
    PanleName = ((LinkButton)selectedRow.Cells[1].Controls[0]).Text;
}

Note: Whatever your cell index is, Cells[1] means index is 1. But Controls always start from 0 if only one control is in your GridView.

Swift
  • 3,250
  • 1
  • 19
  • 35
-1

In your RowCommand handler, I THINK this is what you want:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        int id = (int)(DataBinder.Eval(e.Row.DataItem, "ID"));
        Response.Write(id);
    }
}

I'm converting from vb, so I may have missed some syntax, but you get the idea...

Steve
  • 531
  • 5
  • 16