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

Programmatically setting itemcommand event

1 Answer 221 Views
Grid
This is a migrated thread and some comments may be shown as answers.
James
Top achievements
Rank 1
James asked on 13 Dec 2011, 07:00 PM
I'm fairly new to working with the telerik controls so bare with me,

I have a grid that i have been trying to set up in the code behind. So far everything has been going smoothly and working, but when
i go to set the itemcommand event or any event for the matter, when i go to click on the command or do something that should cause to trigger the event, nothing ends up firing. So i was wondering what exactly i am doing wrong with my declaration on my item command.
You will find my code below:

private void createRadGrid()
        {
            //create radgrid
            RadGrid rg = new RadGrid();
            rg.ID = "RadGridView";
 
            //setting the datasource and itemcommand event handler.
            rg.DataSourceID = "MachineDataSet";
            rg.ItemCommand += new GridCommandEventHandler(RadGridView_ItemCommand);
 
            rg.Width = 862;
            rg.CellSpacing = 2;
            rg.CellPadding = 4;
            rg.BorderWidth = 3;
            rg.BackColor = System.Drawing.Color.Transparent;
            rg.BorderColor = System.Drawing.Color.DarkGray;
            rg.ForeColor = System.Drawing.Color.Black;
            rg.ItemStyle.HorizontalAlign = HorizontalAlign.Center;
            rg.HeaderStyle.HorizontalAlign = HorizontalAlign.Center;
            rg.BorderStyle = BorderStyle.Ridge;
            rg.ShowStatusBar = true;
 
            rg.AllowPaging = true;
            rg.PageSize = 5;
            rg.PagerStyle.Mode = GridPagerMode.NextPrevAndNumeric;
            rg.AutoGenerateColumns = false;
 
            rg.MasterTableView.PageSize = 5;
            rg.MasterTableView.DataKeyNames = new string[] { "ID" };
            rg.MasterTableView.ClientDataKeyNames = new string[] { "ID" };
            rg.MasterTableView.AutoGenerateColumns = false;
 
            rg.ClientSettings.Resizing.AllowColumnResize = true;
            rg.ClientSettings.Resizing.EnableRealTimeResize = true;
            rg.ClientSettings.Resizing.ResizeGridOnColumnResize = true;
 
            GridBoundColumn boundColumn = new GridBoundColumn();
            boundColumn.DataField = "ID";
            boundColumn.HeaderText = "ID";
            boundColumn.UniqueName = "MachineID";
            boundColumn.Visible = false;
            rg.MasterTableView.Columns.Add(boundColumn);
 
            GridBoundColumn boundColumn1 = new GridBoundColumn();
            boundColumn1.DataField = "SiteName";
            boundColumn1.HeaderText ="Site Name";
            boundColumn1.Resizable = true;
            boundColumn1.ReadOnly = true;
            rg.MasterTableView.Columns.Add(boundColumn1);
 
            GridBoundColumn boundColumn2 = new GridBoundColumn();
            boundColumn2.DataField = "Name";
            boundColumn2.HeaderText = "Machine Name";
            boundColumn2.Resizable = true;
            boundColumn2.ReadOnly = true;
            rg.MasterTableView.Columns.Add(boundColumn2);
 
            GridBoundColumn boundColumn3 = new GridBoundColumn();
            boundColumn3.DataField = "MachineType";
            boundColumn3.HeaderText = "Machine Type";
            boundColumn3.Resizable = true;
            boundColumn3.ReadOnly = true;
            rg.MasterTableView.Columns.Add(boundColumn3);
 
            GridBoundColumn boundColumn4 = new GridBoundColumn();
            boundColumn4.DataField = "MachineModel";
            boundColumn4.HeaderText = "Machine Model";
            boundColumn4.Resizable = true;
            boundColumn4.ReadOnly = true;
            rg.MasterTableView.Columns.Add(boundColumn4);
 
            GridButtonColumn buttonColumn = new GridButtonColumn();
            buttonColumn.ButtonType = GridButtonColumnType.PushButton;
            buttonColumn.CommandName = "AssignNewValues";
            buttonColumn.Resizable = true;
            buttonColumn.Text = "Assign New Values";
            rg.MasterTableView.Columns.Add(buttonColumn);
 
            PlaceHolder_RadGridView.Controls.Add(rg);
        }

Any help or suggestions are greatly appreciated,

Thank you

1 Answer, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 14 Dec 2011, 05:00 AM
Hello James,

When creating a grid in the Page_Init event handler, grid columns should be added to the Columns collection of the MasterTableView after their attributes are set. Check the following help documentation which explains the same.
Programmatic Creation

-Shinu.
Tags
Grid
Asked by
James
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Share this question
or