■ GridControl 클래스 : 디폴트 값으로 신규 항목 행 초기화 하기

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


TestProject.zip NORTHWND.zip


MainWindow.xaml

 

 

<Window x:Class="TestProject.MainWindow"

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

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

    xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"

    Width="800"

    Height="600"

    Title="디폴트 값으로 신규 항목 행 초기화 하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid>

        <dxg:GridControl x:Name="gridControl"

            AutoGenerateColumns="AddNew">

            <dxg:GridControl.View>

                <dxg:TableView x:Name="tableView"

                    NewItemRowPosition="Top"

                    InitNewRow="tableView_InitNewRow"

                    ValidateRow="tableView_ValidateRow"

                    InvalidRowException="tableView_InvalidRowException" />

            </dxg:GridControl.View>

        </dxg:GridControl>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Windows;

 

using DevExpress.Xpf.Grid;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.gridControl.ItemsSource = new NorthwindEntities().Products;

        }

 

        #endregion

 

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

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

 

        #region 테이블 뷰 신규 행 초기화시 처리하기 - tableView_InitNewRow(sender, e)

 

        /// <summary>

        /// 테이블 뷰 신규 행 초기화시 처리하기

        /// </summary>

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

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

        private void tableView_InitNewRow(object sender, InitNewRowEventArgs e)

        {

            this.gridControl.SetCellValue(e.RowHandle, "UnitPrice"   , 10   );

            this.gridControl.SetCellValue(e.RowHandle, "Discontinued", false);

            this.gridControl.SetCellValue(e.RowHandle, "UnitsOnOrder", 1    );

        }

 

        #endregion

        #region 테이블 뷰 행 무결성 조사시 처리하기 - tableView_ValidateRow(sender, e)

 

        /// <summary>

        /// 테이블 뷰 행 무결성 조사시 처리하기

        /// </summary>

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

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

        private void tableView_ValidateRow(object sender, GridRowValidationEventArgs e)

        {

            if(e.Row == null)

            {

                return;

            }

 

            if(e.RowHandle == GridControl.NewItemRowHandle)

            {

                e.IsValid =  !string.IsNullOrEmpty((e.Row as Product).ProductName);

            }

        }

 

        #endregion

        #region 테이블 뷰 무효한 행 예외 처리하기 - tableView_InvalidRowException(sender, e)

 

        /// <summary>

        /// 테이블 뷰 무효한 행 예외 처리하기

        /// </summary>

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

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

        private void tableView_InvalidRowException(object sender, InvalidRowExceptionEventArgs e)

        {

            if(e.RowHandle == GridControl.NewItemRowHandle)

            {

                e.ErrorText     = "Please enter the Product name. ";

                e.WindowCaption = "Input Error";

 

                this.gridControl.CurrentColumn = this.gridControl.Columns["ProductName"];

            }

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요