简体   繁体   English

如何在C#窗口形式的DataGridView中删除无标题的无标题列

[英]How to Remove an untitled column without header in DataGridView in c# window form

**I am importing excel data into datagridview and I have to use this data into my word documents using parameters. **我正在将excel数据导入datagridview,并且必须使用参数将这些数据用于word文档中。 But unfortunately, the excel numbering also comes inside gridview. 但不幸的是,excel编号也出现在gridview中。 It is without header whom I unable to remove either use using index either anything. 没有标题,我无法删除任何使用索引的使用。 nore can hide because it doesn't have a header. Nore可以隐藏,因为它没有标题。 so because of this, I am unable to select any row because to this first untitled header. 因此,由于这个第一个无标题标题,我无法选择任何行。 Please help me I really need help. 请帮助我,我真的需要帮助。 either tell me any code by using that code if i can import data from excel without row numbering of excel (Mean red circle column of pic). 要么通过使用该代码告诉我任何代码,如果我可以从excel导入数据而无需对excel行进行编号(图片的平均红色圆圈列)。 Either any way to destroy the first column that doesn't have header ** 可以通过任何方式销毁没有标题的第一列**

在此处输入图片说明

OpenFileDialog openfile = new OpenFileDialog();
        openfile.Filter = "XLS files (*.xls, *.xlt)|*.xls;*.xlt|XLSX files (*.xlsx, *.xlsm, *.xltx, *.xltm)|*.xlsx;*.xlsm;*.xltx;*.xltm|ODS files (*.ods, *.ots)|*.ods;*.ots|CSV files (*.csv, *.tsv)|*.csv;*.tsv|HTML files (*.html, *.htm)|*.html;*.htm";
        openfile.FilterIndex = 2;
        if (openfile.ShowDialog() == DialogResult.OK)
        {
            ExcelFile ef = ExcelFile.Load(openfile.FileName);




            DataGridViewConverter.ExportToDataGridView(ef.Worksheets.ActiveWorksheet, this.dataGridView1, new ExportToDataGridViewOptions() { ColumnHeaders = true  });
            this.dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill;

不会像下面那样使列可见性变为false解决您的问题:

this.dataGridView1.Columns[0].Visible = false;

Just remove column from DataGridViewColumnCollection as: 只需将DataGridViewColumnCollection中的列删除为:

this.dataGridView1.Columns.Remove("");

This must work, my code sample 这必须工作,我的代码示例

private void AddRow()
    {
        this.dgvCount.Columns.Add("", "");
        this.dgvCount.Columns.Add("Stat", "Stat");
        this.dgvCount.Columns[""].Width = 100;
        this.dgvCount.Columns["Stat"].Width = 100;


        this.dgvCount.Rows.Add("1", "100");
        this.dgvCount.Rows.Add("2","200");
        this.dgvCount.Rows.Add("3","300");
        this.dgvCount.Rows.Add("4", "400");

        this.dgvCount.Columns.Remove("");
    }

UPDATE UPDATE

I zoomed pic & noticed that this is not DataGridViewColumn rather than these are Row Headers which were text when you import data from excel, i don't know why, Now just use following code to clear text of rows header :: 我放大了图片并注意到,这不是DataGridViewColumn,而是这些是行标题 ,当您从excel导入数据时,它们是文本,我不知道为什么,现在只使用以下代码清除行标题的文本::

//Remove text from Row Header Cell
        foreach (DataGridViewRow row in this.dgvCount.Rows)
        {
            row.HeaderCell.Value = "";     // Use :: null  OR "";
        }

This should work !! 这应该工作!

**You need to tell for every column what exactly computer want 20 **您需要为每一列告诉计算机究竟要多少20

The error says "The index is out of range". 该错误显示“索引超出范围”。 That means you were trying to index an object with a value that was not valid. 这意味着您正在尝试使用无效值索引对象。 If you have two books, and I ask you to give me your third book, you will look at me funny. 如果您有两本书,而我要您给我第三本书,您会觉得我很有趣。 This is the computer looking at you funny. 这是电脑看着你很有趣。 You said - "create a collection". 您说-“创建收藏集”。 So it did. 确实如此。 But initially the collection is empty: not only is there nothing in it - it has no space to hold anything. 但是最初,集合是空的:不仅其中没有任何内容-它没有空间容纳任何东西。 "It has no hands". “它没有手”。

Then you said "the first element of the collection is now 'ItemID'". 然后,您说“集合的第一个元素现在是'ItemID'”。 And the computer says "I never was asked to create space for a 'first item'." 然后计算机说:“我从来没有被要求为'第一件物品'创造空间。” I have no hands to hold this item you are giving me. 我没有手拿你给我的这个东西。

In terms of your code, you created a view, but never specified the size. 就您的代码而言,您创建了一个视图,但从未指定大小。 You need a ** 你需要一个 **

dataGridView1.Columns[0].Name = "FileRow";
            dataGridView1.Columns[1].Name = "FIRSTNAME1";
            dataGridView1.Columns[2].Name = "LASTNAME1";
            dataGridView1.Columns[3].Name = "ADDRESS1";
            dataGridView1.Columns[4].Name = "CITY1";
            dataGridView1.Columns[5].Name = "STATE1";
            dataGridView1.Columns[6].Name = "ZIP1";
            dataGridView1.Columns[7].Name = "FIRSTNAME2";
            dataGridView1.Columns[8].Name = "LASTNAME2";
            dataGridView1.Columns[9].Name = "ADDRESS2";
            dataGridView1.Columns[10].Name = "CITY2";

foreach (DataGridViewRow row in this.dgvCount.Rows)
    {
        row.HeaderCell.Value = "";     // Use :: null  OR "";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM