-
June 15th, 2013, 12:43 AM
#1
Adding List<string> to DataGridView
Hello All,
I created a Class Library and I am trying to add data from a string to a DataGridViewer.
The string is in the format as follows:
"Test, 1", "Glucose", "10/24/2013 10:00"; "Test, 2", "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 datagridviewer with four columns.
1. Selected - Checkbox
2. Patient Name - String
3. Order Name - String
4. Order Date - String
5. Reason - Combo Box
I am getting an error that 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. If I need to
post anything else, please let me know.
I just wantt he data to display and I will work on the rest of it.
Any help would be appreciated.
Thank you,
Tony
This is the Designer Code
Code:
namespace TEST.RenewOrders
{
partial class RenewOrders
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.dataGridView1 = new System.Windows.Forms.DataGridView();
this.dgvSelect = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.dgvPatientName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dgvOrderName = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dgvOrderDateTime = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dgvReason = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.btnOK = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.btnSelectAll = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).BeginInit();
this.SuspendLayout();
//
// dataGridView1
//
this.dataGridView1.AllowUserToDeleteRows = false;
this.dataGridView1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dataGridView1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dgvSelect,
this.dgvPatientName,
this.dgvOrderName,
this.dgvOrderDateTime,
this.dgvReason});
this.dataGridView1.Location = new System.Drawing.Point(12, 12);
this.dataGridView1.Name = "dataGridView1";
this.dataGridView1.Size = new System.Drawing.Size(695, 311);
this.dataGridView1.TabIndex = 0;
//
// dgvSelect
//
this.dgvSelect.HeaderText = "Selected";
this.dgvSelect.Name = "dgvSelect";
this.dgvSelect.ToolTipText = "Click to Renew Order";
this.dgvSelect.Width = 52;
//
// dgvPatientName
//
this.dgvPatientName.HeaderText = "Patient Name";
this.dgvPatientName.Name = "dgvPatientName";
this.dgvPatientName.Width = 175;
//
// dgvOrderName
//
this.dgvOrderName.HeaderText = "Order Name";
this.dgvOrderName.Name = "dgvOrderName";
this.dgvOrderName.Width = 125;
//
// dgvOrderDateTime
//
this.dgvOrderDateTime.HeaderText = "Order Date/Time";
this.dgvOrderDateTime.Name = "dgvOrderDateTime";
this.dgvOrderDateTime.Width = 125;
//
// dgvReason
//
this.dgvReason.HeaderText = "Renewal Reason";
this.dgvReason.Name = "dgvReason";
this.dgvReason.ToolTipText = "Select a Reason to Renew Order";
this.dgvReason.Width = 175;
//
// btnOK
//
this.btnOK.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnOK.Location = new System.Drawing.Point(12, 342);
this.btnOK.Name = "btnOK";
this.btnOK.Size = new System.Drawing.Size(79, 41);
this.btnOK.TabIndex = 1;
this.btnOK.Text = "OK";
this.btnOK.UseVisualStyleBackColor = true;
//
// btnCancel
//
this.btnCancel.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnCancel.Location = new System.Drawing.Point(140, 342);
this.btnCancel.Name = "btnCancel";
this.btnCancel.Size = new System.Drawing.Size(82, 41);
this.btnCancel.TabIndex = 2;
this.btnCancel.Text = "Cancel";
this.btnCancel.UseVisualStyleBackColor = true;
//
// btnSelectAll
//
this.btnSelectAll.Font = new System.Drawing.Font("Microsoft Sans Serif", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
this.btnSelectAll.Location = new System.Drawing.Point(616, 342);
this.btnSelectAll.Name = "btnSelectAll";
this.btnSelectAll.Size = new System.Drawing.Size(91, 41);
this.btnSelectAll.TabIndex = 3;
this.btnSelectAll.Text = "Select All";
this.btnSelectAll.UseVisualStyleBackColor = true;
//
// RenewOrders
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(717, 395);
this.Controls.Add(this.btnSelectAll);
this.Controls.Add(this.btnCancel);
this.Controls.Add(this.btnOK);
this.Controls.Add(this.dataGridView1);
this.Name = "RenewOrders";
this.Text = "Renew Orders";
this.Load += new System.EventHandler(this.RenewOrders_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGridView1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dataGridView1;
private System.Windows.Forms.Button btnOK;
private System.Windows.Forms.Button btnCancel;
private System.Windows.Forms.Button btnSelectAll;
private System.Windows.Forms.DataGridViewCheckBoxColumn dgvSelect;
private System.Windows.Forms.DataGridViewTextBoxColumn dgvPatientName;
private System.Windows.Forms.DataGridViewTextBoxColumn dgvOrderName;
private System.Windows.Forms.DataGridViewTextBoxColumn dgvOrderDateTime;
private System.Windows.Forms.DataGridViewComboBoxColumn dgvReason;
}
}
This is the program
Code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SCMLib;
using SCMLib.Context;
namespace TEST.RenewOrders
{
public partial class RenewOrders : Form
{
public static string strAllPatientData = string.Empty;
public static string strMLMPatientData = string.Empty;
public string selected_patient_data
{
get
{
return strAllPatientData;
}
}
public RenewOrders(string all_patient_data)
{
InitializeComponent();
strMLMPatientData = all_patient_data;
}
private void RenewOrders_Load(object sender, EventArgs e)
{
this.ConvertStringToList(strMLMPatientData);
}
private void ConvertStringToList(string strMLMPatientData)
{
List<string> patient_list = new List<string>(strMLMPatientData.Split(';'));
DataTable dtTable = ConvertListToDataTable(patient_list);
dataGridView1.DataSource = dtTable;
}
// Convert to DataTable.
static DataTable ConvertListToDataTable(List<string> patient_list)
{
// New table.
DataTable dtTable = new DataTable();
// Add rows.
foreach (var array in patient_list)
{
dtTable.Rows.Add(array);
}
return dtTable;
}
}
-
June 15th, 2013, 04:25 PM
#2
Re: Adding List<string> to DataGridView
The first thing to do is get away from the string format and into an object (i.e. Order object).
So parse the string and create a collection of Order objects.
Code:
List<Order> _orders
. Then bind the list to the DataGridView.
In general, it's good practice to do any [string] conversions up front and all in one place rather than having string parsing peppered throughout your code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|