Click here to Skip to main content
15,867,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
how to take key and value from dictionary? The dictionary is made up of <long,string> and I would like to put the values ​​in a line.
The number of elements in the dictionary is not known, so I would have it in some cycle to be sure, but I don't know how to select Key and Value.

What I have tried:

C#
DataGridView textBox = new DataGridView();
           textBox.ColumnCount = 2;
           textBox.ColumnHeadersDefaultCellStyle.BackColor = Color.Navy;
           textBox.ColumnHeadersDefaultCellStyle.ForeColor = Color.White;
           textBox.ColumnHeadersDefaultCellStyle.Font =
               new Font(textBox.Font, FontStyle.Bold);
           textBox.Columns[0].Name = "ID";
           textBox.Columns[1].Name = "Jméno";
           string[] row0 = {listAnotaci[0].Key.ToString(),listAnotaci[0].Value};//It doesn't work like that, I just kind of imagined it without a cycle
           string[] row1 = { listAnotaci[1].Key.ToString(),listAnotaci[1].Value};//It doesn't work like that, I just kind of imagined it without a cycle
           textBox.Rows.Add(row0);
           textBox.Rows.Add(row1);

Could someone advise?

Thank you in advance

David
Posted
Updated 24-Aug-22 23:06pm

1 solution

Try:
C#
foreach (KeyValuePair<long, string> pair in listAnotaci)
{
    textBox.Rows.Add(new[] { pair.Key.ToString(), pair.Value });
}
 
Share this answer
 
Comments
dejf111 25-Aug-22 5:26am    
That's perfect, thanks a lot.

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