简体   繁体   中英

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. But unfortunately, the excel numbering also comes inside 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. 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). 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:

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

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 ::

//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

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'". 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 "";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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