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 Object, ByVal 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 Object, ByVal 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 String, ByVal IDField As String, ByVal Attachment As Boolean, ByVal 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 Object, ByVal 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 Object, ByVal 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