dotSITE
Учебник по ASP.NET Форумы Work in Murano Software.
новости материалы решения форумы группы настройки/о проекте
Назад

Как осуществить привязку ArrayList'а к DataGrid и отформатировать столбцы?

Пример, который показывает, как это сделать.

using System;
using System.Collections;
using System.Windows.Forms;

namespace TestApplication
{
  public class TestForm : System.Windows.Forms.Form
  {
    private DataGrid _booksGrid;

    public TestForm()
    {
      Text = "Test Form";
      _booksGrid = new DataGrid();
      _booksGrid.Name = "_booksGrid";
      _booksGrid.Dock = DockStyle.Fill;
      Controls.Add(_booksGrid);
      _booksGrid.DataSource = CreateBooksDataSource();
      _booksGrid.TableStyles.Add(CreateBooksStyle());
    }

    ArrayList CreateBooksDataSource()
    {
      ArrayList booksList = new ArrayList();
      for(int i = 0; i < 10; i++)
      {
        booksList.Add(new Book(i));
      }
      return booksList;
    }

    DataGridTableStyle CreateBooksStyle()
    {
      DataGridTableStyle tableStyle = new DataGridTableStyle();
      tableStyle.MappingName = "ArrayList";
      tableStyle.PreferredColumnWidth = 150;
      DataGridTextBoxColumn titleColumn = new DataGridTextBoxColumn();
      titleColumn.HeaderText = "Название";
      titleColumn.MappingName = "Title";
      tableStyle.GridColumnStyles.Add(titleColumn);
      DataGridTextBoxColumn authorColumn = new DataGridTextBoxColumn();
      authorColumn.HeaderText = "Автор";
      authorColumn.MappingName = "Author";
      tableStyle.GridColumnStyles.Add(authorColumn);
      return tableStyle;
    }

    [STAThread]
    static void Main()
    {
      Application.Run(new TestForm());
    }

  }

  public class Book
  {
    private string _title;

    public string Title
    {
      get{ return _title; }
      set{ _title = value; }
    }

    private string _author;

    public string Author
    {
      get{ return _author; }
      set{ _author = value; }
    }

    public Book(int i)
    {
      _title = "book #" + i.ToString();
      _author = "author #" + i.ToString();
    }
  }
}

По материалам http://www.dotsite.ru/Forums/8975.aspx


 Назад
Контакт Реклама на сайте Спонсорам Веб мастерам

Лицензионное соглашение - © 2000-2012 dotSITE
Хостинг .NET предоставлен PARKING.RU
Поддержку сайта осуществляет Murano Software Inc., Offshore software development