change datagrid headers via code

  • Thread starter Thread starter Guest
  • Start date Start date
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.
 
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.
 
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"
 
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.
 
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"
 
Back
Top