Add custom data to dataitem similar to adding attributes in ItemDataBound event

1 posts, 0 answers
  1. Peter
    Peter avatar
    12 posts
    Member since:
    Jan 2017

    Posted 23 Jan Link to this post

    Requirements

    Telerik Product and Version

    UI for ASP.NET AJAX 2019 R1

    Supported Browsers and Platforms

    all browsers supported by Telerik UI for ASP.NET AJAX suite

    Components/Widgets used (JS frameworks, etc.)

    RadMultiColumnComboBox, .NET 4.0/4.5 C#

    PROJECT DESCRIPTION


    This project shows how to modify the data items from SqlDataSource to add or hide any information to an item. This is similar to using and ItemDataBound server-side event and add some attributes to the item.



    <telerik:RadMultiColumnComboBox runat="server" ID="RadMultiColumnComboBox1" Skin="Default"
        Height="400px" Width="300px"
        DataTextField="Name" DataValueField="ID"
        Placeholder="select from the dropdown or type">
        
        <ColumnsCollection>
            <telerik:MultiColumnComboBoxColumn Field="Description" Title="Name" Width="450px" />
            <telerik:MultiColumnComboBoxColumn Field="Title" Title="Title" Width="200px" />
        </ColumnsCollection>
    </telerik:RadMultiColumnComboBox>
    <asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
        ProviderName="System.Data.SqlClient"
        SelectCommand="SELECT Top 20 [CustomerID], [ContactName], [ContactTitle], [CompanyName] FROM [Customers]"></asp:SqlDataSource>

    protected void Page_Load(object sender, EventArgs e)
    {
        DataSourceSelectArguments args = new DataSourceSelectArguments();
        DataView view = (DataView) SqlDataSource1.Select(args);
        DataTable dt = view.ToTable();
     
        RadMultiColumnComboBox1.DataSource = GetModifiedDataSource(dt);
        RadMultiColumnComboBox1.DataBind();
    }
     
    private DataTable GetModifiedDataSource(DataTable dt)
    {
        DataTable dataTable = new DataTable();
     
        dataTable.Columns.Add(new DataColumn("ID", typeof(string)));
        dataTable.Columns.Add(new DataColumn("Name", typeof(string)));
        dataTable.Columns.Add(new DataColumn("Description", typeof(string)));
        dataTable.Columns.Add(new DataColumn("Title", typeof(string)));
     
        dataTable.PrimaryKey = new DataColumn[] { dataTable.Columns["ID"] };
        var rowsCount = dt.Rows.Count;
        for (int i = 0; i < rowsCount; i++)
        {
            DataRow dataRow = dt.Rows[i];
            DataRow row = dataTable.NewRow();
            row["ID"] = dataRow["CustomerID"];
            row["Name"] = dataRow["ContactName"];
            row["Description"] = dataRow["ContactName"] + " from " + dataRow["CompanyName"];
            row["Title"] = dataRow["ContactTitle"];
     
            dataTable.Rows.Add(row);
        }
     
        return dataTable;
    }



Back to Top