change datagrid headers via code

G

Guest

on my web app, i need to display data from a table in a grid. (i know how to
do that). The grid will be populated with data depending on what the user
selects in a drop down box. My question is instead of having 20 grids can i
use only 1 and change the data that should be displayed and the headers via
code, or do I need a seperate grid per choice in the drop down box.
 
S

Shiva

Hi,
You can access individual columns using DataGrid.Columns[<col index>]. For
eg, to change the header text of the first column,
DataGrid.Columns[0].HeaderText = "My Col1 Text".

Hope this is what you are looking for.

on my web app, i need to display data from a table in a grid. (i know how to
do that). The grid will be populated with data depending on what the user
selects in a drop down box. My question is instead of having 20 grids can i
use only 1 and change the data that should be displayed and the headers via
code, or do I need a seperate grid per choice in the drop down box.
 
G

Guest

it is but i get this error message when i try to change the first column
header.

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

heres my code i'm changing it in

dgStuff.Columns(0).HeaderText = "Cars"
 
S

Steve Willcock

Are you trying to set the header text before you have bound the data to the
grid?

There are no columns in the Columns collection until the databind occurs
unless you are setting the columns up at design time.

Steve

IGotYourDotNet said:
it is but i get this error message when i try to change the first column
header.

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

heres my code i'm changing it in

dgStuff.Columns(0).HeaderText = "Cars"


Shiva said:
Hi,
You can access individual columns using DataGrid.Columns[<col index>]. For
eg, to change the header text of the first column,
DataGrid.Columns[0].HeaderText = "My Col1 Text".

Hope this is what you are looking for.

on my web app, i need to display data from a table in a grid. (i know how to
do that). The grid will be populated with data depending on what the user
selects in a drop down box. My question is instead of having 20 grids can i
use only 1 and change the data that should be displayed and the headers via
code, or do I need a seperate grid per choice in the drop down box.
 
S

Shiva

As Steve suggested, try it after you did the databinding on the grid.

it is but i get this error message when i try to change the first column
header.

Index was out of range. Must be non-negative and less than the size of the
collection. Parameter name: index

heres my code i'm changing it in

dgStuff.Columns(0).HeaderText = "Cars"
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top