Click here to Skip to main content
15,882,552 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I have a DataGridView that receives and displays IDs from an external source. I know the nicknames of each ID.

In the "ID" column, how would you optimize to see which nickname corresponds to the cell ID 0

Here is an example of what I did (see below) which works well but the IF conditions are too long because there are nearly 200 IDs with their nicknames.

Do you know a more optimized way like a loop that checks a list that contains all the nicknames of the members ? I hope you understood what I mean ;)


What I have tried:

string id = row.Cells[0].Value.ToString();
var nom = "";

if (id == "FB23E1A8B7E2944FAAEC6219BBDF8243")
  nom = "Pseudo 1";
else if (id == "FE63D6040E22611D978B73064B3A2057")
  nom = "Pseudo 2";
else if (id == "0D44A8E3F29D9E568FE31C7DE45A80E0")
  nom = "Pseudo 3";
Updated 17-May-23 8:24am

This approach with 200 if conditions is not good.
There is a much better mechanism which is called Dictionary.
An explanation of Dictionary can be found here C# Dictionary[^]
Share this answer
QuickSilvain 2023 17-May-23 10:04am    
Thank you TheRealSteveJudge, I'll try, do you know if it's possible to store (ID and nicknames) in a file to use it in the dictionary?
Dave Kreskowiak 17-May-23 10:10am    
Your ID's and nicknames are just strings, so yes, they can be stored in a simple file and loaded into whatever data structure you want when you read the file.
Do you know a more optimized way like a loop that checks a list that contains all the nicknames of the members ?

A more primitive approach is to use a dichotomy search.
Binary search algorithm - Wikipedia[^]
First make a list of Ids and Names, make sorted on Ids.
Liste[]= {{"0D44A8E3F29D9E568FE31C7DE45A80E0", "Pseudo 3"},
    {"FB23E1A8B7E2944FAAEC6219BBDF8243", "Pseudo 1"},
    {"FE63D6040E22611D978B73064B3A2057", "Pseudo 2"},
    ... }

Then apply the binary search algorithm.
When you don't have dictionary container, Dichotomy is much simpler to create from scratch, and with same efficiency.
Share this answer
To add to what TheRealSteveJudge has said, your ID values are GUIDs - so use them as GUID values instead of converting them to strings:
var knownUsers = new Dictionary<Guid, string>();
You can then load the dictionary from (for example) a JSON file and you code becomes simple: "Is the GUID in the Dictionary? If not, we don't know him. If it is, use it as the key to access the alias."
Literally two or three lines of code, and the membership can be changed without needing to modify the app each time.
Share this answer
QuickSilvain 2023 17-May-23 18:07pm    
Thank you very much, I am a beginner in C#
Is it possible to give me an example of a dictionary that loads a JSON file?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900