Csharp_learner
Csharp_learner

Reputation: 341

how to map data from a data dictionary to datagridview?

I have a data dictionary which is as follows:

Key  Value

1    ron k polito
2    Ryan s Winslate    

I am trying to display the Value column in a datagridview with three different columns firstname, midname and lastname. The below statement names.getnames().ToList() retrieves the names (value) from the data dictionary.

datagridview1.DataSource = names.getnames().ToList();

By moving the list into datagridview creates exactly same number of rows as total number of count retrieved by the data dictionary, but it doesnot display the anything. just the blank rows appear.

I am new to C#, any suggestion how can I fix this.

Thanks.

Here's the code:

             List<localname> lname_list = new List<localname>();
             var l_names = names.getnames().ToList();
             foreach(var lcname in l_names)
             {
                 string[] wrd = lcname.Split(' ');
                 localname lv = new localname();
                 lv.mylocal(wrd[0], wrd[1], wrd[2]);
                 lname_list.Add(lv);
             }
             datagridview1.DataSource = lname_list;
             ShowDialog();

Upvotes: 0

Views: 11332

Answers (3)

adnan umar
adnan umar

Reputation: 127

Its simple, you need nothing extra coding. just do this

Grd.DataSource=null;

Grd.DataSource = DictionaryObject.Values.ToList();

thats it

Upvotes: 0

Prashanth Thurairatnam
Prashanth Thurairatnam

Reputation: 4361

With LINQ you can do this with few lines of code.

Provided you have a Custom class like

class CustomerName
{
   public string FirstName { get; set;}
   public string MidName { get; set; }
   public string LastName { get; set; }
}

and your dictionary value has the first. mid, last names (meaning the value should always have 3 parts); you can do this.

Dictionary<int, string> dic = new Dictionary<int, string>();
dic.Add(1, "ron k polito");
dic.Add(2, "Ryan s Winslate");

var dicNames = (from a in (dic.Select(b => b.Value).Select(c => c.Split(' ')).AsEnumerable()) select new CustomerName { FirstName = a[0], MidName = a[1], LastName = a[2] }).ToList();            

dataGridView1.DataSource = dicNames;

Ouput

enter image description here

Upvotes: 1

d.moncada
d.moncada

Reputation: 17402

You will need a List of a specified POCO that will be used to populate the DataGrid. The POCO will contain properties that will represent each column you want..example:

public class Name
{
    public string First { get; set; }
    public string Middle { get; set; }
    public string Last { get; set; }
}

Inside your form, you will populate the list based on your getNames function.

   public Form1()
    {
        InitializeComponent();

        List<Name> namesList = new List<Name>();

        dataGridView1.AutoGenerateColumns = false;
        DataGridViewTextBoxColumn firstNameColumn = new DataGridViewTextBoxColumn();
        makeColumn.DataPropertyName = "First";
        makeColumn.HeaderText = "First Name";

        DataGridViewTextBoxColumn middleNameColumn = new DataGridViewTextBoxColumn();
        modelColumn.DataPropertyName = "Middle";
        modelColumn.HeaderText = "Middle Name";

        DataGridViewTextBoxColumn lastNameColumn = new DataGridViewTextBoxColumn();
        yearColumn.DataPropertyName = "Last";
        yearColumn.HeaderText = "Last Name";

        dataGridView1.Columns.Add(firstNameColumn);
        dataGridView1.Columns.Add(middleNameColumn);
        dataGridView1.Columns.Add(lastNameColumn);

        for (int i = 0; i < names.getNames().Length; i++)
        {
            // TODO create new Name object. 
            // TODO set properties in new Name object accordingly.
            // TODO add name object to namesList once properties have been set.
        }

        dataGridView1.DataSource = namesList;
    }

Instead of a List, you can also use an ObservableCollection, or BindingList

Upvotes: 2

Related Questions