0

First question how to add custom column to aspxgridview column using looping to get name and value of column.

here is my code behind :

Protected Sub gridSubmission_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridSubmission.Init
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
    For i As Integer = 1 To 6
        colBaru.Caption = i
        colBaru.FieldName = i
        colBaru.UnboundType = DevExpress.Data.UnboundColumnType.Integer
        colBaru.VisibleIndex = gridSubmission.VisibleColumns.Count
        colBaru.PropertiesTextEdit.DisplayFormatString = "c2"
        gridSubmission.Columns.Add(colBaru)
    Next
end sub

which i used to add new column at aspxgridview. but its getting an error, "An item with the same key has already been added."

second question, what property which i must use to put that code? I wanted load that code after page.load ! if i put in aspxgridview.init its was loaded before page.load.

and the output it must like this :

1           2          3          4           5           6
row         row        row        row         row         row
row         row        row        row         row         row
row         row        row        row         row         row
row         row        row        row         row         row
row         row        row        row         row         row

help pelase...

thanks before

Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
tyo
  • 67
  • 1
  • 2
  • 13

1 Answers1

1

As far first question goes, you are trying to add same column object again and again into columns, hence you get that error. Try creating column object in the loop - for example:

Protected Sub gridSubmission_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles gridSubmission.Init

    For i As Integer = 1 To 6
        Dim colBaru As GridViewDataTextColumn = New GridViewDataTextColumn()
        colBaru.Caption = i
        colBaru.FieldName = i
        ... 

As far as second question goes, I am not sure what you are asking here. When you mean before/after page.load, do you mean page_load at server side or page load at client (browser) side? Frankly speaking, grid_init or page_init are the best places to add columns. If you want to add columns in certain event on the page then the best way to do is set some hidden variable on the browser side and use that variable to decide whether to do such initialization or not.

VinayC
  • 47,395
  • 5
  • 59
  • 72
  • thanks a lot. its work. for the second question i had solution my self, i put in new sub and call it in every changes of the page. after i see the documentation, property init always load before page.load(i used in server side). – tyo Apr 13 '12 at 06:00