数据网格视图列

时间:2013-08-06 16:21:45

标签: c# datagridview

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

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

2 个答案:

答案 0 :(得分: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));

答案 1 :(得分:1)

查看this link

从链接

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

yourDataGridView.Columns.Remove(/*the column you want to remove*/);