첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ DataGridControl 엘리먼트 : 선택 항목 삭제하기

----------------------------------------------------------------------------------------------------

MainApplication.xaml

 

 

<Application

    x:Class="TestProject.MainApplication"

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    StartupUri="MainWindow.xaml">

</Application>

 

 

MainApplication.xaml.cs

 

 

using System;

using System.Data;

using System.Data.OleDb;

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 애플리케이션

    /// </summary>

    public partial class MainApplication : Application

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Field

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Private

 

        #region Field

 

        /// <summary>

        /// 주문 테이블

        /// </summary>

        private static DataTable _orderTable;

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Static

        //////////////////////////////////////////////////////////////////////////////// Public

 

        #region 주문 테이블 - OrderTable

 

        /// <summary>

        /// 주문 테이블

        /// </summary>

        public static DataTable OrderTable

        {

            get

            {

                return _orderTable;

            }

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Property

        ////////////////////////////////////////////////////////////////////////////////////////// Static

 

        #region 생성자 - MainApplication()

 

        /// <summary>

        /// 생성자

        /// </summary>

        static MainApplication()

        {

            DataSet dataSet = new DataSet();

 

            string           filePath         = @"c:\DS.Core\DS.Test.SampleDatabase\nwind.mdb";

            string           connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0;Data Source={0}", filePath);

            OleDbConnection  oleDbConnection  = new OleDbConnection(connectionString);

            OleDbDataAdapter oleDbDataAdapter = new OleDbDataAdapter();

 

            oleDbDataAdapter.SelectCommand = new OleDbCommand("SELECT * FROM Orders", oleDbConnection);

 

            oleDbDataAdapter.Fill(dataSet, "Orders");

 

            _orderTable = dataSet.Tables["Orders"];

        }

 

        #endregion

    }

}

 

 

MainWindow.xaml

 

 

<Window

    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

    xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid"

    x:Class="TestProject.MainWindow"

    Width="600"

    Height="450"

    Title="선택 항목 삭제하기">

    <Window.Resources>      

        <xcdg:DataGridCollectionViewSource x:Key="DataGridCollectionViewSourceKey"

            Source="{Binding Source={x:Static Application.Current}, Path=OrderTable}" />

    </Window.Resources>

    <Grid>

        <xcdg:DataGridControl x:Name="dataGridControl"

            ItemsSource="{Binding Source={StaticResource DataGridCollectionViewSourceKey}}"

            IsDeleteCommandEnabled="True"

            DeletingSelectedItemError="dataGridControl_DeletingSelectedItemError"

            DeletingSelectedItems="dataGridControl_DeletingSelectedItems" />

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

 

using Xceed.Wpf.DataGrid;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

        ////////////////////////////////////////////////////////////////////////////////////////// Public

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method

        ////////////////////////////////////////////////////////////////////////////////////////// Private

        //////////////////////////////////////////////////////////////////////////////// Event

 

        #region 데이터 그리드 컨트롤 선택 항목 삭제 에러시 처리하기 - dataGridControl_DeletingSelectedItemError(sender, e)

 

        /// <summary>

        /// 데이터 그리드 컨트롤 선택 항목 삭제 에러시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void dataGridControl_DeletingSelectedItemError(object sender, DeletingSelectedItemErrorRoutedEventArgs e)

        {    

            MessageBoxResult result = MessageBox.Show("항목 삭제시 다음 에러가 발생했습니다 : " + e.Exception.Message +

                "\n작업을 계속하시겠습니까?", "에러", MessageBoxButton.YesNoCancel);

 

            if(result == MessageBoxResult.Yes)

            {

                if(this.dataGridControl.IsBeingEdited)

                {

                    try

                    {

                        this.dataGridControl.CancelEdit();

 

                        e.Action = DeletingSelectedItemErrorAction.Retry;

                    }

                    catch

                    {

                        e.Action = DeletingSelectedItemErrorAction.Skip;

                    }

                }

            }

            else if(result == MessageBoxResult.Cancel)

            {

                e.Action = DeletingSelectedItemErrorAction.Abort;

            }

        }

 

        #endregion

 

        #region 데이터 그리드 컨트롤 선택 항목 삭제시 처리하기 - dataGridControl_DeletingSelectedItems(sender, e)

 

        /// <summary>

        /// 데이터 그리드 컨트롤 선택 항목 삭제시 처리하기

        /// </summary>

        /// <param name="sender">이벤트 발생자</param>

        /// <param name="e">이벤트 인자</param>

        private void dataGridControl_DeletingSelectedItems(object sender, CancelRoutedEventArgs e)

        {

            MessageBoxResult result = MessageBox.Show("선택 행을 삭제하시겠습니까?", "확인", MessageBoxButton.YesNo);

 

            if(result == MessageBoxResult.No)

            {

                e.Cancel = true;

            }

        }

 

        #endregion

    }

}

 

----------------------------------------------------------------------------------------------------

※ MS Access 파일 처리를 위해 플랫폼 대상x86으로 컴파일해야 한다.

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요