0

I am storing the value of gridview into a string. I have two lines of code.

string UserName = GridView1.Rows[e.RowIndex].Cells[4].Text.ToString(); 1

string UserName = GridView1.Rows[e.RowIndex].Values["UserName"].ToString(); 2

Both will have same Values. Which is the better option to use 1 or 2 ?

1
  • What is Values here? Commented Oct 19, 2015 at 7:47

2 Answers 2

2

I prefer to use the second one, so that if the column index change in time, it's not necessary to rewrite all indexes in code again.

Sign up to request clarification or add additional context in comments.

1 Comment

In terms of Execution Time?
1

The second one:

  1. Its more future proof, changing the structure of the table would require you to change the cell index.
  2. Its easier to read, without looking at the variable we can see you are asking for the usernames, We don't have to check what values are in the 4th column.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.