如果您手动创建列并将其添加到您的DataGridView
中,我认为您可以保持DataSet
-loading 过程不变。
关闭AutoGenerateColumns
并手动添加它们:
dataGridView1.AutoGenerateColumns = false;
dataGridView1.Columns.Add(new DataGridViewTextBoxColumn { Name="Course", ... } );
dataGridView1.Columns.Add(new DataGridViewCheckBoxColumn { Name="Passed", ... } );
...
DataView
将网格视图与您正在执行的操作绑定后,应该会填充网格视图:
dataGridView1.DataSource = dataset.Tables[0].DefaultView;
编辑
因为它依赖于强类型和适当的绑定,可能更好的选择是创建一个类来表示 CSV 记录(假定类型)。如果该类实现了INotifyPropertyChanged
接口,则可以在 中编辑值DataGridView
并在该类中以最小的努力反映出来。
public class CsvDataRow : INotifyPropertyChanged
{
private bool _aa;
private string _course;
private int _semester;
private double _theory;
private double _lab;
private bool _passed;
public bool AA { get { return _aa; } set { if (value == _aa) return; _aa = value; NotifyPropertyChanged("AA"); } }
public string Course { get { return _course; } set { if (value == _course) return; _course = value; NotifyPropertyChanged("Course"); } }
public int Semester { get { return _semester; } set { if (value == _semester) return; _semester = value; NotifyPropertyChanged("Semester"); } }
public double Theory { get { return _theory; } set { if (value == _theory) return; _theory = value; NotifyPropertyChanged("Theory"); } }
public double Lab { get { return _lab; } set { if (value == _lab) return; _lab = value; NotifyPropertyChanged("Lab"); } }
public bool Passed { get { return _passed; } set { if (value == _passed) return; _passed = value; NotifyPropertyChanged("Passed"); } }
char _delimiter;
// static factory method creates object from CSV row
public static CsvDataRow Create(string row, char delimiter)
{
return new CsvDataRow(row, delimiter);
}
// private constructor initializes property values
private CsvDataRow(string row, char delimiter)
{
_delimiter = delimiter;
var values = row.Split(_delimiter);
AA = (values[0].ToString().Equals("1"));
Course = Convert.ToString(values[1]);
Semester = Convert.ToInt32(values[2]);
Theory = Convert.ToDouble(values[3]);
Lab = Convert.ToDouble(values[4]);
Passed = (values[5].ToString().Equals("1"));
}
// a method to convert back into a CSV row
public string ToCsvString()
{
var values = new string[] { (AA ? 1 : 0).ToString(), Course, Semester.ToString(), Theory.ToString(), Lab.ToString(), (Passed ? 1: 0).ToString() };
return string.Join(_delimiter.ToString(), values);
}
// INotifyPropertyChanged interface requires this event
public event PropertyChangedEventHandler PropertyChanged;
// helper method to raise PropertyChanged event
private void NotifyPropertyChanged(string name)
{
if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(name));
}
}
CSV 文件本身可以由一个类表示:
public class CsvDataSource
{
public char Delimiter { get; private set; }
public CsvDataSource()
: this(',')
{ }
public string[] Columns { get; private set; }
public ObservableCollection<CsvDataRow> Rows { get; private set; }
public CsvDataSource(char delimiter)
{
Delimiter = delimiter;
}
public void LoadCsv(string csvFileName)
{
string header;
string data;
using (var reader = new StreamReader(csvFileName))
{
header = reader.ReadLine(); // assumes 1st row is column headers
data = reader.ReadToEnd();
}
Columns = header.Split(Delimiter);
var rows = Regex.Split(data, Environment.NewLine);
if (!rows.Select(row => row.Split(Delimiter)).All(row => row.Length == Columns.Length)) throw new FormatException("Inconsistent data format.");
Rows = new ObservableCollection<CsvDataRow>(rows.Select(row => CsvDataRow.Create(row, Delimiter)));
}
}
然后可以将 DataGridView 绑定到实例的Rows
属性CsvDataSource
,因此表单的代码如下所示:
public partial class Form1 : Form
{
private CsvDataSource _data;
private ObservableCollection<CsvDataRow> _rows;
public Form1()
{
InitializeComponent();
}
public void LoadCsv(CsvDataSource data)
{
_data = data;
_rows = _data.Rows;
dataGridView1.DataSource = _rows;
}
public void SaveCsv(string path)
{
using (var writer = new StreamWriter(path))
{
writer.WriteLine(string.Join(_data.Delimiter.ToString(), _data.Columns));
foreach (var row in _rows)
{
writer.WriteLine(row.ToCsvString());
}
}
}
}
当然,您会有一个按钮调用该SaveCsv
方法,并且您需要另一个按钮来向/从您的 DataGridView 添加/删除行。此外,您还需要将文件 I/O 操作包装在 try/cath 块中。
这是一个快速总结的实现,确实需要一些额外的工作才能完全发挥作用,但它提供了一个想法,并且开箱即用。
这样,“定义列”部分在CsvDataRow
为每个记录提供强类型的类中完全得到处理。不DataSet
,不DataColumn
,也不DataGridViewColumn
。只是数据,还有一些数据绑定。