Adding List<string> to DataTable

19,306

First you need to spit the string appropriately to get a list of string arrays. Something like this:

var patient_list = new List<string[]>(strMLMPatientData.Split(';').Select(x => x.Split(',')));

or even better:

var patient_list = strMLMPatientData.Split(';').Select(x => x.Split(',')).ToList();

You need Linq for that, but you get the idea.

Then you need to add columns to your data table. You cant add rows to it when there are no columns..

Try something like this in your function

//add columns appropriately
DataTable table = new DataTable();
table.Columns.Add("Name", typeof(string));
table.Columns.Add("Order", typeof(string));
table.Columns.Add("Date", typeof(string));
foreach (var row in patient_list)
    table.Rows.Add(row);

return table;

See an example here. As it stands, your comma separated input string doesnt seem to match your data table column structure. You need to work it out. But I hope you got the idea which way to go.

Share:
19,306
eusanpe
Author by

eusanpe

Updated on June 08, 2022

Comments

  • eusanpe
    eusanpe almost 2 years

    I created a class library and I am trying to add data from a List<string[]> to a DataGridView.

    The string is in the format as follows:

    "Test, 1^Glucose^10/24/2013 10:00;Test, 2^BUN^10/25/2013 11:00;Test, 3^BUN^10/25/2013 11:00"

    I am passing the string from another program. I then an making it into a list and then trying to add it to the DataTable but no such luck. I created the datagridview with four columns.

    Selected - Checkbox

    Patient Name - String

    Order Name - String

    Order Date - String

    Reason - Combo Box

    I am getting an error:

    the list is greater than the number of columns.

    Note: I can make the string anyway I want before passing to this program so if I need to do something with the string before passing it to the program, please let me know. Is there an easier way?

    I just want the data to display and I will work on the rest of it.

    Any help would be appreciated.

    This is my code:

    public partial class RenewOrders : Form
    {
        public static string strMLMPatientData = string.Empty;
    
        public RenewOrders(string all_patient_data)
        {
            InitializeComponent();
            strMLMPatientData = "Test, 1^Glucose^10/24/2013 10:00;Test, 2^BUN^10/25/2013 11:00;Test, 3^BUN^10/25/2013 11:00"
        }
    
        private void RenewOrders_Load(object sender, EventArgs e)
        {    
            this.ConvertStringToList(strMLMPatientData);
        }
    
        private void ConvertStringToList(string strMLMPatientData)
        {
            var patient_list = strMLMPatientData.Split(';').Select(x => x.Split('^')).ToList();
            DataTable dtTable = ConvertListToDataTable(patient_list);
            dataGridView1.DataSource = dtTable;
        }
    
        // Convert to DataTable.
        static DataTable ConvertListToDataTable(List<string[]> patient_list)
        {
            // New table.
            DataTable dtTable = new DataTable();
    
            dtTable.Columns.Add("Name", typeof(string));
            dtTable.Columns.Add("Order Name", typeof(string));
            dtTable.Columns.Add("Order Date/Time", typeof(string));
            
            foreach (var row in patient_list)
            {
                table.Rows.Add(row);
            }
    
            return dtTable;
        }
    }