我有一个填充的数据网格视图,如何删除我不想要的网格视图中的列?
private void csvButton_Click(object sender, EventArgs e)
{
string delimiter = ",";
string tablename = "CsvFile";
DataSet dataset = new DataSet();
OpenFileDialog csvOpenFileDialog1 = new OpenFileDialog();
csvOpenFileDialog1.Filter = "CSV Files (*.csv)|*.csv|All Files (*.*)|*.*";
csvOpenFileDialog1.FilterIndex = 1;
if (csvOpenFileDialog1.ShowDialog() == DialogResult.OK)
{
if (MessageBox.Show("Are you sure you want to import the data from \n " + csvOpenFileDialog1.FileName + "?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
filename = csvOpenFileDialog1.FileName;
StreamReader sr = new StreamReader(filename);
string csv = File.ReadAllText(csvOpenFileDialog1.FileName);
string allData = sr.ReadToEnd();
string[] rows = allData.Split("\n".ToCharArray());
bool header = true;
foreach (string r in rows)
{
string[] items = r.Split(delimiter.ToCharArray());
if (header)
{
dataset.Tables.Add(tablename);
dataset.Tables[tablename].Columns.Add(items[0]);//Sequence\\
dataset.Tables[tablename].Columns.Add(items[1]);//Date and Time\\
dataset.Tables[tablename].Columns.Add(items[2]);//Event message\\
dataset.Tables[tablename].Columns.Add(items[3]);//Event number\\
dataset.Tables[tablename].Columns.Add(items[4]);//Object #1\\
dataset.Tables[tablename].Columns.Add(items[5]);//doors\\
dataset.Tables[tablename].Columns.Add(items[6]);//Object #2\\
dataset.Tables[tablename].Columns.Add(items[7]);//Employee name\\
dataset.Tables[tablename].Columns.Add(items[8]);//Object #3\\
dataset.Tables[tablename].Columns.Add(items[9]);//Description #3\\
dataset.Tables[tablename].Columns.Add(items[10]);//Object #4\\
dataset.Tables[tablename].Columns.Add(items[11]);//Description #4\\
dataset.Tables[tablename].Columns.Add(items[12]);//keycard\\
header = false;
}
else
{
if (!String.IsNullOrEmpty(items[0]))
{
dataset.Tables[tablename].Rows.Add(items);
}
}
我需要适当的代码将我的 csv 文件解析为数据网格视图,其中只有门、员工姓名、卡号和日期、时间和顺序。