This is a migrated thread and some comments may be shown as answers.

Programmatic Adding Edit Column

12 Answers 145 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pete
Top achievements
Rank 1
Pete asked on 30 Nov 2011, 05:00 PM

I'm not sure where this is going wrong, but any help would be appreciated.

I'm trying to add a column to an edit column to a grid (I also want to add a delete column, but I haven't gotten that far yet). I can make the column display without a problem, but the moment I click the button I get very odd results - the button disappears, my column headers move across and one of the bits of content from another of my columns vanishes entirely. I found it rather funny at first, but then I couldn't find out why it was doing it.

My code is below.

Private Sub rgdTable_DetailTableDataBind(ByVal source As Object, ByVal e As GridDetailTableDataBindEventArgs) Handles rgdTable.DetailTableDataBind
    Dim dataItem As GridDataItem = CType(e.DetailTableView.ParentItem, GridDataItem)
    Select Case e.DetailTableView.Name
        Case "TUGDetailTable"
            Dim myTrackingGUID As Guid = New Guid(dataItem.Item("MerchantTrackingGUID").Text)
            Dim myDataSet As New DataSet
            '===Get the DetailTable data===
            myDataSet = *get data from database*
            myDataSet.Tables(0).Columns.Remove("Source")
            myDataSet.Tables(0).Columns.Remove("@Action")
            e.DetailTableView.DataSource = myDataSet
            e.DetailTableView.CommandItemDisplay = GridCommandItemDisplay.Top
            e.DetailTableView.CommandItemSettings.AddNewRecordImageUrl = "../Img/AddRecord.gif"
            e.DetailTableView.CommandItemSettings.AddNewRecordText = "Add New TUG Details"
            e.DetailTableView.CommandItemSettings.ShowRefreshButton = False
            e.DetailTableView.EditFormSettings.EditColumn.UniqueName = "EditCommandColumn1"
            e.DetailTableView.EditFormSettings.UserControlName = "../UserControls/Templates/TUGAdd.ascx"
            e.DetailTableView.EditFormSettings.EditFormType = GridEditFormType.WebUserControl
            Dim editCol As GridEditCommandColumn
            editCol = New GridEditCommandColumn
            editCol.UniqueName = "EditColumn"
            editCol.HeaderText = ""
            editCol.HeaderStyle.Width = 20
            editCol.EditImageUrl = "~\img\edit.gif"
            editCol.ButtonType = GridButtonColumnType.ImageButton
            editCol.EditFormColumnIndex = 0
            e.DetailTableView.Columns.Add(editCol)
    End Select
End Sub

If anyone can tell me what's going wrong I'd appreciate it.

12 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 01 Dec 2011, 05:20 AM
Hello Pete,

Check the following help documentation which explains more about this.
Programmatic Creation

-Shinu.
0
Pete
Top achievements
Rank 1
answered on 01 Dec 2011, 12:36 PM
Hi,

I have been using that documentation, it's how I managed to create the column in the first place. The documentation however only tells me how to create columns in the first place, and I have created the column. It displays on the page without a problem. The image I want it to display with works. The width works. The name works. The problem is that the moment I click the button the column disappears and I don't know why. This is only the smallest of problems I'm having with edit columns at the moment.

If I try adding an edit column to the master table then it tells me every time I click any control that I can't have duplicate edit controls with matching ID's, presumably because it's trying to add the same column again and I can figure that out either. There doesn't seem to be any tutorial for adding these columns. The tutorial you linked to only creates data columns, and I have all my data columns working as I want them to. I can add more or remove them whenever I wish and change the appearance of them. I just can't add any functionality to the grid aside from inserting new records using the CommandItemSettings, which isn't very useful when I need to edit/delete existing data as well.
0
Tsvetina
Telerik team
answered on 05 Dec 2011, 05:44 PM
Hi Pete,

One of the most important things that the topic Shinu linked to elaborates on, is that you can only add columns inside the Page_Load or Page_Init event handler.
If you add them to a grid declared in mark-up, you should do this on initial Page_Load only.
If you create columns on Page_Init, the whole grid structure should be declared in code-behind.

In your code you add the edit column on DetailTableDataBind event which is not supported - one reason is that it does not fire on each postback, the other is that it happens too late in the grid lifecycle.

My advice is to move all the CommandItem and GridEditCommandColumn logic to the Page_Load or Page_Init event handler, depending on the way you create the rest of the columns and the grid itself.

Let us know how this goes for you after applying these changes.

Best wishes,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Pete
Top achievements
Rank 1
answered on 05 Dec 2011, 06:19 PM
Hi,

Thanks for the response.

The reason that I was trying to create them there is that the grid isn't created on Page_Load. It's contained within a User Control and imported in to a panel so as only to be displayed when needed and I've written a procedure to create the grid when a button is clicked to display the contents of the panel (which include a title and a button to save the panel and update the database to say you've completed that section of a form). If I were to create every grid on Page_Load or Page_Init my page would have upwards of 30 grids on it at one time, which would just be insanely confusing to any user.

The reason it's being done this way is to create a more streamline version of an order wizard, which currently consists of various separate pages and it is a requirement that they all be contained within the same page and only displays when required.

Based upon this would you be able to recommend an alternative way of handling the creation of the grid?
0
Tsvetina
Telerik team
answered on 07 Dec 2011, 05:05 PM
Hi Pete,

Why not create the grid in the Page_Load/Init event of the UserControl where you load it and if it is not needed at all times, just set Visible=false to it upon creation. This way it will not try to bind until you set Visible=true to it and call RadGridInstance.Rebind() (more info here). Or am I missing any specific out?

Also, you can consider having autogenerated columns for the grid, so that you do not need to create any columns manually, you just need to pass the control a datasource when you want to display it.

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Pete
Top achievements
Rank 1
answered on 07 Dec 2011, 05:10 PM
Hi Tsvetina, thanks for the reply.

I was actually thinking of trying the page_init of the user control this afternoon, but I've been bombarded with various other tasks and not gotten a chance to try it yet.

Nothing would please me more however than to use autogenerated columns considering I have absolute control over the dataset that I'd be binding to it and I can so easily set the columns in the dataset to match exactly what I want displayed in the radgrid. However doing that doesn't allow me to have an edit and delete column so I don't have any functionality for editing or deleting the data. Is there a way to get these automatically as well, rather than creating them manually? I've looked through several tutorials on the site and have pretty much mastered everything EXCEPT this and it's slowly driving me insane.

If you could help that would be amazing.

Thanks,

-Pete
0
Tsvetina
Telerik team
answered on 07 Dec 2011, 05:31 PM
Hi Pete,

RadGrid has exactly the properties that you need, just set these properties inside the RadGrid tag:

    AutoGenerateDeleteColumn="true"  and
    AutoGenerateEditColumn="true"

and you should get the required functionality.

Greetings,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Pete
Top achievements
Rank 1
answered on 07 Dec 2011, 05:39 PM
Hi Tsvetina,

You are quickly becoming my favourite person on the Internet, I can't believe I didn't see this in any of the tutorials.

I have one more query for you though. Is there a way to edit the appearance and column index? I have no problem doing this in the rgdTest.ColumnCreated method if I need to, assuming I can find out what the column names are so I can mod them, but if there's an easier way (or you could provide the column names) I'd be extremely grateful.

Thanks,

-Pete
0
Tsvetina
Telerik team
answered on 07 Dec 2011, 05:57 PM
Hi Pete,

It is great to hear that our suggestions help you.

The column names are the same as the DataField names when the columns are being auto-generated, so you will have the same column titles as the ones in the DataSet you bind the grid to. You can use these in PreRender and follow the guidelines for reordering from this article:
Reordering Columns
You can also modify the appearance there.Keep in mind that the auto-generated columns are available through :
RadGridInstance.MasterTableView.AutoGeneratedColumns

I hope this helps.

Best wishes,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Pete
Top achievements
Rank 1
answered on 07 Dec 2011, 06:10 PM
Hi Tsvetina,

It was actually the autogenerated columns that I meant I wanted to change the appearance of, sorry if that wasn't clear. I just wanted to change the text, or possibly change the text to a button instead. I know you can do that in other columns using "ButtonType = GridButtonColumnType.ImageButton", but obviously I don't know if this is possible with autogenerated columns.

If it's not possible then it's not possible, I just wondered if it was because we have an "edit pencil" image that's used elsewhere that would be useful to use in the grid as well.

Thanks,

-Pete
0
Accepted
Tsvetina
Telerik team
answered on 07 Dec 2011, 06:29 PM
Hello Pete,

If you mean only the delete and the edit columns, they are named:
"AutoGeneratedDeleteColumn"
"AutoGeneratedEditColumn"

Kind regards,
Tsvetina
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
0
Pete
Top achievements
Rank 1
answered on 09 Dec 2011, 06:14 PM
Tsvetina,

Thanks so much for all your help the other day. I was able to do exactly what I wanted to this morning now and have the control working in exactly the way I wanted.

Thanks again.
Tags
Grid
Asked by
Pete
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Pete
Top achievements
Rank 1
Tsvetina
Telerik team
Share this question
or