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

how can we add GridTemplateColumn with asp:ImageButton inside dynamically in codebehind in c#

5 Answers 237 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Sara
Top achievements
Rank 1
Sara asked on 04 Jan 2017, 05:13 PM
<telerik:GridTemplateColumn DataField="Res6"   Groupable="False" HeaderText="Res6" SortExpression="Res6"  UniqueName="Res6_RadGridMP"  display="false">
                        <HeaderStyle Width="50px" />
                        <ItemTemplate >
                            <asp:ImageButton runat="server" ID="btnres6"   />
                            <asp:HiddenField ID="hdres6" runat="server" />
                        </ItemTemplate>
                        <HeaderStyle HorizontalAlign="Center" />
                        <ItemStyle HorizontalAlign="Center" />
                    </telerik:GridTemplateColumn>

5 Answers, 1 is accepted

Sort by
0
Viktor Tachev
Telerik team
answered on 09 Jan 2017, 12:43 PM
Hi Sara,

If you would like to create a GridTemplateColumn programmatically you should create the entire Grid control in the code-behind. Please examine the example below that describes the approach in more detail.



Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
0
Sara
Top achievements
Rank 1
answered on 18 Jan 2017, 02:43 PM

Hi Viktor.



Thank you for replying back. i have created the grid succesfully but im having some issues with it. I have added GridClientSelectColumn to select a row or all rows. When i select s single row its working fine but when i try to select all rows from header checkbox, it gives me a script error

Unhandled exception at line 18, column 1 in script block
0x800a138f - JavaScript runtime error: Unable to get property '_selectAllRows' of undefined or null reference



can you please help? Also i need to catch the checkbox check/uncheck triggering event? how can i define that?

regards,

Sara

 

public void GetResultsByRequester()
        {

            dtResultTypes = new DataTable();
            dtResultTypes = _clsActivity.GetResultTypes(); 

            DataTable dt = new DataTable();
            dt = _clsActivity.GetResultsByRequester();

            grid = new RadGrid();
            grid.AllowPaging = true;
            grid.AutoGenerateEditColumn = true;
            grid.AutoGenerateColumns = false;
            grid.PageSize = 20;
            grid.EnableEmbeddedSkins = true;
            grid.AllowSorting = false;
            grid.AllowFilteringByColumn = false;
            grid.RenderMode = Telerik.Web.UI.RenderMode.Lightweight;

            grid.AllowMultiRowSelection = true;
            grid.ClientSettings.Selecting.AllowRowSelect = true;
            grid.ClientSettings.Selecting.EnableDragToSelectRows = true;
            grid.ItemDataBound += new GridItemEventHandler(grid_onItemDataBound);
            grid.DataSource = dt;

            GridClientSelectColumn objGridClientSelectColumn = new GridClientSelectColumn();
            objGridClientSelectColumn.UniqueName = "chkDeleteMP11";
            grid.MasterTableView.Columns.Add(objGridClientSelectColumn);

            GridTemplateColumn templateColumn = new GridTemplateColumn();
            templateColumn.HeaderText = "";
            templateColumn.UniqueName = "tempcolRemove";
            templateColumn.ItemTemplate = new MyTemplate("imgbtnRemove", "imgCross.gif");
            grid.MasterTableView.Columns.Add(templateColumn);

            GridBoundColumn boundColumn1 = new GridBoundColumn();
            boundColumn1.DataField = "PatientNo";
            boundColumn1.UniqueName = "PatientNo";
            boundColumn1.HeaderText = "PatientNo";
            boundColumn1.SortExpression = "PatientNo";

            GridBoundColumn boundColumn2 = new GridBoundColumn();
            boundColumn2.DataField = "Sname";
            boundColumn2.UniqueName = "Sname";
            boundColumn2.HeaderText = "Surname";
            boundColumn2.SortExpression = "Sname";

            GridBoundColumn boundColumn3 = new GridBoundColumn();
            boundColumn3.DataField = "Fname1";
            boundColumn3.UniqueName = "Fname1";
            boundColumn3.HeaderText = "Forename";
            boundColumn3.SortExpression = "Fname1";

            GridBoundColumn boundColumn4 = new GridBoundColumn();
            boundColumn4.DataField = "Clinic";
            boundColumn4.UniqueName = "Clinic";
            boundColumn4.HeaderText = "Clinic";
            boundColumn4.SortExpression = "Clinic";

            GridBoundColumn boundColumn5 = new GridBoundColumn();
            boundColumn5.DataField = "Consultantid";
            boundColumn5.UniqueName = "Consultantid";
            boundColumn5.HeaderText = "Consultantid";
            boundColumn5.SortExpression = "Consultant";

            GridBoundColumn boundColumn6 = new GridBoundColumn();
            boundColumn6.DataField = "SeenBy";
            boundColumn6.UniqueName = "SeenBy";
            boundColumn6.HeaderText = "SeenBy";
            boundColumn6.SortExpression = "SeenBy";

            GridBoundColumn boundColumn7 = new GridBoundColumn();
            boundColumn7.DataField = "TDate";
            boundColumn7.UniqueName = "TDate";
            boundColumn7.HeaderText = "Tracking Started";
            boundColumn7.SortExpression = "TDate";
    
            grid.MasterTableView.Columns.Add(boundColumn1);
            grid.MasterTableView.Columns.Add(boundColumn2);
            grid.MasterTableView.Columns.Add(boundColumn3);
            grid.MasterTableView.Columns.Add(boundColumn4);
            grid.MasterTableView.Columns.Add(boundColumn5);
            grid.MasterTableView.Columns.Add(boundColumn6);
            grid.MasterTableView.Columns.Add(boundColumn7);
           

            for (int i = 0; i < dtResultTypes.Rows.Count; i++)
            {
                GridTemplateColumn templateColumn1 = new GridTemplateColumn();
                templateColumn1.HeaderText = (String)dtResultTypes.Rows[i][0];
                templateColumn1.DataField = (String)dtResultTypes.Rows[i][0];
                templateColumn1.UniqueName = (String)dtResultTypes.Rows[i][0];
                templateColumn1.SortExpression = (String)dtResultTypes.Rows[i][0];
                templateColumn1.ItemTemplate = new MyTemplate(dtResultTypes.Rows[i][0].ToString(), string.Empty);
                grid.MasterTableView.Columns.Add(templateColumn1);
            }
         
            grid.DataBind();

            grid.AllowPaging = true;
            grid.PageSize = 10;

            phAllmyActivity.Controls.Add(grid);
          
        }

 

 public class MyTemplate : ITemplate
    {
        ImageButton imageButton;
        string imgbtnID = string.Empty;
        string imageUrl = string.Empty;

        public MyTemplate(string btnID, string imageurl)
        {
            imgbtnID = btnID;
            imageUrl = imageurl;
          
        }
        public void InstantiateIn(Control container)
        {
            if (!String.IsNullOrEmpty(imgbtnID))
            {
                imageButton = new ImageButton();
                imageButton.ID = imgbtnID;

                if (!String.IsNullOrEmpty(imageUrl))
                {
                    imageButton.ImageUrl = "~/Images/" + imageUrl;
                    imageButton.ToolTip = "Cease tracking";
                }

                container.Controls.Add(imageButton);
            }
        
        }


    }

