locked
show values in datagrid RRS feed

  • Question

  • hi

    i have a KeyValuePair<string,string>

    i fill it below like :

    Expr1,"str1"

    Expr1,"str2"

    Expr2,"str3"

    Expr1,"str4"

    Expr2,"str5"

    and i'll show it in datagrid below like :

    Expr1          Expr2

    str1             str3

    str2             str5

    str4

    please help me how to it do

    thanks

    Wednesday, December 14, 2011 9:01 AM

Answers

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                //populate List
                List<MyData> list = new List<MyData>();
                list.Add(new MyData { Key = "Expr1", Value = "str1" });
                list.Add(new MyData { Key = "Expr1", Value = "str2" });
                list.Add(new MyData { Key = "Expr2", Value = "str3" });
                list.Add(new MyData { Key = "Expr1", Value = "str4" });
                list.Add(new MyData { Key = "Expr2", Value = "str5" });
    
                //Create DataTable
                DataTable table = new DataTable("MyData");
                table.Columns.Add("Expr1", typeof(String));
                table.Columns.Add("Expr2", typeof(String));
    
                //Add to DataTable
                var expr1 = list.Where(i => i.Key == "Expr1").Select(i => i.Value);
                foreach (var item in expr1)
                    table.Rows.Add(item, "");
                var expr2 = list.Where(i => i.Key == "Expr2").Select(i => i.Value);
                Int32 count = 0;
                foreach (var item in expr2)
                {
                    table.Rows[count][1] = item;
                    count++;
                }
                dataGridView1.DataSource = table;
    
    
            }
    
            public class MyData
            {
                public String Key { get; set; }
                public String Value { get; set; }
            }
        }
    }
    
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    • Marked as answer by Bob Shen Monday, December 26, 2011 4:40 AM
    Wednesday, December 14, 2011 3:02 PM

All replies

  • i think must create a class with dynamic propries
    Wednesday, December 14, 2011 12:51 PM
  • I don't think you can do so, as Keyvalue pair (I'm considering A Dictionary) ISn't IList or an IListSource.

    Please elaborate your need so that we can come up with solution


    Regards Kumar Gaurav.
    Wednesday, December 14, 2011 1:50 PM
  • thanks for your answer

    what is your solution

    Wednesday, December 14, 2011 2:02 PM
  • How can you fill a KeyValuePair with more than one pair? Do you mean you have a Dictionary?
    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    Wednesday, December 14, 2011 2:50 PM
  • thanks for your answer

    what is your solution


    As i told u, Please tell us your requirement in details.

    Regards Kumar Gaurav.
    Wednesday, December 14, 2011 2:56 PM
  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
    
                //populate List
                List<MyData> list = new List<MyData>();
                list.Add(new MyData { Key = "Expr1", Value = "str1" });
                list.Add(new MyData { Key = "Expr1", Value = "str2" });
                list.Add(new MyData { Key = "Expr2", Value = "str3" });
                list.Add(new MyData { Key = "Expr1", Value = "str4" });
                list.Add(new MyData { Key = "Expr2", Value = "str5" });
    
                //Create DataTable
                DataTable table = new DataTable("MyData");
                table.Columns.Add("Expr1", typeof(String));
                table.Columns.Add("Expr2", typeof(String));
    
                //Add to DataTable
                var expr1 = list.Where(i => i.Key == "Expr1").Select(i => i.Value);
                foreach (var item in expr1)
                    table.Rows.Add(item, "");
                var expr2 = list.Where(i => i.Key == "Expr2").Select(i => i.Value);
                Int32 count = 0;
                foreach (var item in expr2)
                {
                    table.Rows[count][1] = item;
                    count++;
                }
                dataGridView1.DataSource = table;
    
    
            }
    
            public class MyData
            {
                public String Key { get; set; }
                public String Value { get; set; }
            }
        }
    }
    
    


    John Grove, Senior Software Engineer http://www.digitizedschematic.com/
    • Marked as answer by Bob Shen Monday, December 26, 2011 4:40 AM
    Wednesday, December 14, 2011 3:02 PM
  • thanks for your answer

    but i use of in silver light , and in silver light i can't use of DataTable

    if possible show me another solution

    thanks

    Friday, December 16, 2011 10:39 AM
  • For silverlight issue, you can post here http://forums.silverlight.net/
    Tuesday, December 20, 2011 3:23 AM