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

[Solved] Unable to create columns in rad grid programatically

5 Answers 261 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pinkey
Top achievements
Rank 1
Pinkey asked on 11 Feb 2010, 01:35 PM

Hi, 
      I am creating dynamic columns in the Radgrid. The first olumn is checkbox box column and the other columns are as per to the user selected criteria. There can be a link column also which will open new page. 
I get the datatable according to the user search criteria, hence I am getting different types of columns every time.
Following code shows the aspx page for radgrid display.

 

<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">  
                                     <ContentTemplate>  
                                         <telerik:RadGrid ID="rgResult" runat="server" BorderColor="Black" BorderStyle="Solid" 
                                             BorderWidth="1px" AllowPaging="true" AllowSorting="true"  AutoGenerateColumns="false" PageSize="8">  
                                             <MasterTableView>  
                                                                                             <PagerStyle Mode="NextPrev" Font-Size="11px" Font-Names="Arial" PagerTextFormat="Change page: {4} Item {3} of {5}" />  
                                                 <ItemStyle Font-Size="11.5px" HorizontalAlign="Left" />  
                                                 <AlternatingItemStyle Font-Size="11.5px" HorizontalAlign="Left" />  
                                                 <RowIndicatorColumn>  
                                                     <HeaderStyle Width="20px" />  
                                                 </RowIndicatorColumn>  
                                                 <ExpandCollapseColumn ItemStyle-Wrap="true">  
                                                     <HeaderStyle Width="20px" />  
                                                 </ExpandCollapseColumn>  
                                             </MasterTableView>  
                                         </telerik:RadGrid>  
                                     </ContentTemplate>  
                                 </asp:UpdatePanel> 

And the bind function I am calling is given

Public Sub BindNewGrid()  
        Try 
            Dim attachFlag As Boolean = False 
            Dim dt As New DataTable  
            Dim dtLoad As New DataTable  
            Dim lstHeader As New ListBox  
            Dim ShowMe As String = CType(Session("ShowMe"), String)  
            Dim chkflag As Boolean = False 
            'gets the data from session variable   
            dt = CType(Session("dtSearch"), DataTable).Copy  
 
            'checks is Display Columns are selected  
            lstHeader = GetDisplayColList()  
 
            rgResult.MasterTableView.DataSource = dt  
 
             For Each item As ListItem In lstHeader.Items  
                For i As Integer = 0 To dt.Columns.Count - 1  
                    Dim ProID As String = GetDatatableID()  
                    If Not chkflag Then 
                        If item.Value.ToLower = "keep" Then  'for checkbox column  
                            Dim templateColumn As New GridTemplateColumn()  
                            templateColumn.ItemTemplate = New GridCheckboxTemplate(ProID)  
                            ' templateColumn.HeaderImageUrl = "images/delete.gif"  
                            templateColumn.ItemStyle.HorizontalAlign = HorizontalAlign.Center  
                            templateColumn.HeaderStyle.HorizontalAlign = HorizontalAlign.Left  
                            templateColumn.ItemStyle.VerticalAlign = VerticalAlign.Middle  
                            templateColumn.HeaderStyle.Width = Unit.Pixel(23)  
                            templateColumn.ItemStyle.Width = Unit.Pixel(35)  
                            templateColumn.HeaderText = "Keep" 
                            rgResult.MasterTableView.Columns.Add(templateColumn)  
                            chkflag = True 
                            Exit For 
                        End If 
                    End If 
                     
                    If item.Value.ToLower = dt.Columns(i).ColumnName.ToLower Then 
                       If item.Value.ToLower = ProID.ToLower Then  'if ID Field linkbutton  
                            Dim Tempcol As New GridTemplateColumn  
                            Tempcol.ItemTemplate = New MyTemplate(dt.Columns(i).ColumnName, ProID, False, ShowMe)  
 
                            Tempcol.HeaderText = item.Text  'drow("DisplayName") 'GetDisplayName(dt.Columns(i).ColumnName)  
                            rgResult.MasterTableView.Columns.Add(Tempcol)  
 
                        ElseIf item.Value.ToLower = "totalattachments" Then 
                            'If CType(Session("Attachment"), Boolean) = True Then  
                            Dim Tempcol As New GridTemplateColumn  
 
                            'creates the template column for showing link   
                            Tempcol.ItemTemplate = New MyTemplate(dt.Columns(i).ColumnName, ProID, True, ShowMe)  
 
                            Tempcol.HeaderText = "Attachments" 'item.Text  'drow("DisplayName") 'GetDisplayName(dt.Columns(i).ColumnName)  
                            rgResult.MasterTableView.Columns.Add(Tempcol)  
                            'End If  
                        Else 
                            'removes all html tags from the data.  
                            For Each row As DataRow In dt.Rows  
                                row(i) = RemoveHTML(row(i).ToString)  
                                If row(i).ToString.Length > MaxLength Then 
                                    row(i) = row(i).ToString.Substring(0, MaxLength) & "..." 
                                End If 
                            Next 
 
                            'creates the column  
                            Dim column1 As New GridBoundColumn  
                            column1.DataField = dt.Columns(i).ColumnName  
                            column1.HeaderText = item.Text  
                            rgResult.MasterTableView.Columns.Add(column1)  
                        End If 
 
 
                        End If 
                Next 
            Next 
 
       Catch ex As Exception  
 
        End Try 
    End Sub 
 
 

