简体   繁体   English

在c#中的datagridview中将不同的列表绑定到列中

[英]Bind different lists into columns in datagridview in c#

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. 但是,我需要将此列表绑定到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. 我有Datagridview。 I want to bind this into data grid view as, 我想将其绑定到数据网格视图中, 在此输入图像描述

how might I add datasource to this datagridview? 我如何将数据源添加到此datagridview? I tired to include this into datatable. 我厌倦了把它包含在数据表中。 In any case, I couldn't accomplish this. 无论如何,我无法做到这一点。 Any help would be truly valued. 任何帮助都会得到真正的重视。

You can create a BooK class and shape your data in a List<Book> and bind the grid to the list. 您可以在List<Book>创建BooK类并对数据进行整形,并将网格绑定到列表中。

Book

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

Bind data to DataGridView 将数据绑定到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. 基于注释,我想你有一个Dictionary<String, List<String>> ,你想要动态地将它们添加到网格中。

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: 这是结果:

在此输入图像描述

I guess you need to make a DataTable and add data in it. 我想你需要创建一个DataTable并在其中add数据。

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 . 如果您有Lists ,则可以将List作为DataTable column ,并且您在List中添加的数据将是该column row

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 . 如果您收到List ,你可以loop通过list中添加数据RowsDataTable

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM