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

Dynamic gridbuttoncolumn in dynamic radgrid event not firing

3 Answers 166 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Steven Webster
Top achievements
Rank 1
Steven Webster asked on 30 Sep 2010, 04:23 PM
Hi there

Could someone advise me how to create a gridbuttoncolumn to delete records in a dynamic radgrid that's generated on the fly. I'm using the code in this link as a reference - http://www.telerik.com/help/aspnet-ajax/grdchanginggridstructuredynamically.html, but can't get the command to fire.

I've added a hander for the item_command:

 

AddHandler grid.ItemCommand, AddressOf grid_ItemCommand


but I never get that far when the code runs.

Thanks in advance!!

Steven

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 01 Oct 2010, 05:53 AM
Hello Steven,

The following code snippet shows how to add GridButtonColumn dynamically. You can also attach ItemCommand event to fire the Delete command.

C#:
protected void Page_Init(object sender, EventArgs e)
   {
       RadGrid grid = new RadGrid();
       grid.ID = "RadGrid1";
       //creating GridButtonColumn
       GridButtonColumn btncol = new GridButtonColumn();
       grid.MasterTableView.Columns.Add(btncol);
       btncol.ButtonType = GridButtonColumnType.LinkButton; // setting ButtonType as LinkButton
       btncol.CommandName = "Delete";
       btncol.Text = "delete";
       //attaching ItemCommand event
       grid.ItemCommand+=new GridCommandEventHandler(grid_ItemCommand);
       PlaceHolder1.Controls.Add(grid);
   }
 protected void grid_ItemCommand(object source, GridCommandEventArgs e)
   {
       if (e.CommandName == "Delete")
       {
 
       }
   }

And please go through the following documentation.
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html#Creating_the_grid_entirely_in_the_code_behind

Thanks,
Princy.
0
Steven Webster
Top achievements
Rank 1
answered on 01 Oct 2010, 04:09 PM
Thanks Princy - much appreciated. I think I was adding the AddHandler in the wrong place. Anyways, bringing this a step further, based on the selection made in the dropdown list, a query is run that will return a resultset made up of multiple tables (with different columns in each). The code below works well to display the multiple dynamic radgrids and the delete command works, however, I get a "Failed to load viewstate" error intermittently when changing the value of the dropdown. I know it's because there are a different number of gridviews displayed with different id's depending on the option selected so I can't figure out which ones to rebind. Would you have any suggestions please?

Protected Sub Page_Init(ByVal sender As Object, _
  ByVal e As System.EventArgs) Handles Me.Init
    PopulateGridOnPageInit()
End Sub
Protected Sub PopulateGridOnPageInit()
    Dim literal1 As New Literal
    literal1.Text = "<br/><br/>"
    Dim ddlValue As String = Request.Form.Get("DropDownList1")
    Dim conn As New SqlConnection(ConfigurationManager.ConnectionStrings("StagingConnectionString").ConnectionString)
    Dim cmd As New SqlCommand()
    cmd.Connection = conn
    cmd.CommandType = CommandType.StoredProcedure
    cmd.CommandText = "spLookupChecksNew"
    If ddlValue Is Nothing Then
        ddlValue = DropDownList1.SelectedValue
    End If
    cmd.Parameters.Add(New SqlParameter("@Lab", SqlDbType.Int))
    'Assign the search value to the parameter.
    cmd.Parameters("@Lab").Value = ddlValue
    Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
    Dim ds As New DataSet()
    ds.Clear()
    ds.Tables.Clear()
    da.Fill(ds)
    Dim i As Integer = 0
    For Each DataTable In ds.Tables
        Dim grid As RadGrid = New RadGrid
        grid.ID = ddlValue.ToString + "grid" + i.ToString
        grid.Width = Unit.Pixel(500)
        grid.AllowSorting = True
        grid.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric
        grid.AllowPaging = True
        grid.Skin = "Sunset"
        grid.DataSource = ds.Tables(i)
        grid.MasterTableView.AutoGenerateColumns = False
        grid.MasterTableView.EnableColumnsViewState = False
        grid.MasterTableView.EnableViewState = False
        Dim boundColumn As GridBoundColumn
        Dim DeleteColumn As GridButtonColumn
        If Not (ddlValue Is Nothing) Then
            For Each DataColumn In ds.Tables(i).Columns
                boundColumn = New GridBoundColumn
                boundColumn.HeaderText = DataColumn.ToString
                boundColumn.DataField = DataColumn.ToString
                boundColumn.UniqueName = DataColumn.ToString
                grid.MasterTableView.Columns.Add(boundColumn)
            Next
            Dim btncol As GridButtonColumn = New GridButtonColumn
            grid.MasterTableView.Columns.Add(btncol)
            btncol.ButtonType = GridButtonColumnType.LinkButton
            ' setting ButtonType as LinkButton
            btncol.CommandName = "Delete"
            btncol.Text = "delete"
            'attaching ItemCommand event
            AddHandler grid.ItemCommand, AddressOf grid_ItemCommand
            Select Case ddlValue
                Case "0"
                    grid.DataSourceID = ""
                    grid.DataSource = New Object() {}
            End Select
            If ds.Tables(i).Rows.Count > 0 Then
                PlaceHolder1.Controls.Add(grid)
            End If
            PlaceHolder1.Controls.Add(literal1)
        Else
            grid.DataSourceID = ""
            grid.DataSource = New Object() {}
        End If
        i = i + 1
    Next
End Sub
Protected Sub DropDownList1_SelectedIndexChanged( _
               ByVal sender As Object, _
               ByVal e As EventArgs) _
         Handles DropDownList1.SelectedIndexChanged
    For Each childControl As Control In PlaceHolder1.Controls
        If TypeOf childControl Is RadGrid Then
            Dim grid As RadGrid = CType(childControl, RadGrid)
            grid.Rebind()
        End If
    Next
    'Dim grid As RadGrid = CType(PlaceHolder1.FindControl("grid"), RadGrid)
    'grid.Rebind()
End Sub
Protected Sub grid_ItemCommand(ByVal source As Object, ByVal e As GridCommandEventArgs)
    If e.CommandName = "Delete" Then
        ' Put delete commands in here ...
    End If
End Sub
0
Iana Tsolova
Telerik team
answered on 07 Oct 2010, 09:27 AM
Hello Steven,

I suggest that you try binding the grid through its NeedDataSource event instead of just setting its DataSource on Page_Init and see if it makes any difference.

All the best,
Iana
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Tags
Grid
Asked by
Steven Webster
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Steven Webster
Top achievements
Rank 1
Iana Tsolova
Telerik team
Share this question
or