1

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);
Ehsan Sajjad
  • 61,834
  • 16
  • 105
  • 160
debiione
  • 13
  • 3
  • First: When you ask questions here you should show some effort to solve your problem,not just ask us to write some code for you. Next: There is an event named _CellContentClick_ and the event receives the row and column index of the click. Finally there are many ways to convert a string to an integer. Look for example to Convert.ToInt32 and/or Int32.TryParse – Steve Dec 15 '18 at 08:10
  • sorry, this was my first question, but Im not asking anyone to write my code, the only problem I have is, that I`m trying to convert a cell value into an int, and i cant, this is what i have: – debiione Dec 15 '18 at 08:22
  • int ci = Convert.ToInt32(grdClientes.Rows[Convert.ToInt32(e.CommandArgument)].Cells[4].ToString()); List listaTels = lgCliente.ListaTelefonos(ci); – debiione Dec 15 '18 at 08:23
  • try like: `var row=grdClientes.CurrentRow;` or `DataGridViewRow row = grdClientes.Rows[grdClientes.SelectedCells[0].RowIndex];` and then : `Convert.ToInt32(row.Cells["4"].Value)` – Ehsan Sajjad Dec 15 '18 at 08:50

1 Answers1

1

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.

Gauravsa
  • 6,330
  • 2
  • 21
  • 30