datagrid column exists

G

gane

Hi,

I am creating datagrid bound column dynamically and need to check if a
datagrid column already exists?Is there a way to check this?

thanks
gane
 
E

Eliyahu Goldin

Is datagrid's Columns collection good for you? Note, that autogenerated
columns don't get there.

Eliyahu
 
G

gane

Hi,

Can you show me with a sample code?
I have something like this, i need to check if the column exists before i
add to the grid. how can i do with the collections.
foreach(DataColumn c in dsinfo.Tables[0].Columns)

{

dgresults.Columns.Add(CreateBoundColumn(c));

}

Thanks
 
B

Bob Barrows [MVP]

gane said:
Hi,

I am creating datagrid bound column dynamically and need to check if a
datagrid column already exists?Is there a way to check this?
Dynamically created columns do not persist between page loads so I'm not
sure why you need to check.You should only dynamically create and add
columns to a datagrid in the Page_Init event. From online help:

The DataGrid control does not store the contents of its Columns
collection into the view state. To add or remove a column dynamically,
you must programmatically add or remove the column everytime the page is
refreshed. Provide a Page_Init function that adds or removes the column
before the DataGrid control's state is reload and the control is
rebuilt. Otherwise, the changes to the Columns collection are not
reflected in the DataGrid control when it is displayed.
Note Although you can programmatically add columns to or remove
columns from the Columns collection of the DataGrid control, it is
easier to list the columns statically and then use the Visible property
to display or hide each column.
 
G

gane

I am using sort functionality on the datagrid, which is not firing if i dont
have the datagrid binded in pageload.
so i need to rebind twice in pageload and onsortcommand, which creates the
dynamic bound columns twice.
But now i clearing the grid, instead of finding a way to check for existing
columns. Now it works.

Thanks
Gane
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top