http://msdn.microsoft.com/ko-kr/library/ms229684.aspx

 

방법: Pocket PC에서 DataGrid 사용

업데이트: 2007년 11월

이 예제에서는 DataGrid 컨트롤을 폼과 함께 사용하여 DataGrid 컨트롤에서 선택된 레코드를 보고 편집하며 새 레코드를 데이터베이스에 추가하는 방법을 보여 줍니다. .NET Compact Framework에서는 DataGrid 셀 편집이 지원되지 않으므로 DataGrid 값을 편집할 수 있도록 사용자 인터페이스가 제공되어야 합니다. 이 예제에서는 Visual Studio와 함께 설치되는 Northwind 데이터베이스를 사용합니다.

참고:

.NET Compact Framework 버전 2.0을 사용하는 경우 DataGrid 컨트롤을 사용하려면 프로젝트에 System.Windows.Forms.DataGrid.dll에 대한 참조를 추가해야 합니다.

BindingSource 개체는 데이터베이스에서 현재 선택된 레코드에 대한 액세스를 제공하고, 이 레코드는 요약 및 편집 폼의 생성자로 전달되어 모든 폼에서 동일한 바인딩 소스가 사용되도록 합니다. 데이터 바인딩 컨트롤 외에도 BindingSource 개체는 현재 행의 DataRowView 개체를 반환할 수 있습니다. DataRowView를 사용하여 열의 현재 값을 확인하는 등 여러 가지 목적으로 데이터에 액세스할 수 있습니다. 요약 및 편집 폼에 대한 이 예제에서는 데모용으로 두 개의 열만 사용합니다.

또는 DataGrid 컨트롤의 스마트 태그에 대한 바로 가기 메뉴에서 데이터 폼 생성을 선택하여 Visual Studio가 요약 및 편집 폼을 자동으로 생성하도록 할 수 있습니다. 이 기능에 대한 자세한 내용은 방법: 데이터 응용 프로그램에 대한 요약 및 편집 뷰 생성(장치)을 참조하십시오.

이 응용 프로그램에는 다음 표에 설명된 폼이 있습니다. 이 표에는 또한 해당 메뉴 옵션도 나열되어 있습니다.

기능

메뉴 옵션

기본 폼

(Form1)

DataGrid 컨트롤을 표시합니다.

New

데이터베이스에 새 레코드를 추가하고 EditView 폼을 표시합니다.

Edit

EditView 폼을 표시합니다.

SummaryView

보기 편하도록 최적화하여 현재 레코드의 열 값을 표시합니다.

(없음)

EditView

편집하기 편하도록 최적화하여 현재 레코드의 열 값을 표시합니다.

Done

대화 상자에서 확인을 눌러 데이터베이스를 업데이트한 다음 기본 폼을 표시합니다.

Cancel

대화 상자를 취소하고 기본 폼을 표시합니다.

