Remove a column from gridview

Asked

Viewed 1,350 times

0

I’m implementing a functionality in a project using Asp.net mvc and I’m trying to remove some columns from a gridview that I created in my application, the gridview datasource is a table in the database, but I want to remove some columns from the table, because I am trying to export this data to an excel file and some table items like the primary key are not required. How do I remove the columns from my gridview choice? I’ve tried things like:

gv.Columns.Remove("ArquivoExcelId");

also tried to:

gv.Columns[0].Visible = false;

but the 2 generated error, as if the past parameters were incomparable or something like, someone knows how to do this?

  • 2

    Which error does it generate? which code are you using to generate excel?

  • If it is Asp.net mvc you will not be able to do this via server side code. Try to hide the column using jquery.

2 answers

0

Try this:

private void BindDataAndInitializeColumns()
{
    dataGridView1.AutoGenerateColumns = true;
    dataGridView1.DataSource = customersDataSet;
    dataGridView1.Columns.Remove("Fax");
    dataGridView1.Columns["CustomerID"].Visible = false;
}

Taken from the MSDN.

Or with the gv.Columns.RemoveAt(x);

  • I tried already this solution and had not succeeded, I did not know, but da para fazer if utilizando operação ternaria no Inq, I got through this select what I wanted, thanks.

0


Friend, why don’t you filter the column by removing it from the select?

You can also make the change via front-end, by css putting style = "display: none;" in the column.

Another solution via server side is to mount the columns through the RowDataBound. If you prefer, treat Dataset:

gv.Columns.RemoveAt(0); //Index da coluna 'ArquivoExcelId'

Or

gv.Columns.Remove("ArquivoExcelId");

  • Thanks Marllon, I managed to do using a tender operation in Brasili, I did not want in select pq have other things in the application that this was impacting, anyway thank you!

  • Ball show, joao igor! If you can, edit your question with the solution to help other people who have the same question.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.