Search code examples
c#gridviewintdatagridviewcolumn

Get cell value from gridview and convert it into int


I have a gridview, that has 4 columns, the first column has a select button, and I need to get the values of the fourth column, depending on which button do u press. And i need to get or convert the value of the fourth column into int. Thanks for the help!

My code is

int ci = Convert.ToInt32(grdClientes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[4].ToString()); 
List<int> listaTels = lgCliente.ListaTelefonos(ci);

Solution

  • You are quite close:

    int ci = Convert.ToInt32(grdClientes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[4].Text); 
    List<int> listaTels = lgCliente.ListaTelefonos(ci);
    

    Also, I would suggest int.TryParse and use of var instead of List.

    To make your code more readable, you can use this:

    int currentRowIndex = Convert.ToInt32(e.CommandArgument); // Get the current row
    

    Now you can use currentRowIndex to get cell text.