0

我有一个填充的数据网格视图,如何删除我不想要的网格视图中的列?

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 文件解析为数据网格视图,其中只有门、员工姓名、卡号和日期、时间和顺序。

4

2 回答 2

1

看看这个链接

从链接我认为你可以尝试这样的事情:

yourDataGridView.Columns.Remove(/*the column you want to remove*/);
于 2013-08-06T18:12:04.810 回答
1

您可以隐藏一列,使其不显示在 DataGridView 中:

dataGridView.Columns["Column you want to hide"].Visible = false;

您可以创建一个仅包含所需列的新 DataTable 并使用 LINQ 添加行:

DataTable dt = new DataTable();
dt.Columns.Add("ID");
dt.Columns.Add("Blah1");
dt.Columns.Add("Blah2");

for (int i = 1; i < 5; i++) {
    DataRow dr = dt.NewRow();
    dr["ID"] = i;
    dr["Blah1"] = "Blah1" + i.ToString();
    dr["Blah2"] = "Blah2" + i.ToString();
    dt.Rows.Add(dr);
}

var Query1 = from row in dt.AsEnumerable()
             select new { ID = row["ID"], Blah1 = row["Blah1"] };

DataTable newDT = new DataTable();
newDT.Columns.Add("ID");
newDT.Columns.Add("Blah1");

Query1.ToList().ForEach(r => newDT.Rows.Add(r));
于 2013-08-06T16:32:12.207 回答