5

I have dynamic list item. This list is not static. In view of the necessity, I will have at least one/more list. what I need is, I gotta split that list into at least two unique records. This I can part. Yet, I need to bind this list into datagridview.

This is my code: I have two list,

  List<string> list1=new List<string>();
  list1.add("THE BOOK OF BURGER");
  list1.add("The Hogwarts Library");
  list1.add("NEW Day in the Life of a Farmer");

  List<string> list2=new List<string>();
  list2.add("$200");
  list2.add("$150");
  list2.add("$170");

  .....
  list3,
  list4.. etc..

I have Datagridview. I want to bind this into data grid view as, enter image description here

how might I add datasource to this datagridview? I tired to include this into datatable. In any case, I couldn't accomplish this. Any help would be truly valued.

3
  • You can create a BooK class and shape your data in a List<Book> and bind the grid to the list. Commented Dec 11, 2015 at 6:57
  • You do not have to split it, have a look at stackoverflow.com/questions/16695885/… Commented Dec 11, 2015 at 6:57
  • This list is not static. Based on the requirement I will have one or more lists. So I can't create object for the list. I have updated the question. Commented Dec 11, 2015 at 7:03

2 Answers 2

5

You can create a BooK class and shape your data in a List<Book> and bind the grid to the list.

Book

public class Book
{
    public string Title { get; set; }
    public string Price { get; set; }
}

Bind data to DataGridView

var list = new List<Book>();
for (int i = 0; i < list1.Count; i++)
{
    list.Add(new Book()
    {
        Title = list1[i],
        Price = list2[i]
    });
}

this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = list;

EDIT

Based on comments, I suppose you have a Dictionary<String, List<String>> and you want to add them dynamically to the grid.

I suppose you will use the key of dictionary as header text for the column.

//Add your lists to the dictionary
var dictionary = new Dictionary<string, List<string>>();
dictionary.Add("list1", list1);
dictionary.Add("list2", list2);
// And some othet lists ....

//Calculate the count of rows.
int rowCount = dictionary.Cast<KeyValuePair<string, List<string>>>()
                         .Select(x=>x.Value.Count).Max();

//Create the data table and for each table, add a column
var dataTable = new DataTable();
foreach (var key in dictionary.Keys)
{
    dataTable.Columns.Add(key);
}

//Add items of each list to the columns of rows of table
for (int i = 0; i < rowCount; i++)
{
    var row = dataTable.Rows.Add();
    foreach (var key in dictionary.Keys)
    {
        try
        {
            row[key] = dictionary[key][i];
        }
        catch 
        {
            row[key] = null;
        }
    }
}

//Show data in grid
this.dataGridView1.AutoGenerateColumns = true;
this.dataGridView1.DataSource = dataTable;

and here is the result:

enter image description here

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

11 Comments

You have mentioned, class Book variables are static. In my scenario I won't get static list. I have modified my question. Hope its clear :) Thanks for your time.
@RajDeInno No, you don't need static class and I didn't say that, Do you mean you may have dynamic multiple lists?
Yep. title and book are not static. Its just an example. Similarly I may have 5 or 10 lists.
You can simply add other fields like Description, Author, ... to the Book model. And fill the list the same way that I did for 2 properties.
Nah. I don't know whether I am explained clearly. Let me try to clear this further. Actually this list is won't be fixed. Every time it will change. Its a dynamic list. Its based on the customer selection. So I can't go with the above solution.
|
0

I guess you need to make a DataTable and add data in it.

As you have the Lists you can make a List as column of DataTable and the data you are adding in List will be row of that column.

DataTable dt = new DataTable();
dt.Columns.Add("Title");
DataRow dr = dt.NewRow();
dr["Title"] = "THE BOOK OF BURGER";
dt.Rows.Add(dr);

dt.Columns.Add("Price");
DataRow dr = dt.NewRow();
dr["Price"] = "$200";
dt.Rows.Add(dr);

grdBooks.DataSource = dt;
grdBooks.DataBind();

If you are receiving List you can loop through list to add data in Rows of DataTable.

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.