■ DataGrid 클래스 : DataTable 바인딩 하기

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


TestProject.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"

    Width="800"

    Height="600"

    Title="DataGrid 클래스 : DataTable 바인딩 하기"

    FontFamily="나눔고딕코딩"

    FontSize="16">

    <Grid Margin="10">

        <DataGrid Name="dataGrid"

            AutoGenerateColumns="False"

            SelectionMode="Single"

            CanUserAddRows="false"

            AlternationCount="2"

            AlternatingRowBackground="LightGray">

            <DataGrid.Columns>

                <DataGridTextColumn

                    Binding="{Binding ID}"

                    Header="ID"

                    Width="50"

                    IsReadOnly="True" />

                <DataGridTextColumn

                    Binding="{Binding Name}"

                    Header="성명"

                    Width="100" />

                <DataGridTextColumn

                    Binding="{Binding Hobby}"

                    Header="취미"

                    Width="100" />

                <DataGridTemplateColumn

                    Header="주소"

                    Width="200">

                    <DataGridTemplateColumn.CellTemplate>

                        <DataTemplate>

                            <Grid>

                                <Grid.ColumnDefinitions>

                                    <ColumnDefinition Width="*" />

                                    <ColumnDefinition Width="20" />

                                </Grid.ColumnDefinitions>

                                <TextBox Grid.Column="0"

                                    HorizontalAlignment="Stretch"

                                    MinWidth="80"

                                    Background="{Binding Path=Background, RelativeSource={RelativeSource AncestorType=DataGridRow}}"

                                    Text="{Binding Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />

                                <Button Grid.Column="1"

                                    HorizontalAlignment="Center"

                                    VerticalAlignment="Center"

                                    Width="20"

                                    Height="20"

                                    Content="변경"

                                    Click="changeButton_Click" />

                            </Grid>

                        </DataTemplate>

                    </DataGridTemplateColumn.CellTemplate>

                </DataGridTemplateColumn>

            </DataGrid.Columns>

        </DataGrid>

    </Grid>

</Window>

 

 

MainWindow.xaml.cs

 

 

using System.Data;

using System.Windows;

 

namespace TestProject

{

    /// <summary>

    /// 메인 윈도우

    /// </summary>

    public partial class MainWindow : Window

    {

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

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

 

        #region Field

 

        /// <summary>

        /// 데이터 테이블

        /// </summary>

        private DataTable table;

 

        #endregion

 

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

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

 

        #region 생성자 - MainWindow()

 

        /// <summary>

        /// 생성자

        /// </summary>

        public MainWindow()

        {

            InitializeComponent();

 

            this.table = GetTable();

 

            this.dataGrid.ItemsSource = this.table.DefaultView;

        }

 

        #endregion

 

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

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

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

 

        #region 변경 버튼 클릭시 처리하기 - changeButton_Click(sender, e)

 

        /// <summary>

        /// 변경 버튼 클릭시 처리하기

        /// </summary>

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

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

        private void changeButton_Click(object sender, RoutedEventArgs e)

        {

            MessageBox.Show(this.table.Rows[dataGrid.SelectedIndex]["Address"].ToString());

        }

 

        #endregion

 

        //////////////////////////////////////////////////////////////////////////////// Function

 

        #region 데이터 테이블 구하기 - GetTable()

 

        /// <summary>

        /// 데이터 테이블 구하기

        /// </summary>

        /// <returns>데이터 테이블</returns>

        private DataTable GetTable()

        {

            DataTable table = new DataTable();

 

            table.Columns.Add("ID"     );

            table.Columns.Add("Name"   );

            table.Columns.Add("Hobby"  );

            table.Columns.Add("Address");

 

            table.Rows.Add("1", "홍길동", "등산", "서울시 마포구");

            table.Rows.Add("2", "김철수", "축구", "경기도 성남시");

 

            return table;

        }

 

        #endregion

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요