0

I have a datagridview that has a list of names and phone numbers, if you click a name it loads a form. I am trying to return the name that is clicked so that, I can load the form more quickly. I got the 3 line of this code from someone else but get errors on both e.Rowindex and e.ColumnIndex. I would think from the error this code is wrong, but the answer i got it from had a bunch of upvotes and was market correct.

Error 43 'System.Web.UI.WebControls.GridViewCommandEventArgs' does not contain a definition for 'RowIndex' and no extension method 'RowIndex' accepting a first argument of type 'System.Web.UI.WebControls.GridViewCommandEventArgs' could be found (are you missing a using directive or an assembly reference?)

      protected void userListGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Display") //switch to update user mode
            {
//what I am trying to do
                string Username = userListGrid.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString();

                List<CustomUserRecord> userRoleList = GetUserList();
                int idx = 0;
                int.TryParse(e.CommandArgument.ToString(), out idx);
                CustomUserRecord user = userRoleList[idx];
                SetUpdateUserMode(user);
            }
        }
Holmes IV
  • 1,673
  • 2
  • 23
  • 47
  • What answer? WPF or Winforms? – lokusking Jul 08 '16 at 18:56
  • Add `CommandArgument` to a `LinkButton` (or whatever you click) with `username` value. – Alex Kudryashev Jul 08 '16 at 18:58
  • @lokusking http://www.codeproject.com/Questions/191252/Data-grid-view-selected-cell-value-how-to-get-it – Holmes IV Jul 08 '16 at 18:58
  • Let me guess. You use WPF/ASP and the answer is based on WinForms? And please make sure, you are not mixing up `GridView` and `DataGridView` – lokusking Jul 08 '16 at 19:00
  • @lokusking no idea, to be honest this is my first time in working with C#, usually us VB or SQL. It seems it should be super easy. The code above uses the Index from another list it generates, the problem is if the initial list is filtered, you click the 4th, but it pulls the 4th from the new list so they ~never match. If it helps/matters I am in MS VS 2010, working on .aspx and aspx.cs files – Holmes IV Jul 08 '16 at 19:05

1 Answers1

1

According to this post

something like this should help:

int rowIndex = Convert.ToInt32(e.CommandArgument);
userListGrid.Rows[rowIndex].Cells[YourIndexHere].Text
Community
  • 1
  • 1
lokusking
  • 7,396
  • 13
  • 38
  • 57
  • Thanks, this worked, no sure why google didn't find that other page, but maybe I just had the wrong order of words or something. Thanks again. – Holmes IV Jul 08 '16 at 19:18
  • I know I already said this worked, but for some reason it only works on column other than column[0], everything in column[0] comes back blank – Holmes IV Jul 08 '16 at 21:17