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

System Null Exception Error after adding CustomFilteringColumn in Project.

1 Answer 96 Views
Grid
This is a migrated thread and some comments may be shown as answers.
gc_0620
Top achievements
Rank 1
gc_0620 asked on 28 Oct 2011, 04:56 PM

Using VS 2008 SP1 with RadControls for ASP.NET 2011.2.712.V 2.

 

I copied below example into my Project.

 

http://demos.telerik.com/aspnet-ajax/grid/examples/programming/filteringtemplatecolumns/defaultcs.aspx

 

After adding the FilteringTemplateColumns.cs in APP_Code folder, I am getting attached error message.

The results are OK except the error shows in Visual Studio Design/Split Mode. This error does not
show in Telerik Demo.


Below is complete Code:

<%@ Page Language="c#" AutoEventWireup="false" CodeFile="DefaultCS.aspx.cs" Inherits="Telerik.GridExamplesCSharp.Programming.FilteringTemplateColumns.DefaultCS" %>
  
<%@ Register TagPrefix="custom" Namespace="FilteringTemplateColumns" %>
<%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
  
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
<head runat="server">
     
</head>
<body >
    <form id="mainForm" method="post" runat="server">
          
     <telerik:RadScriptManager ID="RadScriptManager1" runat="server" />
         
        <telerik:RadAjaxManager ID="RadAjaxManager1" runat="server">
            <AjaxSettings>
                <telerik:AjaxSetting AjaxControlID="RadGrid1">
                    <UpdatedControls>
                        <telerik:AjaxUpdatedControl ControlID="RadGrid1" />
                    </UpdatedControls>
                </telerik:AjaxSetting>
            </AjaxSettings>
        </telerik:RadAjaxManager>
        <telerik:RadGrid ID="RadGrid1" DataSourceID="SqlDataSource1" Width="97%" AllowFilteringByColumn="True"
            AllowSorting="True" PageSize="12" ShowFooter="True" AllowPaging="True" runat="server"
            AutoGenerateColumns="False" GridLines="None" ShowStatusBar="true">
              <GroupingSettings CaseSensitive="false" />
            <MasterTableView EditMode="InPlace" AllowFilteringByColumn="True" ShowFooter="True"
                DataKeyNames="CustomerID" TableLayout="Auto">
                <Columns>
                    <telerik:GridBoundColumn DataField="ContactName" HeaderText="Contact name">
                        <HeaderStyle Width="25%" />
                    </telerik:GridBoundColumn>
                    <telerik:GridBoundColumn DataField="ContactTitle" HeaderText="Contact title"
                     AutoPostBackOnFilter="true" CurrentFilterFunction="Contains" ShowFilterIcon="false">
                        <HeaderStyle Width="25%" />
                    </telerik:GridBoundColumn>
                    <telerik:GridTemplateColumn DataField="City" HeaderText="TemplateColumn City">
                        <HeaderStyle Width="25%" />
                        <ItemTemplate>
                            <%# Eval("City") %>
                        </ItemTemplate>
                    </telerik:GridTemplateColumn>
                   <custom:MyCustomFilteringColumn DataField="Country" FilterControlWidth="180px" 
                   HeaderText="Custom TemplateColumn Country">
                        <headerstyle width="25%" />
                        <itemtemplate>
                              <img src='Img/<%# Eval("Country") %>.gif'
                               alt="" style="vertical-align:middle;margin-right:7px;"/><%# Eval("Country") %>
                        </itemtemplate>
                    </custom:MyCustomFilteringColumn>
                </Columns>
            </MasterTableView>
        </telerik:RadGrid>
        <br />
        <asp:SqlDataSource ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthWindConnectionString %>"
            ProviderName="System.Data.SqlClient" SelectCommand="SELECT CustomerID, ContactName, ContactTitle, Country, City FROM Customers"
            runat="server"></asp:SqlDataSource>
  
      
        
    </form>
</body>
</html>
  
DefaultCS.aspx.cs
_____________
  
  
using Telerik.Web.UI;
using System.Web.UI.WebControls;
  
namespace Telerik.GridExamplesCSharp.Programming.FilteringTemplateColumns
{
    public partial class DefaultCS : System.Web.UI.Page
    {
    }
  
}
_________
  
FilteringTemplateColumns.cs
  
using Telerik.Web.UI;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
  
  
namespace FilteringTemplateColumns
{
    public class MyCustomFilteringColumn : GridTemplateColumn
    {
  
        public DataTable GetDataTable(string queryString)
        {
            string ConnString = ConfigurationManager.ConnectionStrings["NorthWindConnectionString"].ConnectionString;
            SqlConnection MySqlConnection = new SqlConnection(ConnString);
            SqlDataAdapter MySqlDataAdapter = new SqlDataAdapter();
            MySqlDataAdapter.SelectCommand = new SqlCommand(queryString, MySqlConnection);
  
            DataTable myDataTable = new DataTable();
            MySqlConnection.Open();
            try
            {
                MySqlDataAdapter.Fill(myDataTable);
            }
            finally
            {
                MySqlConnection.Close();
            }
  
            return myDataTable;
        }
  
        protected override void SetupFilterControls(TableCell cell)
        {
            RadComboBox rcBox = new RadComboBox();
            rcBox.ID = "DropDownList1";
            //rcBox.Width = Unit.Percentage(100);
            rcBox.AutoPostBack = true;
            rcBox.DataTextField = this.DataField;
            rcBox.DataValueField = this.DataField;
            rcBox.SelectedIndexChanged += rcBox_SelectedIndexChanged;
            DataTable table = GetDataTable(string.Format("SELECT DISTINCT {0} FROM {1}", this.DataField, "Customers"));
            DataRow row = table.NewRow();
            row[this.DataField] = "";
            table.Rows.InsertAt(row, 0);
            rcBox.DataSource = table;
            cell.Controls.Add(rcBox);
        }
  
        protected override void SetCurrentFilterValueToControl(TableCell cell)
        {
            if (!(this.CurrentFilterValue == ""))
            {
                ((RadComboBox)cell.Controls[0]).Items.FindItemByText(this.CurrentFilterValue).Selected = true;
            }
        }
  
        protected override string GetCurrentFilterValueFromControl(TableCell cell)
        {
            string currentValue = ((RadComboBox)cell.Controls[0]).SelectedItem.Value;
            this.CurrentFilterFunction = (currentValue != "")? GridKnownFunction.EqualTo : GridKnownFunction.NoFilter;
            return currentValue;
        }
  
        private void rcBox_SelectedIndexChanged(object sender, Telerik.Web.UI.RadComboBoxSelectedIndexChangedEventArgs e)
        {
            ((GridFilteringItem)(((RadComboBox)sender).Parent.Parent)).FireCommandEvent("Filter", new Pair());
        }
    }
}

Any help will be appreciated.

 

Thanks


GC_0620


1 Answer, 1 is accepted

Sort by
0
Andrey
Telerik team
answered on 02 Nov 2011, 03:39 PM
Hello Gc_0620,

Seeing this error in the designer is expected, because this demo relies on a custom implementation of a column. And RadGrid does not know how to visualize such kind of content. Because of the many variations of the custom implementation we do not provide designer support for them.

Greetings,
Andrey
the Telerik team
If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
Tags
Grid
Asked by
gc_0620
Top achievements
Rank 1
Answers by
Andrey
Telerik team
Share this question
or