Register Register Member Login Member Login Member Login Forgot Password ??
PHP , ASP , ASP.NET, VB.NET, C#, Java , jQuery , Android , iOS , Windows Phone
 

Registered : 109,027

HOME > .NET Framework > Forum > สวัสดีครับ ช่วยดู โค้ดนี้ให้หน่อยครับ ผมอยากเปลี่ยนชื่อ column header เปลี่ยนไม่ได้ WPF C#



 

สวัสดีครับ ช่วยดู โค้ดนี้ให้หน่อยครับ ผมอยากเปลี่ยนชื่อ column header เปลี่ยนไม่ได้ WPF C#

 



Topic : 058401



โพสกระทู้ ( 36 )
บทความ ( 0 )



สถานะออฟไลน์




ผมเปลี่ยนชื่อ column headerได้นะครับ ไส่ code นี้ไป แต่ปัญหามันเกิดตรง ผมคลิกที่ cell ของ datagrid แต่ cell ที่ผมคลิกไม่ แสดงข้อมูลบน textbox ครับ ช่วยแก้ให้หน่อยครับ หรือแนะนำก็ได้ครับ
Code (C#)
                foreach (var cs in bt)
                {
                    dgBookTypeList.Columns[0].Header = "รหัสประเภทหนังสือ";
                    dgBookTypeList.Columns[1].Header = "ชื่อประเภทหนังสือ";
                }


ติดแค่เปลี่ยนชื่อ column header เท่านั้นเองครับ ก็เสร็จแล้ว ช่วยหน่อยนะแงบบบ

โค้ดทั้งหมด
Code (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Transactions;
using System.Windows.Controls.Primitives;
using System.Reflection;
using System.Diagnostics;

namespace Drewsn32Book.BookType
{
    /// <summary>
    /// Interaction logic for WinBookType.xaml
    /// </summary>
    public partial class BookType : Window
    {
        DataBase.dbDrewsn32BookDataContext db = new DataBase.dbDrewsn32BookDataContext();
        string[] tempdata = new string[2];
        public BookType()
        {
            InitializeComponent();
            //ผูกเหตุการณ์
            this.Loaded += new RoutedEventHandler(BookType_Loaded);
            cmdAdd.Click += new RoutedEventHandler(cmdAdd_Click);
            cmdEdit.Click += new RoutedEventHandler(cmdEdit_Click);
            dgBookTypeList.MouseLeftButtonUp += new MouseButtonEventHandler(dgBookTypeList_MouseLeftButtonUp);
            cmdRefresh.Click += new RoutedEventHandler(cmdRefresh_Click);
            cmdClear.Click += new RoutedEventHandler(cmdClear_Click);
            this.Closed += new EventHandler(BookType_Closed);
        }
        void BookType_Loaded(object sender, RoutedEventArgs e)
        {
            var bt = from b in db.BookTypes
                     orderby b.BookTypeID
                     select new { b.BookTypeID, b.BookTypeName };
            if (bt.Count() > 0)
            {
                dgBookTypeList.ItemsSource = bt.ToList();

            }
            else
            {
                dgBookTypeList.ItemsSource = null;
            }
        }
        void dgBookTypeList_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            #region Algorithum Check Row
            DependencyObject dep = (DependencyObject)e.OriginalSource;
            while ((dep != null) && !(dep is DataGridCell) && !(dep is DataGridColumnHeader))
            {
                dep = VisualTreeHelper.GetParent(dep);
            }

            if (dep == null)
                return;
            if (dep is DataGridColumn)
            {
                dgBookTypeList.Columns[0] = null;
            }
            if (dep is DataGridCell)
            {
                // navigate further up the tree
                while ((dep != null) && !(dep is DataGridRow))
                {
                    dep = VisualTreeHelper.GetParent(dep);
                }
                if (dep == null)
                    return;
                try
                {
                    int count = 0;
                    string data = "";
                    foreach (var dataGridCellInfo in dgBookTypeList.SelectedCells)
                    {
                        PropertyInfo pi = dataGridCellInfo.Item.GetType().GetProperty(Convert.ToString(dataGridCellInfo.Column.Header));
                        var temp = Convert.ToString(pi.GetValue(dataGridCellInfo.Item,null));
                        data += temp ;
                        tempdata[count] = Convert.ToString(temp);
                        count += +1;
                    }
                    txtBookTypeID.Text = tempdata[0].ToString();
                    txtBookTypeName.Text = tempdata[1].ToString();
                    txtBookTypeID.IsReadOnly = true;
                    txtBookTypeName.Focus();
                    txtBookTypeName.SelectAll();
                    cmdAdd.IsEnabled = false;
                }
                catch (Exception)
                {
                    return;
                }
            }
            #endregion
        }
        void cmdAdd_Click(object sender, RoutedEventArgs e)
        {
            var result = CheckBookTypeData();
            if (result == true)
            {
                if (MessageBox.Show("คุณต้องการเพิ่มรายชื่อจังหวัดใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    //สร้าง object b มาเพื่อเก็บค่าที่เรา พิมพ์ใน txt ไว้ก่อนเพื่อรอการทำ transaction
                    DataBase.BookType b = new DataBase.BookType();
                    b.BookTypeID = txtBookTypeID.Text.Trim();
                    b.BookTypeName = txtBookTypeName.Text.Trim();
                    try
                    {
                        // ใช้คำสั้่ง linq ดึงค่า booktype ใน db.booktypes 
                        var bt = (from bb in db.BookTypes
                                  where bb.BookTypeID == txtBookTypeID.Text.Trim()
                                  select bb).SingleOrDefault();
                        if (bt != null)
                        {
                            MessageBox.Show("รหัสจังหวัดที่คุณป้อน ซ้ำกับรหัสเดิมที่มีอยู่", "ข้อผิดพลาด", MessageBoxButton.OK, MessageBoxImage.Exclamation);
                            txtBookTypeID.Focus();
                            txtBookTypeID.SelectAll();
                        }
                        else
                        {
                            using (TransactionScope ts = new TransactionScope())
                            {
                                db.BookTypes.InsertOnSubmit(b);// object b ส่งค่าเข้าไปใน BookType
                                db.SubmitChanges();
                                ts.Complete();
                            }
                            ClearAllData();
                            ShowBookTypesList();//เรียกข้อมูลใหม่ที่เรา add เข้าไป มาโชว์แทนข้อมูลเดิม จาก db.BookType
                            //FormatDgvProvinceList();
                            MessageBox.Show("เพิ่มรายชื่อจังหวัดใหม่ เรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButton.OK, MessageBoxImage.Information);
                        }
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ข้อผิดพลาด");
                    }
                }
            }
            txtBookTypeID.Focus();
            txtBookTypeID.SelectAll();
        }
        void cmdEdit_Click(object sender, RoutedEventArgs e)
        {
            var result = CheckBookTypeData();
            if (result == true)
            {
                if (MessageBox.Show("คุณต้องการแก้ไขข้อมูลจังหวัดใหม่ ใช่หรือไม่?", "คำยืนยัน", MessageBoxButton.YesNo, MessageBoxImage.Question) == MessageBoxResult.Yes)
                {
                    try
                    {
                        using (var ts = new TransactionScope())
                        {
                            // var ps type var จะเก็บ ชนิดของสิ่งที่เก็บไว้ เช่น var ใน ps เก็บ class drewsn32.database.booktype
                            //สร้างตัวแปร id วน loop ตำแหน่งแรกใน db.BookTypes จนกว่า booktypeid == txtbooktypeid แล้วเก็บไว้ใน ps
                            var ps = db.BookTypes.First(id => id.BookTypeID == txtBookTypeID.Text.Trim());
                            ps.BookTypeName = txtBookTypeName.Text.Trim();//นำค่าใน txtbooktypename ไปไส่ใน booktypename ที่เก็บค่าของ db.booktype อยู่
                            ps.BookTypeID = txtBookTypeID.Text.Trim();
                            db.SubmitChanges();
                            ts.Complete();
                        }
                        ClearAllData();
                        ShowBookTypesList();
                        MessageBox.Show("แก้ไขข้อมูลจังหวัดใหม่ เรียบร้อยแล้ว", "ผลการทำงาน", MessageBoxButton.OK, MessageBoxImage.Information);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, "ข้อผิดพลาด");
                    }
                }
            }
        }
        void cmdRefresh_Click(object sender, RoutedEventArgs e)
        {
            ShowBookTypesList();
            txtBookTypeID.IsReadOnly = false;
            txtBookTypeID.Focus();
            cmdAdd.IsEnabled = true;
        }
        void cmdClear_Click(object sender, RoutedEventArgs e)
        {
            ClearAllData();
            txtBookTypeID.Focus();
        }
        void BookType_Closed(object sender, EventArgs e)
        {
            db.Connection.Close();// ถ้าปิด form ให้ ปิดการติดต่อกับ db
        }
        private bool CheckBookTypeData()
        {
            bool returnValue = false;
            if ((txtBookTypeID.Text.Trim() == string.Empty) || (txtBookTypeName.Text.Trim() == string.Empty))
            {
                MessageBox.Show("กรุณาป้อนข้อมูลจังหวัดให้ครบ !!!", "ข้อผิดพลาด", MessageBoxButton.OK, MessageBoxImage.Information);
                txtBookTypeID.Focus();
                returnValue = false;
            }
            else
            {
                returnValue = true;
            }
            return returnValue;
        }
        // อัพเดทข้อมูล นำข้อมูลใหม่ ที่เราอัพเดทมาโชว์ใน datagrid
        private void ShowBookTypesList()
        {
            var ps = (from p in db.BookTypes
                      select new
                      {
                          p.BookTypeID,
                          p.BookTypeName
                      }).OrderBy(o => o.BookTypeID);

            if (ps.Count() > 0)
            {
                dgBookTypeList.ItemsSource = ps.ToList();
                cmdEdit.IsEnabled = true;
            }
            else
            {
                dgBookTypeList.ItemsSource = null;
                cmdEdit.IsEnabled = false;
            }
        }
        private void ClearAllData()
        {
            cmdAdd.IsEnabled = true;
            txtBookTypeID.Text = string.Empty;
            txtBookTypeID.IsReadOnly = false;
            txtBookTypeID.Focus();
            txtBookTypeName.Text = string.Empty;
        }
    }
}





Tag : .NET, C#, VS 2010 (.NET 4.x)









ประวัติการแก้ไข
2011-04-07 00:50:35
2011-04-07 00:51:45
2011-04-07 00:54:03
2011-04-07 00:59:39
2011-04-07 01:00:20
2011-04-07 01:00:51
Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2011-04-07 00:48:34 By : Drewsn32 View : 1419 Reply : 1
 

 

No. 1



โพสกระทู้ ( 36 )
บทความ ( 0 )



สถานะออฟไลน์


ทำได้แล้วฮ่า ฮ่า สะใจโว้ยๆ

Code (C#)
        void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            var bt = from b in db.BookTypes
                     orderby b.BookTypeID
                     select new { b.BookTypeID, b.BookTypeName };
            if (bt.Count() > 0)
            {
                dgBookTypeList.ItemsSource = bt.ToList();
                dgBookTypeList.Columns[0].Header = "รหัสประเภทหนังสือ";
                dgBookTypeList.Columns[1].Header = "ชื่อประเภทหนังสือ";
            }
            else
            {
                dataGrid1.ItemsSource = null;
            }
        }




Code (C#)
                foreach (var dataGridCellInfo in dgBookTypeList.SelectedCells)
                {
                    /* pi = { BookTypeID: "Value", BookTypeName: "Value" } , row ที่ เมาส์ selectedCells จะได้ BookTypeID เป็นตำแหน่งที่ คลิกคลิกเมาส์*/
                    PropertyInfo pi = dataGridCellInfo.Item.GetType().GetProperty(FindBoundProperty(dataGridCellInfo.Column).ToString());
                    var temp = (pi.GetValue(dataGridCellInfo.Item, null));
                    data += temp;
                    tempdata[count] = Convert.ToString(temp);
                    count += +1;
                }




Code (C#)
        private string FindBoundProperty(DataGridColumn col)
        {
            DataGridBoundColumn boundColumn = col as DataGridBoundColumn;
            // find the property that this column is bound to
            Binding binding = boundColumn.Binding as Binding;
            string boundPropertyName = binding.Path.Path;
            return boundPropertyName;
        }









ประวัติการแก้ไข
2011-04-08 01:42:21
2011-04-08 01:43:27
2011-04-08 01:44:19
2011-04-08 01:58:57
2011-04-08 01:59:25
2011-04-08 02:10:16
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2011-04-08 01:40:51 By : Drewsn32
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : สวัสดีครับ ช่วยดู โค้ดนี้ให้หน่อยครับ ผมอยากเปลี่ยนชื่อ column header เปลี่ยนไม่ได้ WPF C#
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ FTP| ใส่แถวของตาราง ใส่คอลัมน์ตาราง| ตัวยก ตัวห้อย ตัวพิมพ์ดีด| ใส่โค้ด ใส่การอ้างถึงคำพูด| ใส่ลีสต์
smiley for :lol: smiley for :ken: smiley for :D smiley for :) smiley for ;) smiley for :eek: smiley for :geek: smiley for :roll: smiley for :erm: smiley for :cool: smiley for :blank: smiley for :idea: smiley for :ehh: smiley for :aargh: smiley for :evil:
Insert PHP Code
Insert ASP Code
Insert VB.NET Code Insert C#.NET Code Insert JavaScript Code Insert C#.NET Code
Insert Java Code
Insert Android Code
Insert Objective-C Code
Insert XML Code
Insert SQL Code
Insert Code
เพื่อความเรียบร้อยของข้อความ ควรจัดรูปแบบให้พอดีกับขนาดของหน้าจอ เพื่อง่ายต่อการอ่านและสบายตา และตรวจสอบภาษาไทยให้ถูกต้อง

อัพโหลดแทรกรูปภาพ

Notice

เพื่อความปลอดภัยของเว็บบอร์ด ไม่อนุญาติให้แทรก แท็ก [img]....[/img] โดยการอัพโหลดไฟล์รูปจากที่อื่น เช่นเว็บไซต์ ฟรีอัพโหลดต่าง ๆ
อัพโหลดแทรกรูปภาพ ให้ใช้บริการอัพโหลดไฟล์ของไทยครีเอท และตัดรูปภาพให้พอดีกับสกรีน เพื่อความโหลดเร็วและไฟล์ไม่ถูกลบทิ้ง

   
  เพื่อความปลอดภัยและการตรวจสอบ กระทู้ที่แทรกไฟล์อัพโหลดไฟล์จากที่อื่น อาจจะถูกลบทิ้ง
 
โดย
อีเมล์
บวกค่าให้ถูก
<= ตัวเลขฮินดูอารบิก เช่น 123 (หรือล็อกอินเข้าระบบสมาชิกเพื่อไม่ต้องกรอก)







Exchange: นำเข้าสินค้าจากจีน, Taobao, เฟอร์นิเจอร์, ของพรีเมี่ยม, ร่ม, ปากกา, power bank, แฟลชไดร์ฟ, กระบอกน้ำ

Load balance : Server 04
ThaiCreate.Com Logo
© www.ThaiCreate.Com. 2003-2024 All Rights Reserved.
ไทยครีเอทบริการ จัดทำดูแลแก้ไข Web Application ทุกรูปแบบ (PHP, .Net Application, VB.Net, C#)
[Conditions Privacy Statement] ติดต่อโฆษณา 081-987-6107 อัตราราคา คลิกที่นี่