프로젝트를 만들고 기본 폼을 디자인하려면
  1. Visual Studio에서 스마트 장치 프로젝트를 만들고 대상 플랫폼을 Windows Mobile 5.0 Pocket PC SDK 또는 Windows Mobile 6 Professional SDK로 설정합니다.

  2. 데이터 메뉴에서 새 데이터 소스 추가를 클릭합니다.

  3. 데이터 소스 구성 마법사에서 Microsoft SQL Server Compact Edition(.NET Framework Data Provider for SQL Server CE)을 사용하여 Northwind 데이터베이스에 연결합니다. Northwind 데이터베이스인 Northwind.sdf는 \Program Files\Microsoft SQL Server Compact Edition\v3.5\Samples 폴더에 설치되어 있습니다.

    참고:

    Windows Vista에서 Northwind 데이터베이스에 액세스하려면 관리자로 Visual Studio를 실행해야 합니다. 데이터베이스를 추가하는 방법에 대한 자세한 내용은 방법: 장치 프로젝트에 데이터베이스 추가를 참조하십시오.

  4. 마법사의 데이터베이스 개체 선택 페이지에서 Products 테이블과 이 테이블의 모든 열을 선택합니다.

  5. 도구 상자에서 DataGrid 컨트롤을 폼에 추가합니다. 원하는 대로 해당 크기와 레이아웃 속성을 설정합니다.

  6. DataSource 속성을 Products 테이블로 설정합니다. Visual Studio에서 프로젝트에 NorthwindDataSet, ProductsBindingSource 및 ProductsTableAdapter 개체를 추가합니다.

  7. DataGridTableStyle 개체를 TableStyles 컬렉션에 추가하여 DataGrid 컨트롤이 테이블의 한두 열을 표시하도록 합니다. 속성 창에서 TableStyles 속성을 클릭합니다. 이 작업으로 DataGridTableStyle 컬렉션 편집기 대화 상자가 표시됩니다. 그런 후 다음 작업을 수행합니다.

    1. TableStyles 컬렉션에 DataGridTableStyle 개체를 추가합니다.

    2. MappingName 속성으로 "Products"를 지정합니다.

    3. GridColumnStyle 속성을 클릭합니다. 이 작업으로 DataGridColumnStyle 컬렉션 편집기 대화 상자가 표시됩니다.

    4. GridColumnStyles 컬렉션에 DataGridTextBoxColumn 개체를 추가합니다.

    5. MappingName 속성을 클릭하고 Product Name을 선택합니다.

    6. 원하는 머리글 텍스트와 너비를 설정합니다.

    7. 추가 열에 대해 반복합니다.

    8. 대화 상자를 닫습니다.

  8. 각각 요약 뷰와 편집 뷰로 사용할 두 개의 폼을 프로젝트에 추가합니다. 각각의 이름을 SummaryView와 EditView로 지정합니다.

  9. SummaryView 및 EditView 폼의 생성자에 BindingSource 개체를 가져오는 매개 변수를 추가합니다. 이러한 폼에서 CurrentBindingSouce라는 전역 변수를 선언하여 생성자에 전달된 BindingSource 개체로 설정합니다. 이것은 InitializeComponent 메서드가 호출되기 전에 설정되어야 합니다.

    Visual Basic 개발자는 코드 창의 오른쪽 위에 있는 메서드 이름 목록에서 New 메서드를 추가하여 폼에 Sub New를 추가해야 합니다.

     

private BindingSource CurrentBindingSource;
public SummaryView(BindingSource bsource)
{
    CurrentBindingSource = bsource;
    InitializeComponent();
}

 

 

기본 폼에서 New라는 MenuItem 개체(MenuItem1)와 Edit라는 다른 개체(MenuItem2)를 추가합니다. New 및 Edit의 Click 이벤트에 대해 다음 코드를 추가합니다.

// Add new record.
private void menuItem1_Click(object sender, EventArgs e)
{
    productsBindingSource.AllowNew = true;
    productsBindingSource.AddNew();
    EditView EditViewDialog = new EditView(productsBindingSource);
    if (EditViewDialog.ShowDialog() != DialogResult.OK)
    {
        productsBindingSource.CancelEdit();
    }
    else
    {
        ProductsBindingSource.EndEdit();
        this.productsTableAdapter.Update(this.northwindDataSet);
     }
}
// Edit record (Edit).
private void menuItem2_Click(object sender, EventArgs e)
{
    EditView EditViewDialog = new EditView(productsBindingSource);
    if (EditViewDialog.ShowDialog() != DialogResult.OK)
    {
        productsBindingSource.CancelEdit();
    }
    else
    {
        productsBindingSource.EndEdit();
        this.productsTableAdapter.Update(this.northwindDataSet);
    }
}

기본 폼에서 DataGrid 컨트롤에 대해, Pocket PC에서 작업 키를 누르면 발생하는 KeyDown 이벤트에 대한 코드를 추가합니다. 이 작업으로 SummaryView 폼이 표시됩니다.

// Action button pressed.
private void dataGrid1_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        SummaryView SummaryViewDialog =
          new SummaryView(productsBindingSource);
        SummaryViewDialog.ShowDialog();
     }
}

