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

Thread is closed for posting
1 posts, 0 answers
  1. E2DE6E82-E2D9-4F0D-906D-3F1E2C32B9D6
    E2DE6E82-E2D9-4F0D-906D-3F1E2C32B9D6 avatar
    12 posts
    Member since:
    Jan 2017

    Posted 23 Jan 2019 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

This Code Library is part of the product documentation and subject to the respective product license agreement.