The classes used for template column are

Public Class GridCheckboxTemplate  
    Implements ITemplate  
    Protected boolValue As CheckBox  
    Private colname As String 
    Private DatacolField As String 
 
    Public Sub New(ByVal cName As String)  
        colname = cName  
    End Sub 
    Public Sub InstantiateIn(ByVal container As System.Web.UI.Control) Implements ITemplate.InstantiateIn  
        boolValue = New CheckBox()  
        boolValue.ID = "CheckBox1" 
        boolValue.Enabled = True 
        boolValue.AutoPostBack = True 
        AddHandler boolValue.DataBinding, AddressOf boolValue_DataBinding  
        AddHandler boolValue.CheckedChanged, AddressOf CheckChanged  
        container.Controls.Add(boolValue)  
    End Sub 
    Sub boolValue_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
        Dim cBox As CheckBox = DirectCast(sender, CheckBox)  
        Dim container As GridDataItem = DirectCast(cBox.NamingContainer, GridDataItem)  
 
        cBox.ToolTip = (DirectCast(container.DataItem, DataRowView))(colname).ToString()  
         
    End Sub 
 
    Sub CheckChanged(ByVal sender As ObjectByVal e As System.EventArgs)  
      
   End Class 
Public Class MyTemplate  
    Inherits SearchResult  
    Implements ITemplate  
    Shared itemcount As Integer = 0  
    Private colname As String 
    Private ProjId As String 
    Private Trial_ID As String 
    Private PID As String = "" 
    Private ShowPage As String 
    Private ShowAttachment As Boolean 
    Protected lnkAttach As LinkButton  
    Protected lnkID As LinkButton  
 
 
        Public Sub New(ByVal cName As StringByVal IDField As StringByVal Attachment As BooleanByVal Show As String)  
        colname = cName  
        PID = IDField  
        ShowPage = Show  
        ShowAttachment = Attachment  
    End Sub 
 
      
       Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn  
        'defines the link button  
 
        If ShowAttachment Then 
            lnkAttach = New LinkButton  
            lnkAttach.ID = "lnkAttach" 
            lnkAttach.ForeColor = Drawing.Color.Blue  
            AddHandler lnkAttach.DataBinding, AddressOf lnkAttach_DataBinding  
            lnkAttach.OnClientClick = "if (this.innerText == 'No Attachments'){return false;}else{ ShowAttachmentDetails(this.title);return false;}" ', " + lnkAttach.CommandName + " 
            container.Controls.Add(lnkAttach)  
        Else 
            lnkID = New LinkButton  
            lnkID.ID = "lnkAttach" 
            lnkID.ForeColor = Drawing.Color.Blue  
            AddHandler lnkID.DataBinding, AddressOf lnkID_DataBinding  
                       lnkID.OnClientClick = "Redirectwin(this.innerText, '" & ShowPage.ToLower.Trim & "'); return false;" 
                  container.Controls.Add(lnkID)  
        End If 
    End Sub 
 
    Sub lnkID_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
        Dim link As LinkButton = DirectCast(sender, LinkButton)  
        Dim container As GridDataItem = DirectCast(link.NamingContainer, GridDataItem)  
        link.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()  
         
    End Sub 
 
 
     Sub lnkAttach_DataBinding(ByVal sender As ObjectByVal e As EventArgs)  
        Dim link As LinkButton = DirectCast(sender, LinkButton)  
        Dim container As GridDataItem = DirectCast(link.NamingContainer, GridDataItem)  
        link.Text = (DirectCast(container.DataItem, DataRowView))(colname).ToString()  
 
 
        GetDatatableID()  
 
        'checks if attachment is from project or trial  
        If Trial_ID = "0" Or Trial_ID Is Nothing Then 
            link.ToolTip = (DirectCast(container.DataItem, DataRowView))(ProjId).ToString() & "_" & "00" 
        Else 
            link.ToolTip = (DirectCast(container.DataItem, DataRowView))(ProjId).ToString() & "_" & (DirectCast(container.DataItem, DataRowView))(Trial_ID).ToString()  
        End If 
 
    End Sub 
 
     
    Private Sub GetDatatableID()     
        Try 
            Dim dt As New DataTable  
              Dim dtColumnList As New DataTable  
            Dim xmlDoc As New XmlDocument  
 
 
            Dim ds As New DataSet  
            Dim xmlReader As New XmlTextReader(Server.MapPath("~/XMLFiles_Search/IDFields.xml"))  
 
            ds.ReadXml(xmlReader)  
            xmlReader.Close()  
 
            dt = ds.Tables(0).Copy  
            For Each row As DataRow In dt.Rows  
                If row("Name") = ShowPage.ToLower.Trim Then 
                    ProjId = CType(row("ID"), String)  
                    Trial_ID = CType(row("TrialID"), String)  
                End If 
            Next 
 
        Catch ex As Exception  
 
        End Try 
 
 
    End Sub 
 
 