요약 뷰를 만들려면

  • SummaryView 폼에 다음 컨트롤을 추가합니다.

    • Product Name 제목(예: "Product Name:")에 대한 Label 컨트롤

    • Product Name 값에 대한 Label 컨트롤

    • Discontinued 값에 대한 Label 컨트롤. 이 컨트롤은 Products 테이블의 Discontinued 열 값이 true인 경우에만 표시됩니다. 이 레이블을 빨간 글꼴로 "DISCONTINUED"라고 지정합니다.

  • SummaryView 폼에 대한 생성자에 다음 코드를 추가하여 데이터 바인딩을 설정합니다. CurrentBindingSource라는 폼 변수를 선언하여 폼의 생성자에서 전달된 BindingSource 인스턴스로 설정되도록 합니다. DataRowView 개체는 Discontinued 열이 true이고 Discontinued 레이블이 표시되는지 여부를 결정합니다.

  •  

     

     

    private BindingSource CurrentBindingSource;
    public SummaryView(BindingSource bsource)
    {
        CurrentBindingSource = bsource;
        InitializeComponent();
        // Bind the label that shows the product name.
        ProductNameLabelVal.DataBindings.Add("Text",
          CurrentBindingSource, "Product Name");
        // Show the Discontinued label if
        // that value is true in the database.
        DataRowView drView;
        drView = (DataRowView) CurrentBindingSource.Current;
        if (drView["Discontinued"] == true)
        {
            DiscontinuedLabel.Visible = true;
        }
        else
        {
            DiscontinuedLabel.Visible = false;
        }
    }

     

     

    편집 뷰를 만들려면

    1. Microsoft.WindowsCE.Forms 네임스페이스에 대한 참조를 프로젝트에 추가합니다.

    2. InputPanel 구성 요소를 도구 상자에서 EditView 폼으로 끌어 옵니다. 텍스트 상자에 텍스트를 입력할 수 있는 SIP(Soft Input Panel)를 사용자에게 제공하는 데는 하나의 인스턴스만 필요합니다.

    3. 폼에 다음 컨트롤을 추가합니다.

      • Product Name 텍스트 상자에 대한 Label 컨트롤

      • Pruduct Name 열에 대한 TextBox 컨트롤

      • Discontinued 열에 대한 CheckBox 컨트롤. 이 컨트롤의 ThreeState 속성을 true로 설정합니다.

    4. 데이터 바인딩을 설정하려면 폼의 생성자에서 InitializeComponent 호출 뒤에 다음 코드를 추가합니다. 이 코드는 새 레코드를 추가하거나 기존 레코드를 편집하는 작업을 수행합니다. 새 레코드가 추가되면 DataRowView 개체는 Discontinued 열의 값이 null인지 확인하고 바인딩의 NullValue 속성을 CheckState 속성의 Indeterminate 값으로 설정합니다.

      public EditView(BindingSource bsource)
      {
          CurrentBindingSource = bsource;
           InitializeComponent();
           CurrentBindingSource = bsource;
           InitializeComponent();
           //  Add the bindings.
           productNameTextBox.DataBindings.Add("Text",
             CurrentBindingSource, "Product Name");
           DataRowView drView;
           drView = (DataRowView) CurrentBindingSource.Current;
           if (drView("Discontinued") == null)
           {
               DiscontinuedCheckBox.DataBindings.Add("CheckState",
                 CurrentBindingSource, "Discontinued", true,
                 DataSourceUpdateMode.OnValidation,
                 CheckState.Indeterminate);
           }
           else
           {
                DiscontinuedCheckBox.DataBindings.Add("Checked",
                  CurrentBindingSource, "Discontinued");
           }
      }

     

    image

    DataGrid 의 컬럼을 원하는 모양으로 변경하겠습니다.

    기본적으로는 바인딩되어져 있는 컬럼명이 표시됩니다.

     

     

    image

    TableStyles 를 클릭합니다.

     

     

    image

    GridColumStyles( 컬렉션을 클릭해서 ) 상세 컬럼을 적어줍니다.

    MappingName 에 바인딩되는 데이터셋 이름을 적어줍니다.

    예를들어 AppDatabase1DataSet 에 MyNumbers 테이블과

    Numbers 테이블이 있는데 바인딩이 MyNumbers  되어져 있다면  MyNumbers 를 입력합니다.

     

     

     

     image

    요기서 사이즈와 헤더표시이름을 변경할수 있습니다.

     

     

    image

    변경된 화면

     

    실행화면

    image

     

    * 코드로 구현하기

    DataTable dt = new LoadGenres().Load(strFullPathToMyFile).Tables[0];        genreGrid.DataSource = dt;
    DataGridTableStyle ts1 = new DataGridTableStyle();
            ts1.MappingName = dt.TableName.ToString();
    DataGridColumnStyle textCol1 = new DataGridTextBoxColumn();
            textCol1.MappingName = dt.Columns[0].ColumnName.ToString();
            textCol1.HeaderText = "genre";
            textCol1.Width = 300;
            ts1.GridColumnStyles.Add(textCol1);

     

    image

    데이터 베이스를 만들어 줍니다

     

     

    image

    데이터 필드는 위와 같이 초간단 입니다

     

    그다음 아래와 같은 셀렉트 메소드를 만들어줍니다.

    image

     

     

     

    코드상에서 아래와 같이 하면 끝입니다.~

    private void Form1_Load(object sender, EventArgs e)
    {

        BindingSource binding1 = new BindingSource();
        binding1.DataSource = testTableAdapter.GetDataByTitle(2222);
        this.dataGrid1.DataSource = binding1;

    }

     

     

     

    image

     


    참고사항

     

    BindingSource 구성 요소는 다양한 용도로 사용됩니다. 먼저 이 구성 요소는 Windows Forms 컨트롤과 데이터 소스 간의 현재 위치 관리, 변경 알림 및 기타 서비스를 제공하므로 DataSource 속성으로 BindingSource 구성 요소를 데이터 소스에 연결하여 폼의 컨트롤을 데이터에 간단하게 바인딩할 수 있습니다. 복잡한 바인딩 시나리오의 경우 필요에 따라 DataMember 속성을 데이터 소스의 특정 열이나 목록으로 설정할 수 있습니다. 그런 다음 컨트롤을 BindingSource에 바인딩합니다. 데이터와의 모든 추가 상호 작용은 BindingSource 구성 요소 호출을 통해 이루어지며 BindingSource가 바인딩 프로세스를 단순화할 수 있는 방법에 대한 예제는 방법: DBNull 데이터베이스 값에 Windows Forms 컨트롤 바인딩 및 방법: 데이터 바인딩에서 발생하는 오류 및 예외 처리를 참조하십시오. 데이터 소스의 탐색과 업데이트는 MoveNext, MoveLast, Remove 등의 메서드를 통해 이루어집니다. 정렬과 필터링 등의 작업은 SortFilter 속성을 통해 처리됩니다. BindingSource로 정렬 및 필터링 사용에 대한 자세한 내용은 방법: Windows Forms BindingSource 구성 요소를 사용하여 ADO.NET 데이터 정렬 및 필터링을 참조하십시오.

    또한 BindingSource 구성 요소를 강력한 형식의 데이터 소스로 사용할 수 있습니다. 일반적으로 내부 데이터 소스의 형식은 다음 메커니즘 중 하나를 통해 고정됩니다.

    • Add 메서드를 사용하여 BindingSource 구성 요소에 항목을 추가합니다.
    • DataSource 속성을 목록, 단일 개체 또는 형식으로 설정합니다.

    이 두 메커니즘은 모두 강력한 형식의 목록을 만듭니다. BindingSource를 사용하여 형식에 바인딩하는 방법에 대한 자세한 내용은 방법: 형식에 Windows Forms 컨트롤 바인딩을 참조하십시오. BindingSource를 사용하여 팩터리 개체에 컨트롤을 바인딩할 수도 있습니다. 이 작업을 수행하는 방법에 대한 자세한 내용은 방법: 팩터리 개체에 Windows Forms 컨트롤 바인딩을 참조하십시오.

    clip_image001

    참고:

    BindingSource에서는 단순한 데이터 소스와 복잡한 데이터 소스 모두를 처리하므로 용어상의 문제가 있습니다. 이 클래스에 대한 설명에서 목록이라는 용어는 호스팅된 데이터 소스 내의 데이터 컬렉션을 나타내고 항목은 단일 요소를 나타냅니다. 복잡한 데이터 소스와 관련된 기능을 설명할 때는 같은 의미로 테이블과 행이라는 용어가 사용됩니다.

    BindingSource에서는 내부 데이터에 액세스하기 위한 멤버를 제공합니다. 현재 항목은 Current 속성을 통해 검색할 수 있으며 목록 전체는 List 속성을 통해 검색할 수 있습니다. 현재 항목에 대한 편집 작업은 Current 속성과 RemoveCurrent, EndEdit, CancelEdit, AddAddNew 메서드를 통해 지원됩니다. 통화 관리는 모든 내부 데이터 소스 형식에 대해 자동으로 처리되지만 이 클래스는 사용자 지정할 수 있는 CurrentItemChangedDataSourceChanged 등의 여러 이벤트를 노출합니다.

    목록 내의 항목을 탐색하기 위해 VCR과 비슷한 UI(사용자 인터페이스)를 제공하는 BindingNavigator 클래스를 사용하여 BindingSource 구성 요소에 바인딩된 데이터 소스를 탐색하거나 관리할 수도 있습니다. BindingNavigator는 모든 데이터 소스에 바인딩될 수 있지만 BindingNavigator.BindingSource 속성을 통해 BindingSource 구성 요소와 통합되도록 디자인되어 있습니다.

    BindingSource 클래스의 기본 속성은 DataSource입니다. 기본 이벤트는 CurrentChanged입니다.

    Get videos designed for all .NET Compact Framework developers, from the novice to the professional. If you are new to the .NET Compact Framework, you can learn the basics of how to create applications for devices, using either Visual Basic or C#.

    Categories include: the .NET Framework; Data and Services; Hardware, Performance, and Resource Management; Multithreaded Programming; Office; Security and Deployment; Testing and Debugging; User Interface Programming, Web Technologies, State and Notification Broker; and PowerToys.

    Check back often or subscribe to the RSS feed to be notified when new videos are added.

     

     

    모바일 개발 온라인강좌

    http://msdn.microsoft.com/ko-kr/windowsmobile/bb495180(en-us).aspx

    Windows Mobile 6 Professional and Standard Software Development Kits Refresh

    http://www.microsoft.com/downloads/details.aspx?FamilyID=06111a3a-a651-4745-88ef-3d48091a390b&displaylang=en

    Overview

    The Windows Mobile 6 SDKs add documentation, sample code, header and library files, emulator images and tools to Visual Studio that let you build applications for Windows Mobile 6.
    With Windows Mobile 6, we are revising our SKU taxonomy and naming to better align our brand and products with the realities of today’s mobile device marketplace. The historical form-factor based distinction between Windows Mobile powered Smartphone and Windows Mobile powered Pocket PC Phone Edition is blurring dramatically. We want our taxonomies and terminology to evolve to better reflect the evolution of the mobile device industry.
    The list below summarizes the changes and the SDK that must be used when targeting the platforms (Previous Categories = New Categories):

    • Windows Mobile for Smartphone = Windows Mobile Standard (download the Windows Mobile 6 Standard SDK)
    • Windows Mobile for Pocket PC = Windows Mobile Classic (download the Windows Mobile 6 Professional SDK)
    • Windows Mobile for Pocket PC Phone Edition = Windows Mobile Professional (download the Windows Mobile 6 Professional SDK)

    The Windows Mobile 6 SDKs reflect these changes in the naming of "Project Templates" and "Emulator Images".
    The following emulator images are included in the SDKs:

    • Windows Mobile 6 Standard SDK
      • Windows Mobile 6 Standard (176x220 pixels - 96 dpi)
      • Windows Mobile 6 Standard Landscape QVGA (240x320 pixels - 131 dpi)
      • Windows Mobile 6 Standard QVGA (320x240 pixels - 131 dpi)
    • Windows Mobile 6 Professional SDK
      • Windows Mobile 6 Classic (240x320 pixels - 96 dpi)
      • Windows Mobile 6 Professional (240x320 pixels - 96 dpi)
      • Windows Mobile 6 Professional Square (240x240 pixels - 96 dpi)
      • Windows Mobile 6 Professional Square QVGA (320x320 pixels - 128 dpi)
      • Windows Mobile 6 Professional Square VGA (480x480 pixels - 192 dpi)
      • Windows Mobile 6 Professional VGA (480x640 pixels - 192 dpi)

    Windows Mobile 6.1 Emulator Images

    http://www.microsoft.com/downloads/details.aspx?familyid=3D6F581E-C093-4B15-AB0C-A2CE5BFFDB47&displaylang=en

    Windows Mobile 6 Localized Emulator Images

    하단으로 주욱 내려다 보면 한국어 버젼이 있습니다.

    http://www.microsoft.com/downloads/details.aspx?familyid=38C46AA8-1DD7-426F-A913-4F370A65A582&displaylang=en

    + Recent posts