Click here to Skip to main content
Click here to Skip to main content

Data Repeater Control in Windows Applications

, 29 Oct 2012
Rate this:
Please Sign up or sign in to vote.
An alternative way to use data repeater control in windows application.

Introduction

I worked with data repeater control in web application and i wanted to use one in windows application i was assigned to. I must admit, i had a hard time getting it to work. The only help on internet was - to use a database connection as data source and then to drag-drop relative fields on repeater control's surface. I did not find it suitable for my needs. I just can't show a data table contents to a user.

Background

A data repeater control takes advantage of a 'collection'. We need to declare the fields we want to show, and then we need to have a collection consisting those fields with our data(formatted/after applying business logic). On internet, data repeater control was bind to a data table. A data table essentially is a collection.

Using the code

As a starting point, I created a class in my application. A very simple class with two class variables and relative public properties. Then i created a constructor in that class so that I can initialize an object with data. Below is the code of that entire class.

using System;
using System.Collections.Generic;
using System.Text;

namespace testWinFormsRepeater.DataSourceForDataRepeaterControl
{
    public class clsDataRepeaterTemplateData
    {
        private string m_msg = string.Empty;
        private string m_sub = string.Empty;

        public string Message
        {
            get
            {
                return this.m_msg;
            }
        }

        public string Subject
        {
            get
            {
                return this.m_sub;
            }
        }

        public clsDataRepeaterTemplateData(string msg, string sub)
        {
            this.m_msg = msg;
            this.m_sub = sub;
        }
    }
}
  • Change the namespace of your class to make it distinctive. It will help later. I added ".DataSourceForDataRepeaterControl" to the original namespace as you may notice above.
  • I needed to rebuild my project in order to have this namespace get reflected.

Then I added a windows form to my project. And a data repeater control on it.

DataRepeaterInWinForms/main_screen.png

The next step was to add a data source to this repeater control. I opened 'Data Sources' window to add one.

  • If 'Data Sources' window is not open in your visual studio, you may find it using "Data->Show Data Sources" or by pressing "Shift+Alt+D".

DataRepeaterInWinForms/add_new_data_source.png

Clicking 'Add New Data Source' will open a wizard for adding a data source to your project.

DataRepeaterInWinForms/add_data_source_wiz_1.png

Select 'Object' and click 'Next'.

DataRepeaterInWinForms/add_data_source_wiz_2.png

As can be seen above, we will be presented with options to select our class (data source schema, here). Select your class under the modified namespace. Click Finish.

  • If you did not opt for having a distinctive namespace while creating a class, you may need to find your class under the default namespace.
  • Having a distinctive namespace is helpful in a scenario when you are using more than one class as data sources. With the approach of having an exclusive namespace for themselves, all 'classes as data sources' will club themselves together under one namespace here.

As soon as we click 'Finish', we are back in our 'Data Sources' window with one data source listed.

  • This data source is reflection of your class. If you declare a public class variable, it would be shown here too.
  • Note that the fields shown in your data source now, are actually the public properties of your class. Being a property, it provides us an option to modify the output ( we can code our desired functionality in 'get' section of desired property). Data repeater control will show a value determined by 'get' section of property.

DataRepeaterInWinForms/your_data_source_is_there.png

Now you can drag-drop the data fields you wish to appear on data repeater control. In our test application as seen above, if you drag both data fields your windows form should look like the image below:

DataRepeaterInWinForms/winform_looks_like_this_now.png

Our data repeater control is set. Now only data is required.

In Form_Load event of your form, assign the data source of your data repeater control a list. The list should contain objects of your special class. The code below should explain this condition itself:

private void Form1_Load(object sender, EventArgs e)
{
    this.dataRepeater1.DataSource = getDataForDataRepeaterControl();
}

private List<testWinFormsRepeater.DataSourceForDataRepeaterControl.clsDataRepeaterTemplateData> getDataForDataRepeaterControl()
{
    List<testWinFormsRepeater.DataSourceForDataRepeaterControl.clsDataRepeaterTemplateData> lst = new List<DataSourceForDataRepeaterControl.clsDataRepeaterTemplateData>();
    for (int i = 0; i <= 9; i++)
    {
        lst.Add(new DataSourceForDataRepeaterControl.clsDataRepeaterTemplateData("msg no. " + i.ToString(), "sub no. " + i.ToString()));
    }
    return lst;
}

Here, we are creating a collection of our class' objects. In the example above, List<> collection is used. After creating this collection, we are assigning it to the DataSource property of our data repeater control.

Rebuild and Run your project. In Form1, you should be able to see data repeater control with data.

Points of Interest

This article is to give you a start with Repeater control in Windows application. I could not find anything which could tell me alternative ways to bind and use a data repeater control in windows application. So I thought it may be helpful for someone else who does not want to use data repeater with a data table only.

There may be events of data repeater control which can enforce the functionality we achieved above. I don't know them. I got what I wanted in my application through above procedure and thought of sharing it. If someone knows other ways also, please share them.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

No Biography provided

Comments and Discussions

 
-- There are no messages in this forum --
| Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 29 Oct 2012
Article Copyright 2012 by gaurav_mittal
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid