Binding a DataGrid in .Net

I wanted to bind a Windows form datagrid to data source which was a strongly typed (generic) collection and went into problems. So I investigated how to bind to four different datasources: a DataSet, a typed array, an untyped list (ArrayList) and a typed list (generic List). Here’s how to do it. This code also demonstrates how to customize the displayed columns. The records displayed only contains one column called "Title".
 

private void DisplayDataSet()

{

    dataSet1.Tables[0].Rows.Add("Title 1");

    dataSet1.Tables[0].Rows.Add("Title 2");

    DataGridTableStyle tableStyle = new DataGridTableStyle();

    tableStyle.MappingName = "Titles";

    DataGridColumnStyle columnStyle = new DataGridTextBoxColumn();

    columnStyle.MappingName = "Title";

    columnStyle.HeaderText = "Title";

    columnStyle.Width = dataGrid1.Width;

    tableStyle.GridColumnStyles.Add(columnStyle);

    dataGrid1.TableStyles.Add(tableStyle);

    dataGrid1.DataSource = dataSet1;

    dataGrid1.DataMember = "Titles";

}

 

private void DisplayTypedArray()

{

    Record[] titles = new Record[2];

    titles[0] = new Record("Title 3");

    titles[1] = new Record("Title 4");

    DataGridTableStyle tableStyle = new DataGridTableStyle();

    tableStyle.MappingName = "Record[]";

    DataGridColumnStyle columnStyle = new DataGridTextBoxColumn();

    columnStyle.MappingName = "Title";

    columnStyle.HeaderText = "Title";

    columnStyle.Width = dataGrid2.Width;

    tableStyle.GridColumnStyles.Add(columnStyle);

    dataGrid2.TableStyles.Add(tableStyle);

    dataGrid2.DataSource = titles;

}

 

private void DisplayArrayList()

{

    System.Collections.ArrayList titles = new System.Collections.ArrayList();

    titles.Add(new Record("Title 5"));

    titles.Add(new Record("Title 6"));

    DataGridTableStyle tableStyle = new DataGridTableStyle();

    tableStyle.MappingName = "ArrayList";

    DataGridColumnStyle columnStyle = new DataGridTextBoxColumn();

    columnStyle.MappingName = "Title";

    columnStyle.HeaderText = "Title";

    columnStyle.Width = dataGrid3.Width;

    tableStyle.GridColumnStyles.Add(columnStyle);

    dataGrid3.TableStyles.Add(tableStyle);

    dataGrid3.DataSource = titles;

}

 

private void DisplayTypedList()

{

    System.Collections.Generic.List<Record> titles = new System.Collections.Generic.List<Record>();

    titles.Add(new Record("Title 7"));

    titles.Add(new Record("Title 8"));

    DataGridTableStyle tableStyle = new DataGridTableStyle();

    tableStyle.MappingName = titles.GetType().Name;

    DataGridColumnStyle columnStyle = new DataGridTextBoxColumn();

    columnStyle.MappingName = "Title";

    columnStyle.HeaderText = "Title";

    columnStyle.Width = dataGrid4.Width;

    tableStyle.GridColumnStyles.Add(columnStyle);

    dataGrid4.TableStyles.Add(tableStyle);

    dataGrid4.DataSource = titles;

}

 

public class Record

{

    private string title;

    public string Title

    {

        get { return title; }

        set { title = value; }

    }

   

    public Record(string title)

    {

        this.title = title;

    }

}

 

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s