1. DevExpress DataGrid Master-Detail Mode
- 스크립트(Script)를 통한 DataGrid Master-Detail 만들기입니다.
- DB를 접속해서 자료를 읽어와 자료를 조회하는 방식중 Master-Detail 적용입니다.
(1) 소스
using System.Data;
using System.Data.OleDb;
using DevExpress.Utils;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Card;
namespace DXDataGridMasterDetailApp {
public partial class Form1 : DevExpress.XtraEditors.XtraForm {
public Form1() {
InitializeComponent();
// Creates a connection to the Nwind database.
OleDbConnection connection = new OleDbConnection(
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\Users\\Public\\Documents\\DevExpress Demos 22.2\\Components\\Data\\nwind.mdb");
// --------------------- (1)
// Creates the data adapters to retrieve data from the Categories and Products data tables.
OleDbDataAdapter adapterCategories = new OleDbDataAdapter(
"SELECT CategoryID, CategoryName, Picture FROM Categories", connection);
OleDbDataAdapter adapterProducts = new OleDbDataAdapter(
"SELECT CategoryID, ProductID, ProductName, UnitPrice FROM Products", connection);
// --------------------- (2)
DataSet dataSetNwind = new DataSet();
// Creates DataTable objects that correspond to database tables.
adapterCategories.Fill(dataSetNwind, "Categories");
adapterProducts.Fill(dataSetNwind, "Products");
// --------------------- (3)
// Sets up a master-detail relationship between data tables.
DataColumn keyColumn = dataSetNwind.Tables["Categories"].Columns["CategoryID"];
DataColumn foreignKeyColumn = dataSetNwind.Tables["Products"].Columns["CategoryID"];
dataSetNwind.Relations.Add("CategoriesProducts", keyColumn, foreignKeyColumn);
// --------------------- (4)
// Binds the Data Grid to a data source.
gridControl1.DataSource = dataSetNwind.Tables["Categories"];
// Forces the Data Grid to initialize its settings.
gridControl1.ForceInitialize();
// --------------------- (5)
// Creates a pattern view (CardView) to display detail data.
CardView cardViewProducts = new CardView(gridControl1);
gridControl1.LevelTree.Nodes.Add("CategoriesProducts", cardViewProducts);
// Specifies the detail view's caption (the caption of detail tabs).
cardViewProducts.ViewCaption = "Category Products";
// --------------------- (6)
// Hides the CategoryID column from the master view.
gridView1.Columns["CategoryID"].VisibleIndex = -1;
// --------------------- (7)
/* Creates a Picture Edit repository item to display images in the Picture column,
* adds it to the Data Grid's RepositoryItems collection, and sets up image settings. */
RepositoryItemPictureEdit riPictureEdit = gridControl1.RepositoryItems.Add("PictureEdit") as RepositoryItemPictureEdit;
gridView1.Columns["Picture"].ColumnEdit = riPictureEdit;
riPictureEdit.SizeMode = DevExpress.XtraEditors.Controls.PictureSizeMode.Stretch;
// Specifies the width of the Picture column.
gridView1.Columns["Picture"].Width = 250;
gridView1.Columns["Picture"].OptionsColumn.FixedWidth = true;
// --------------------- (8)
// Enables automatic height for master rows.
gridView1.OptionsView.RowAutoHeight = true;
// --------------------- (9)
// Creates columns in a detail pattern view (CardView) for all fields in the Products table.
cardViewProducts.PopulateColumns(dataSetNwind.Tables["Products"]);
// Hides the CategoryID column.
cardViewProducts.Columns["CategoryID"].VisibleIndex = -1;
// Formats cell values in the UnitPrice column as currency.
cardViewProducts.Columns["UnitPrice"].DisplayFormat.FormatType = FormatType.Numeric;
cardViewProducts.Columns["UnitPrice"].DisplayFormat.FormatString = "c2";
}
}
}
(2) 결과화면
2. 프로그램 설명
(1) DB 자료 Select
>> DataTable를 사용하여 Select를 할때
DataTable retData1 = sqlHelper("SELECT CategoryID, CategoryName, Picture FROM Categories");
DataTable retData2 = sqlHelper( "SELECT CategoryID, ProductID, ProductName, UnitPrice FROM Products" );
retData1.Name = "Categories" ;
retData2.Name = "Products" ;
(2) DataSet 테이블 자료등록 및 연결
>> DataSet를 DatatTable 변경시
DataSet dataSetNwind = new DataSet();
dataSetNwind.Add(retData1);
dataSetNwind.Add(retData2);
(3) DataSet 테이블 마스터와 디테일의 연결키 매칭작업 및 선언
(4) DataContral DataSource DB DataSet 연결
(5) DataGrid Detail 처리
- 카드형 : CardView
(6) GridView 첫번째 컬럽 않보이게 처리
- 그리드형 : GridView
- 예)
gridView1.Columns["CategoryID"].Visible = false;
(7) 그리드뷰에 Image 적용
(8) Row 높이 자동처리
(9) Detail 출력 양식 변경 및 편집
'개발(IT) > DevExpress(WinForm)' 카테고리의 다른 글
DevExpress DataGrid Master-Detail Scripts (Examples #3) (0) | 2024.04.10 |
---|---|
DevExpress DataGrid Master-Detail Scripts (Examples #2) (0) | 2024.04.10 |
DevExpress 문서파일 분리작업 (0) | 2023.12.01 |
DevExpress Excel 열기 (0) | 2023.12.01 |
DevExpress Excel 열기/저장 #1 (0) | 2023.12.01 |