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

GridButtonColumn does not fire in programatically created RadGrid

1 Answer 75 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Richard
Top achievements
Rank 1
Veteran
Richard asked on 24 Nov 2018, 05:03 AM

I have a need to programmatically create a RadGrid based on the number of elements from another table.  So I have the following:

foreach (DataRow customerRow in dtDistinctMonitors.Rows)
            {
                //Get Data
                DataRow[] rowData = dtMonitors.Select("tenantID = '" + tenantID + "'");

                //Add Label
                Label lblName = new Label();
                lblName.Text = "Channel: xxxx";
                lblName.Font.Size = FontUnit.Large;
                PlaceHolder1.Controls.Add(lblName);

                //Add RadGrid
                RadGrid RadGrid1 = new RadGrid();
                RadGrid1.ID = customerRow["channelID"].ToString();

                //Add RadGrid to the Controls collection of the placeholder
                PlaceHolder1.Controls.Add(RadGrid1);

                RadGrid1.DataSource = rowData;
                RadGrid1.MasterTableView.DataKeyNames = new string[] { "monitorID" };
                RadGrid1.AllowPaging = false;
                RadGrid1.MasterTableView.AutoGenerateColumns = false;
                RadGrid1.Skin = "Silk";
                RadGrid1.RenderMode = RenderMode.Lightweight;
                RadGrid1.Width = Unit.Percentage(100);
                GridBoundColumn boundColumn;
                boundColumn = new GridBoundColumn();
                RadGrid1.MasterTableView.Columns.Add(boundColumn);
                boundColumn.DataField = "monitorName";
                boundColumn.HeaderText = "Monitor Name";
                GridButtonColumn buttonColumn;
                buttonColumn = new GridButtonColumn();
                buttonColumn.Text = "Delete";
                buttonColumn.ButtonType = GridButtonColumnType.LinkButton;
                buttonColumn.HeaderStyle.Width = Unit.Pixel(100);
                buttonColumn.CommandArgument = "monitorID";
                buttonColumn.CommandName = "lnkDelete_Command";
                RadGrid1.MasterTableView.Columns.Add(buttonColumn);
            }

And below I have lnkDelete_Command as:

public void lnkDelete_Command(object sender, CommandEventArgs e)
    {
        using (SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["stringvalue"].ConnectionString))
        {
            string ID = e.CommandArgument.ToString();

            //DELETE FROM WHERE ID = ID
        }
    }

 

Yet, when I click on the LinkButton created it doesn't fire.  It doesn't really do anything at all but creates a second instance of the button.  Am I missing something simple here?

1 Answer, 1 is accepted

Sort by
0
Marin Bratanov
Telerik team
answered on 26 Nov 2018, 10:46 AM
Hello Richard,

If you will be creating the grid programmatically, I advise that you review the following articles. The key points to look for in them are:

  • which events you must create the grid in (generally, using Page_Init is the best option, and Page_Load is the latest option)
  • ensure the grid is re-created after each postback
  • the order of setting the column properties and adding the columns to the grid
  • how to add event handlers dynamically (usually handing the ItemCommand event will be easiest as it is likely you will be using it for other tasks and not just the custom command)
  • that you should add the grid to the page at the end of its settings creation

That said, here are the links:

 


Regards,
Marin Bratanov
Progress Telerik
Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
Grid
Asked by
Richard
Top achievements
Rank 1
Veteran
Answers by
Marin Bratanov
Telerik team
Share this question
or