End Class 

But I am not able to maintain the radgrid structure if the checkbox is checked. As it I use Autopostback true then the grid refreshes and the checkbox column gets disappear. 
On a button click I have to remove all the unchecked rows from the grid.

Please can you suggest any way by which I can perform this function. 

Thanks and Regards,

Pinkey Yadav
   

 

 

5 Answers, 1 is accepted

Sort by
0
Pavlina
Telerik team
answered on 11 Feb 2010, 02:26 PM
Hello Pinkey,

Please checkout the following help articles which elaborate on this subject and let me know if they help to achieve your goal:
http://www.telerik.com/help/aspnet-ajax/grdprogrammaticcreation.html
http://www.telerik.com/help/aspnet-ajax/gridpersistcheckboxstateingridtemplatecolumnonrebind.html

Regards,
Pavlina
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.
0
Pinkey
Top achievements
Rank 1
answered on 15 Feb 2010, 10:20 AM
Hi Pavlina,

             Thanks for the reply. But my problem is that I am generating the checkbox column as template column at runtime. Hence whenever I check it, due to postback the Rad grid generates the additional column which are blank. It occurs everytime I check the checkbox which compressed the grid.
           For reference I have attached the images of the Rad Grid. The second image shows the checked row, which also indicate the columns compressed towards left of the grid.
           This also happens when I sort the column.

Please do suggest me some solution for this. Also is it possible to delete or remove the columns of the RadGrid.

Thanks and Regards,
Pinkey Yadav
0
Pavlina
Telerik team
answered on 15 Feb 2010, 01:09 PM
Hello Pinkey,

I suggest you review the following forum thread which elaborates on similar matter and see if it helps.
http://www.telerik.com/community/forums/aspnet-ajax/grid/empty-gridcolumns-after-postback.aspx

Additionally, if the issue persists it will be best if you open a formal support ticket and send us a simple working project with reproduced this erroneous behavior. Thus we could be able to help you further in resolving it.

Sincerely yours,
Pavlina
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.
0
Elango S
Top achievements
Rank 1
answered on 23 Feb 2010, 09:58 AM
Hi,

I created dynamic controls for the RAD GRID by the following code.

Dim
categoryColumn As New Telerik.WinControls.UI.GridViewComboBoxColumn

 

 

categoryColumn.UniqueName = "CategoryColumn"

 

 

 

categoryColumn.FieldName = "Type"

 

 

 

categoryColumn.HeaderText = "Type"

 

 

 

categoryColumn.DataSource = temp  // Existing datasource

 

categoryColumn.ValueMember =

"Type"

 

 

 

categoryColumn.DisplayMember = "Type"

 

 

Mygrid.MasterGridViewTemplate.Columns.Add(categoryColumn).

My questions are
1) How can I implement the SelectedIndexChanged event for this dynamic combobox during run time ?
2) In the same grid I also bind a textbox column. How can I implement the TextChanged event for this control ?

Do provide me with sample code ?

Thanks & Regards
Elango.S

 

0
Pavlina
Telerik team
answered on 25 Feb 2010, 03:43 PM
Hi Elango,

You could handle on ItemCreated event, find the combobox and attach handler for the SelectedIndexChanged event.

For more information about accessing cells and rows, please refer to the following help article:
Accessing cells and rows

Sincerely yours,
Pavlina
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
Pinkey
Top achievements
Rank 1
Answers by
Pavlina
Telerik team
Pinkey
Top achievements
Rank 1
Elango S
Top achievements
Rank 1
Share this question
or