0
Sara
Top achievements
Rank 1
answered on 18 Jan 2017, 04:07 PM

I have added a delete button which will iterate through grid to collect all checked rows and delete them. But im getting a grid null here. is it something with the dynamically created grid. how can i retain the values? 

 

protected void btnDelete_Click(object sender, EventArgs e)
        {
            foreach (GridDataItem item in grid.MasterTableView.Items)
            {
                CheckBox chk = (CheckBox)item.FindControl("chkDeleteMP11");
                if (chk.Checked == true)
                {
                    string ID = item.GetDataKeyValue("PatientNo").ToString();
                }
            }
        }

0
Sara
Top achievements
Rank 1
answered on 19 Jan 2017, 12:05 PM

Im getting grid as null here. whats wrong im doing? 

private void grid_SelectedIndexChanged(object sender, EventArgs e)

        {
            GridHeaderItem headerItem = (GridHeaderItem)grid.MasterTableView.GetItems(GridItemType.Header)[0];
            CheckBox headerChkBox = (CheckBox)headerItem["GridClientSelectColumn"].Controls[0];
            if (headerChkBox.Checked)
            {
                // header CheckBox is clicked
            }
            else
            {
                // check box inside Grid row is clicked
            }

        }

0
Viktor Tachev
Telerik team
answered on 23 Jan 2017, 11:32 AM
Hi Sara,

I have examined the provided code and noticed that you are using DataBind to provide data for RadGrid. Note that this method is used only for simple data binding. As the name implies this binding is used only in the most simple scenarios where features like paging, sorting, editing, etc. will not be used for the Grid.

For your scenario it is recommended to use advanced data binding. You can handle the NeedDataSource event for RadGrid and pass the data there. Check out the article below that illustrates the approach in more detail:


If you would like to add additional logic to the CheckBoxes that are used for selection you may consider using a TemplateColumn and placing the CheckBox components with the respective settings there.

To delete the selected items you can use a button that will fire a custom command. Then you can handle the ItemCommad event for the grid and remove the selected items.


Regards,
Viktor Tachev
Telerik by Progress
Try our brand new, jQuery-free Angular 2 components built from ground-up which deliver the business app essential building blocks - a grid component, data visualization (charts) and form elements.
Tags
Grid
Asked by
Sara
Top achievements
Rank 1
Answers by
Viktor Tachev
Telerik team
Sara
Top achievements
Rank 1
Share this question
or