0

I have a blank/unbound GridView control on my form and I am binding it in the code behind like this:

GridView1.DataSource = _dataSet
DataBind()

Running this code populates my GridView control with all the columns and data that _dataSet has. I want to display only some of the columns, change the column names, and rearrange some of these columns too (i want the last column from the actual sql database table to be displayed first in my GridView).

Can someone show me how do do this?

3 Answers 3

3

Set the AutoGenerateColumns property of the GridView to false and manually create the columns. That would be the easiest way.

Another way is to also set AutoGenerateColums property to false but this time, append the colums to the columns property of the GridView. Use the .Clear and the Add methods provided

Sign up to request clarification or add additional context in comments.

1 Comment

Could you please provide a code sample of your second solution? I don't know how to append columns to the Columns property since that property is read-only; unless I misunderstood you. Thanks.
2

First of all, in your GridView markup, you can change the header text:

<asp:BoundField DataField="SOME_COLUMN" HeaderText="Comment" SortExpression="SOME_COLUMN" />

Be sure to set AutoGenerateColumns to false.

Second of all, you can filter using DataSource.FilterExpression = "col1 = 'this'", if your dataset is being populated by a SqlDataSource or similar.

Comments

0

I've written an article that focuses on different ways to create columns in a GridView. It also discusses how to change the name of a column etc. I hope it will be of use to those, who are new to the GridView: http://www.tomot.de/en-us/article/7/asp.net/gridview-overview-of-different-ways-to-bind-data-to-columns

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.