Giorgio Giorgio - 3 years ago 267
ASP.NET (C#) Question

Type UserControl does not have a public property named Columns

I'm trying to develop a

containing a
inside a

<asp:Panel runat="server" ID="panel">
<asp:GridView ID="gridView" runat="server" AutoGenerateColumns="false">

Code behind

public partial class DropDownMultiColumn : UserControl

public virtual DataControlFieldCollection Columns
get { return gridView.Columns; }
foreach (DataControlField c in value)

I've included the control into a ASPX page, using the
public property

<dd:DropDownMultiColumn id="_dd_InsertStore" runat="server" DataSourceID="_dsTest" >
<asp:BoundField DataField="MGCOD" HeaderText="MGCOD"></asp:BoundField>
<asp:BoundField DataField="MGDSC" HeaderText="MGDSC"></asp:BoundField>

But I get the following error in Design section:

Type 'System.Web.UI.UserControl' does not have a public property named 'Columns'

I've tried to implement this solution, but it doesn't work. Any ideas?

Answer Source

Here follows a working workaround for the issue.

Declare a property with JSON content

Declare Columns as an attribute, containing required data in JSON format

<dd:DropDownMultiColumn id="_ddInsertStore" runat="server" DataSourceID="_dsTest" Columns="
    [{ 'DataField':'MGCOD', 'HeaderText':'MGCOD' },
    { 'DataField':'MGDSC', 'HeaderText':'MGDSC' }]">

Convert JSON to BoundField

Within OnLoad event, deserialize JSON and build BoundField data

protected override void OnLoad(EventArgs e)
    List<Column> columns = JsonConvert.DeserializeObject<List<Column>>(Columns);

    foreach (Column column in columns)
        BoundField boundField = new BoundField();
        boundField.DataField = column.DataField;
        boundField.HeaderText = (string) column.HeaderText;

Declare a Column class, having the required properties.

public class Column
    public string DataField { get; set; }
    public string HeaderText { get; set; }
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download