TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

C# Convert List to DataTable: DataGridView

This C# example program converts a List to a DataTable instance.

Convert list, DataTable. The DataGridView can display a List.

This can be done by converting the List to a DataTable first. With some custom List to DataTable conversion logic, we get the best performance from the DataGridView by using the DataSource property.

Example. This example shows a List of string arrays. This is a data structure that could be parsed from a text file easily, as with the Split method. We introduce the ConvertListToDataTable method, which converts a List<string[]> to a DataTable.

Here: We create a new empty DataTable. Next, we find the maximum number of columns that will be required by looping through the input List.

Then: We add those columns, and then add all the rows. We return the DataTable.

Based on:

.NET 4

C# program that converts List to DataTable for DataGridView

using System;
using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
	public Form1()
	{
	    InitializeComponent();
	}

	private void Form1_Load(object sender, EventArgs e)
	{
	    // Example list.
	    List<string[]> list = new List<string[]>();
	    list.Add(new string[] { "Column 1", "Column 2", "Column 3" });
	    list.Add(new string[] { "Row 2", "Row 2" });
	    list.Add(new string[] { "Row 3" });

	    // Convert to DataTable.
	    DataTable table = ConvertListToDataTable(list);
	    dataGridView1.DataSource = table;
	}

	static DataTable ConvertListToDataTable(List<string[]> list)
	{
	    // New table.
	    DataTable table = new DataTable();

	    // Get max columns.
	    int columns = 0;
	    foreach (var array in list)
	    {
		if (array.Length > columns)
		{
		    columns = array.Length;
		}
	    }

	    // Add columns.
	    for (int i = 0; i < columns; i++)
	    {
		table.Columns.Add();
	    }

	    // Add rows.
	    foreach (var array in list)
	    {
		table.Rows.Add(array);
	    }

	    return table;
	}
    }
}

Output
    Please see screenshot above.

Assign to DataSource. Once we have a DataTable instance, it is appropriate to assign it to the DataSource property. It is not a good idea to assign the List<string[]> to the DataSource. It will not be displayed properly.

DataSource

Note: The DataSource provides the best performance for displaying large amounts of data instantly in a DataGridView.

Warning: If you were to add Columns and Rows individually, slowdowns are more likely to occur.

Discussion. DataTable can be used as an optimization. Using controls in Windows Forms or WPF directly often results in rendering. This is often much slower, and more processor-intensive, than in-memory operations.

So: When using DataGridView or similar controls, processing the data first seems like it might be slower, but it ends up much faster.

DataGridView

 

Summary. We converted a List of string arrays into a DataTable, yielding a collection that can be displayed in a DataGridView with the DataSource property. You could imperatively add rows and columns to the DataGridView. But this can be slower.

Instead: Building a collection in-memory with a DataTable is often more effective.


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf