I have a List of objects (all strings) that populates a DataGridView however I am unable to use the column headers to sort. I have been reading up on SortableBindingList to solve the problem, sadly however, since I have only been learning code for a few days, I am unable to understand how to implement it. Any advice to get me moving again would be greatly appreciated (please keep it simple as possible).
Here is my code so far
{
public class Product
{
public string itemCode;
public string description;
public string currentCount;
public string onOrder;
public string ItemCode
{
get
{
return itemCode;
}
set
{
itemCode = value;
}
}
public string Description
{
get
{
return description;
}
set
{
description = value;
}
}
public string CurrentCount
{
get
{
return currentCount;
}
set
{
currentCount = value;
}
}
public string OnOrder
{
get
{
return onOrder;
}
set
{
onOrder = value;
}
}
}
{
class Program
{
public static List<Product> itemList = new List<Product>();
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
//Reads csv file
var filestream = new FileStream(@"c:\StockFile\stocklist.csv", FileMode.Open,
FileAccess.Read, FileShare.ReadWrite);
var file = new StreamReader(filestream, Encoding.UTF8, true, 128);
string lineOfText;
bool firstLine = true;
while ((lineOfText = file.ReadLine()) != null)
{
if (!firstLine)
{
// splits the values
string[] elements;
Product newItem = new Product();
elements = lineOfText.Split(',');
newItem.itemCode = elements[0];
newItem.description = elements[1];
newItem.currentCount = elements[2];
newItem.onOrder = elements[3];
// add to list
itemList.Add(newItem);
}
else
firstLine = false;
}
//loads grid
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
//Output to file
using (TextWriter tw = new StreamWriter(@"C:\StockFile\stocklist.csv"))
{
tw.WriteLine("Item Code,Item Description,Current Count,On Order");
foreach (Product newItem in itemList)
{
tw.WriteLine(newItem.ItemCode + "," + newItem.Description + "," + newItem.CurrentCount + "," + newItem.OnOrder);
}
}
}
}
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//Binds data to Grid
private void Form1_Load(object sender, EventArgs e)
{
var source = new BindingSource();
source.DataSource = Program.itemList;
dataGridView1.DataSource = source;
dataGridView1.AutoGenerateColumns = true;
this.Controls.Add(dataGridView1);
dataGridView1.Refresh();
foreach (DataGridViewColumn column in dataGridView1.Columns)
{
dataGridView1.Columns[column.Name].SortMode = DataGridViewColumnSortMode.Automatic;
}
//Make Colomns read only
foreach (DataGridViewColumn col in dataGridView1.Columns)
{
if (col.Name == "CurrentCount")
{
col.ReadOnly = false;
}
else
{
col.ReadOnly = true;
}
}
}
}
There is example code for a SortableBindingList here...
https://stackoverflow.com/a/40051526/2093531
To convert a List to a SortableBindingList...
SortableBindingList<YourListType> sortableBindingList = new SortableBindingList<YourListType>(list);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.