001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Text;
005.
using
System.Windows;
006.
using
System.Windows.Controls;
007.
using
System.Windows.Data;
008.
using
System.Windows.Documents;
009.
using
System.Windows.Input;
010.
using
System.Windows.Media;
011.
using
System.Windows.Media.Imaging;
012.
using
System.Windows.Shapes;
013.
using
System.Transactions;
014.
using
System.Windows.Controls.Primitives;
015.
using
System.Reflection;
016.
using
System.Diagnostics;
017.
018.
namespace
Drewsn32Book.BookType
019.
{
020.
/// <summary>
021.
/// Interaction logic for WinBookType.xaml
022.
/// </summary>
023.
public
partial
class
BookType : Window
024.
{
025.
DataBase.dbDrewsn32BookDataContext db =
new
DataBase.dbDrewsn32BookDataContext();
026.
string
[] tempdata =
new
string
[2];
027.
public
BookType()
028.
{
029.
InitializeComponent();
030.
031.
this
.Loaded +=
new
RoutedEventHandler(BookType_Loaded);
032.
cmdAdd.Click +=
new
RoutedEventHandler(cmdAdd_Click);
033.
cmdEdit.Click +=
new
RoutedEventHandler(cmdEdit_Click);
034.
dgBookTypeList.MouseLeftButtonUp +=
new
MouseButtonEventHandler(dgBookTypeList_MouseLeftButtonUp);
035.
cmdRefresh.Click +=
new
RoutedEventHandler(cmdRefresh_Click);
036.
cmdClear.Click +=
new
RoutedEventHandler(cmdClear_Click);
037.
this
.Closed +=
new
EventHandler(BookType_Closed);
038.
}
039.
void
BookType_Loaded(
object
sender, RoutedEventArgs e)
040.
{
041.
var bt = from b
in
db.BookTypes
042.
orderby b.BookTypeID
043.
select
new
{ b.BookTypeID, b.BookTypeName };
044.
if
(bt.Count() > 0)
045.
{
046.
dgBookTypeList.ItemsSource = bt.ToList();
047.
048.
}
049.
else
050.
{
051.
dgBookTypeList.ItemsSource =
null
;
052.
}
053.
}
054.
void
dgBookTypeList_MouseLeftButtonUp(
object
sender, MouseButtonEventArgs e)
055.
{
056.
#region Algorithum Check Row
057.
DependencyObject dep = (DependencyObject)e.OriginalSource;
058.
while
((dep !=
null
) && !(dep
is
DataGridCell) && !(dep
is
DataGridColumnHeader))
059.
{
060.
dep = VisualTreeHelper.GetParent(dep);
061.
}
062.
063.
if
(dep ==
null
)
064.
return
;
065.
if
(dep
is
DataGridColumn)
066.
{
067.
dgBookTypeList.Columns[0] =
null
;
068.
}
069.
if
(dep
is
DataGridCell)
070.
{
071.
072.
while
((dep !=
null
) && !(dep
is
DataGridRow))
073.
{
074.
dep = VisualTreeHelper.GetParent(dep);
075.
}
076.
if
(dep ==
null
)
077.
return
;
078.
try
079.
{
080.
int
count = 0;
081.
string
data =
""
;
082.
foreach
(var dataGridCellInfo
in
dgBookTypeList.SelectedCells)
083.
{
084.
PropertyInfo pi = dataGridCellInfo.Item.GetType().GetProperty(Convert.ToString(dataGridCellInfo.Column.Header));
085.
var temp = Convert.ToString(pi.GetValue(dataGridCellInfo.Item,
null
));
086.
data += temp ;
087.
tempdata[count] = Convert.ToString(temp);
088.
count += +1;
089.
}
090.
txtBookTypeID.Text = tempdata[0].ToString();
091.
txtBookTypeName.Text = tempdata[1].ToString();
092.
txtBookTypeID.IsReadOnly =
true
;
093.
txtBookTypeName.Focus();
094.
txtBookTypeName.SelectAll();
095.
cmdAdd.IsEnabled =
false
;
096.
}
097.
catch
(Exception)
098.
{
099.
return
;
100.
}
101.
}
102.
#endregion
103.
}
104.
void
cmdAdd_Click(
object
sender, RoutedEventArgs e)
105.
{
106.
var result = CheckBookTypeData();
107.
if
(result ==
true
)
108.
{
109.
if
(MessageBox.Show(
"คุณต้องการเพิ่มรายชื่อจังหวัดใหม่ ใช่หรือไม่?"
,
"คำยืนยัน"
, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
110.
{
111.
112.
DataBase.BookType b =
new
DataBase.BookType();
113.
b.BookTypeID = txtBookTypeID.Text.Trim();
114.
b.BookTypeName = txtBookTypeName.Text.Trim();
115.
try
116.
{
117.
118.
var bt = (from bb
in
db.BookTypes
119.
where bb.BookTypeID == txtBookTypeID.Text.Trim()
120.
select bb).SingleOrDefault();
121.
if
(bt !=
null
)
122.
{
123.
MessageBox.Show(
"รหัสจังหวัดที่คุณป้อน ซ้ำกับรหัสเดิมที่มีอยู่"
,
"ข้อผิดพลาด"
, MessageBoxButton.OK, MessageBoxImage.Exclamation);
124.
txtBookTypeID.Focus();
125.
txtBookTypeID.SelectAll();
126.
}
127.
else
128.
{
129.
using
(TransactionScope ts =
new
TransactionScope())
130.
{
131.
db.BookTypes.InsertOnSubmit(b);
132.
db.SubmitChanges();
133.
ts.Complete();
134.
}
135.
ClearAllData();
136.
ShowBookTypesList();
137.
138.
MessageBox.Show(
"เพิ่มรายชื่อจังหวัดใหม่ เรียบร้อยแล้ว"
,
"ผลการทำงาน"
, MessageBoxButton.OK, MessageBoxImage.Information);
139.
}
140.
}
141.
catch
(Exception ex)
142.
{
143.
MessageBox.Show(ex.Message,
"ข้อผิดพลาด"
);
144.
}
145.
}
146.
}
147.
txtBookTypeID.Focus();
148.
txtBookTypeID.SelectAll();
149.
}
150.
void
cmdEdit_Click(
object
sender, RoutedEventArgs e)
151.
{
152.
var result = CheckBookTypeData();
153.
if
(result ==
true
)
154.
{
155.
if
(MessageBox.Show(
"คุณต้องการแก้ไขข้อมูลจังหวัดใหม่ ใช่หรือไม่?"
,
"คำยืนยัน"
, MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
156.
{
157.
try
158.
{
159.
using
(var ts =
new
TransactionScope())
160.
{
161.
162.
163.
var ps = db.BookTypes.First(id => id.BookTypeID == txtBookTypeID.Text.Trim());
164.
ps.BookTypeName = txtBookTypeName.Text.Trim();
165.
ps.BookTypeID = txtBookTypeID.Text.Trim();
166.
db.SubmitChanges();
167.
ts.Complete();
168.
}
169.
ClearAllData();
170.
ShowBookTypesList();
171.
MessageBox.Show(
"แก้ไขข้อมูลจังหวัดใหม่ เรียบร้อยแล้ว"
,
"ผลการทำงาน"
, MessageBoxButton.OK, MessageBoxImage.Information);
172.
}
173.
catch
(Exception ex)
174.
{
175.
MessageBox.Show(ex.Message,
"ข้อผิดพลาด"
);
176.
}
177.
}
178.
}
179.
}
180.
void
cmdRefresh_Click(
object
sender, RoutedEventArgs e)
181.
{
182.
ShowBookTypesList();
183.
txtBookTypeID.IsReadOnly =
false
;
184.
txtBookTypeID.Focus();
185.
cmdAdd.IsEnabled =
true
;
186.
}
187.
void
cmdClear_Click(
object
sender, RoutedEventArgs e)
188.
{
189.
ClearAllData();
190.
txtBookTypeID.Focus();
191.
}
192.
void
BookType_Closed(
object
sender, EventArgs e)
193.
{
194.
db.Connection.Close();
195.
}
196.
private
bool
CheckBookTypeData()
197.
{
198.
bool
returnValue =
false
;
199.
if
((txtBookTypeID.Text.Trim() ==
string
.Empty) || (txtBookTypeName.Text.Trim() ==
string
.Empty))
200.
{
201.
MessageBox.Show(
"กรุณาป้อนข้อมูลจังหวัดให้ครบ !!!"
,
"ข้อผิดพลาด"
, MessageBoxButton.OK, MessageBoxImage.Information);
202.
txtBookTypeID.Focus();
203.
returnValue =
false
;
204.
}
205.
else
206.
{
207.
returnValue =
true
;
208.
}
209.
return
returnValue;
210.
}
211.
212.
private
void
ShowBookTypesList()
213.
{
214.
var ps = (from p
in
db.BookTypes
215.
select
new
216.
{
217.
p.BookTypeID,
218.
p.BookTypeName
219.
}).OrderBy(o => o.BookTypeID);
220.
221.
if
(ps.Count() > 0)
222.
{
223.
dgBookTypeList.ItemsSource = ps.ToList();
224.
cmdEdit.IsEnabled =
true
;
225.
}
226.
else
227.
{
228.
dgBookTypeList.ItemsSource =
null
;
229.
cmdEdit.IsEnabled =
false
;
230.
}
231.
}
232.
private
void
ClearAllData()
233.
{
234.
cmdAdd.IsEnabled =
true
;
235.
txtBookTypeID.Text =
string
.Empty;
236.
txtBookTypeID.IsReadOnly =
false
;
237.
txtBookTypeID.Focus();
238.
txtBookTypeName.Text =
string
.Empty;
239.
}
240.
}
241.
}