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 > C# WinApp สอบถามเรื่องการทำ HeaderCheckBox ใน DataGridView ครับ



 

C# WinApp สอบถามเรื่องการทำ HeaderCheckBox ใน DataGridView ครับ

 



Topic : 127042



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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



โค้ดจาก

https://www.codeproject.com/kb/grid/selectall.aspx

และอีะหลายๆที่ก็มาทำนองเดียวกัน

มีปัญหาตอนใช้ scrolbar ครับ

11

22



Tag : .NET, Win (Windows App), C#, VS 2012 (.NET 4.x), Windows







Move To Hilight (Stock) 
Send To Friend.Bookmark.
Date : 2017-04-01 11:40:23 By : lamaka.tor View : 1221 Reply : 17
 

 

No. 1



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



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


yyyyy

ลองทำดูก็น่าจะได้น่ะครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Grid_CheckBox
{
    public partial class Form1 : Form
    {
        CheckBox chkAll = new CheckBox();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void AddCheckAll()
        {
            Rectangle rect;
            try
            {
                rect = new Rectangle();
                chkAll.Size = new Size(14, 14);
                rect.X = dataGridView1.RowHeadersWidth + (dataGridView1.Columns[0].Width/2)-6;
                rect.Y = 5;
                chkAll.Location = rect.Location;
                chkAll.CheckedChanged += new System.EventHandler(new EventHandler(ckBox_CheckedChanged));
                dataGridView1.Controls.Add(chkAll);
            }
            catch (Exception ex)
            {
                throw (new ApplicationException(System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message));
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i;
            DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
            DataGridViewCheckBoxColumn chk1 = new DataGridViewCheckBoxColumn();
            
            chk1.Name = "chk1";
            col1.Name = "col1";
            col2.Name = "col2";
            chk1.HeaderText = "";
            col1.HeaderText = "col1";
            col2.HeaderText = "col2";

            dataGridView1.Columns.Add(chk1);
            dataGridView1.Columns.Add(col1);
            dataGridView1.Columns.Add(col2);
            dataGridView1.AllowUserToAddRows = false;
            AddCheckAll();

            for (i = 0; i <= 100; i++)

            {
                dataGridView1.Rows.Add(new object[] {0, i,"Test" });
            }
        }

        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {

            foreach (DataGridViewRow  dr in dataGridView1.Rows)
                {
                    switch (chkAll.Checked)
                    {
                        case true:
                            dr.Cells[0].Value= 1;
                            break;
                        case false:
                            dr.Cells[0].Value = 0;
                            break;
                        default:
                            dr.Cells[0].Value = 0;
                            break;
                    }
                }
            dataGridView1.CurrentCell = null;
        }
    }
}








แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-01 16:31:37 By : fonfire
 


 

No. 2



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

ตอบความคิดเห็นที่ : 1 เขียนโดย : fonfire เมื่อวันที่ 2017-04-01 16:31:37
รายละเอียดของการตอบ ::
ลอง scrolbar ซ้ายขวาดูนะครับ

Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//https://www.thaicreate.com/dotnet/forum/127042.html
namespace TestCSharp.testHeaderCheckBox
{
    public partial class Form5 : Form
    {
        public Form5()
        {
            InitializeComponent();
        }
        CheckBox chkAll = new CheckBox();
        private void Form5_Load(object sender, EventArgs e)
        {
            dataGridView1.AllowUserToAddRows = false;
            AddCheckAll();
            for (int i = 0; i <= 100; i++)
            {
                dataGridView1.Rows.Add(  null, "Test" + i );
            }
        }
        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {

            foreach (DataGridViewRow dr in dataGridView1.Rows)
            {
                switch (chkAll.Checked)
                {
                    case true:
                        dr.Cells[0].Value = 1;
                        break;
                    case false:
                        dr.Cells[0].Value = 0;
                        break;
                    default:
                        dr.Cells[0].Value = 0;
                        break;
                }
            }
            dataGridView1.CurrentCell = null;
        }
        public void AddCheckAll()
        {
            Rectangle rect;
            try
            {
                rect = new Rectangle();
                chkAll.Size = new Size(14, 14);
                rect.X = dataGridView1.RowHeadersWidth + (dataGridView1.Columns[0].Width / 2) - 6;
                rect.Y = 5;
                chkAll.Location = rect.Location;
                chkAll.CheckedChanged += new System.EventHandler(new EventHandler(ckBox_CheckedChanged));
                dataGridView1.Controls.Add(chkAll);
            }
            catch (Exception ex)
            {
                throw (new ApplicationException(System.Reflection.MethodBase.GetCurrentMethod().Name + " : " + ex.Message));
            }
        }
    }
}



11

13

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-01 18:54:21 By : lamaka.tor
 

 

No. 3

Guest


ส่วนใหญ่ทุกฯ สมรภูมิ (บางอณาจักร)
--- ผมคือพลทหารโปรแกรมเมอร์ รางวัลที่ได้คือ "ความตาย" นับว่าเป็นเกียริตยศ อันสูงส่ง
(...)

แต่ในความเป็นจริง ผมก็มีอณาจักรเล็กฯ (แน่นอนว่านับไพร่พล) ผมไม่อาจจะสู้ได้

...
...
...
...



ผมเข้าใจในคำว่า "คุณธรรม"


ปล. ผมคิดแค่เล็กฯน้อยฯ (เจ้าแคว้นใหญ่) สะเทือน +55555
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-01 20:05:48 By : หน้าฮี
 


 

No. 4

Guest


@TOR_CHEMISTRY

คนฯหนึ่งทำให้อีกคนหนึ่งลังเลได้
--- นับว่าเป็นยอดคน


ปล. ไม่พูดเขายังไม่รู้ว่าเราคิดอย่างไร (พูดแล้วทำให้คนอื่นฯรู้ว่าเราโง่จริงฯและไม่ได้แกล้งโง่)

แต่เมื่อไหร่ก็ตามที่เราพูด/แสดงออกไป และทำให้เขารู้สึกว่า "เราไม่ฉลาดเหมือนเดิม" อันนี้เป็นเรื่องใหญ่
แต่มันอาจจะเล็กก็ได้ (ผมคาดเดาได้แต่ผมไม่สามารถรับรองผลลัพธ์ของการเดาได้ 100%)

...
...
...
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-01 20:12:19 By : หน้าฮี
 


 

No. 5



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-01 21:25:19 By : lamaka.tor
 


 

No. 6



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



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


ลองดูอันนี้ครับ
้เห็นมีย้ายตำแหน่งด้วย

http://csharp.ihavenomoney.co.kr/?p=290
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 09:08:12 By : fonfire
 


 

No. 7



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

ตอบความคิดเห็นที่ : 6 เขียนโดย : fonfire เมื่อวันที่ 2017-04-03 09:08:12
รายละเอียดของการตอบ ::
ขอบคุณมากครับ
รันเครื่องแป๊บ เดี๋ยวจะมาลองดูครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 09:47:23 By : lamaka.tor
 


 

No. 8



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

ตอบความคิดเห็นที่ : 6 เขียนโดย : fonfire เมื่อวันที่ 2017-04-03 09:08:12
รายละเอียดของการตอบ ::
ทดสอบเรียบร้อย อาการเดิมครับ
แต่ก็พอได้แนวทางแล้วครับ

เมื่อมีการลาก scollbar ก็ให้ checkbox เลื่อนตามไปด้วย

กับอีกวิธีหนึ่งคับ ยัด checkbox ใส่ใน header โดยไม่ต้องผ่าน datagrid ซึ่งเป็นวิธีที่ยากกว่า แต่น่าจะดีกว่า

ว่าแต่ท่านๆทั้งหลายเขาไม่ใช้ checkbox ใน header กันมั่งรึครับ
แล้วเวลาเลือกทั้งหมดใน datagrid ใช้วิธีไหนกันรึครับ ขอยืมมาใช้มั่ง จิได้บ่

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 10:27:26 By : lamaka.tor
 


 

No. 9



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



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


เห็นบางคนบอกให้ Freeze Columns ไว้อะ่คัรบ
มันจะได้ไม่เคลื่อนไปไหน
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 10:42:16 By : fonfire
 


 

No. 10



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



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


666
น่าจะได้น่ะครับ
http://stackoverflow.com/questions/12642348/datagridview-custom-column-header-content-checkbox-control


Code (C#)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test_Grid_CheckBox
{
    public partial class Form1 : Form
    {
        CheckBox chkAll = new CheckBox();
        private int headerCheckboxRightMargin;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        public void AddCheckAll()
        {
            Rectangle rect;
            try
            {
                rect = new Rectangle();
                chkAll.Size = new Size(14, 14);
                rect.X = dataGridView1.RowHeadersWidth + (dataGridView1.Columns[0].Width/2)-6;
                rect.Y = 5;
                chkAll.Location = rect.Location;
                chkAll.CheckedChanged += new System.EventHandler(new EventHandler(ckBox_CheckedChanged));
                dataGridView1.Controls.Add(chkAll);
            }
            catch (Exception ex)
            {
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i;
            DataGridViewTextBoxColumn col1 = new DataGridViewTextBoxColumn();
            DataGridViewTextBoxColumn col2 = new DataGridViewTextBoxColumn();
            DataGridViewCheckBoxColumn chk1 = new DataGridViewCheckBoxColumn();
            
            chk1.Name = "chk1";
            col1.Name = "col1";
            col2.Name = "col2";
            chk1.HeaderText = "";
            col1.HeaderText = "col1";
            col2.HeaderText = "col2";

            dataGridView1.Columns.Add(chk1);
            dataGridView1.Columns.Add(col1);
            dataGridView1.Columns.Add(col2);
            dataGridView1.AllowUserToAddRows = false;
            AddCheckAll();

           

            for (i = 0; i <= 100; i++)

            {
                dataGridView1.Rows.Add(new object[] {0, i,"Test" });
            }

            var checkboxHeaderCellRect = dataGridView1.GetCellDisplayRectangle(0, -1, false);
            headerCheckboxRightMargin = (checkboxHeaderCellRect.Width - chkAll.Width) / 2;

        }

        private void ckBox_CheckedChanged(object sender, EventArgs e)
        {

            foreach (DataGridViewRow  dr in dataGridView1.Rows)
                {
                    switch (chkAll.Checked)
                    {
                        case true:
                            dr.Cells[0].Value= 1;
                            break;
                        case false:
                            dr.Cells[0].Value = 0;
                            break;
                        default:
                            dr.Cells[0].Value = 0;
                            break;
                    }
                }
            dataGridView1.CurrentCell = null;
        }

        private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0)
                ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }

        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
          
            Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

            Point oPoint = new Point();

            oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - chkAll.Width);
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - chkAll.Height) / 2 + 1;

            if (oPoint.X < oRectangle.X)
            {
                chkAll.Visible = false;
            }
            else
            {
                chkAll.Visible = true;
            }
            chkAll.Location = oPoint;


        }


    }
}


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 10:50:14 By : fonfire
 


 

No. 11



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

ตอบความคิดเห็นที่ : 9 เขียนโดย : fonfire เมื่อวันที่ 2017-04-03 10:42:16
รายละเอียดของการตอบ ::
แบบนั้นก็ลองแล้วครับ(ไม่ได้เช่นกัน)
มันเป็นการ add ที่ datagrid ครับ พอเราเลื่อน column ตัว checkbox จึงอยู่กับที่

ถ้าเราหาทางยัดเข้าไปใน column ได้ น่าจะจัดการง่ายขึ้นรึป่าวครับ(แต่ผมก็ยังคิดไม่ออกว่าจะยัดเข้าไปยังไง)


แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 12:15:24 By : lamaka.tor
 


 

No. 12



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



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


คุณ TOR_CHEMISTRY ลองตาม คห.10 ดูครับ
ว่าพอจะใช้ได้ไหม
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 15:19:58 By : fonfire
 


 

No. 13



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

Code (C#)
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0)
                ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }

        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
          
            Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

            Point oPoint = new Point();

            oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - chkAll.Width);
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - chkAll.Height) / 2 + 1;

            if (oPoint.X < oRectangle.X)
            {
                chkAll.Visible = false;
            }
            else
            {
                chkAll.Visible = true;
            }
            chkAll.Location = oPoint;


        }




เมื่อวานลองดูแล้วครับท่าน ไม่ได้เหมือนเดิม

1311
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 15:49:17 By : lamaka.tor
 


 

No. 14



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

แล้วถ้าเราเอา checkbox ใส่ใน column จะได้ไม๊ครับ

ถ้าได้ต้องทำยังไงรึครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 15:53:04 By : lamaka.tor
 


 

No. 15



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



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


ลองก๊อปไปใหม่ทั้งหมดได้ไหมครับ
เพราะผมลองลากแล้ว checkbox มันก็หายไปน่ะครับ

แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 15:58:49 By : fonfire
 


 

No. 16



โพสกระทู้ ( 4,436 )
บทความ ( 23 )



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

ตอบความคิดเห็นที่ : 13 เขียนโดย : lamaka.tor เมื่อวันที่ 2017-04-03 15:49:17
รายละเอียดของการตอบ ::
มาแก้ข่าวครับ
ใช้ได้นะครับ

Code (C#)
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0)
                ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }

        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
          
            Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, false);

            Point oPoint = new Point();

            oPoint.X = oRectangle.Location.X + (oRectangle.Width - headerCheckboxRightMargin - chkAll.Width);
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - chkAll.Height) / 2 + 1;

            if (oPoint.X < oRectangle.X)
            {
                chkAll.Visible = false;
            }
            else
            {
                chkAll.Visible = true;
            }
            chkAll.Location = oPoint;


        }



พอดีเมื่อวานที่ทำมีแค่นี้ครับ
Code (C#)
private void dataGridView1_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
        {
            if (e.RowIndex == -1 && e.ColumnIndex == 0) ResetHeaderCheckBoxLocation(e.ColumnIndex, e.RowIndex);
        }
        private void ResetHeaderCheckBoxLocation(int ColumnIndex, int RowIndex)
        {
            //Get the column header cell bounds
            Rectangle oRectangle = this.dataGridView1.GetCellDisplayRectangle(ColumnIndex, RowIndex, true);

            Point oPoint = new Point();

            oPoint.X = oRectangle.Location.X + (oRectangle.Width - chkAll.Width) / 2 + 1;
            oPoint.Y = oRectangle.Location.Y + (oRectangle.Height - chkAll.Height) / 2 + 1;

            //Change the location of the CheckBox to make it stay on the header
           // MessageBox.Show(oPoint.ToString());
            chkAll.Location = oPoint;
}



ขอบคุณมากๆครับ
แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 16:01:09 By : lamaka.tor
 


 

No. 17



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



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



แสดงความคิดเห็นโดยอ้างถึง ความคิดเห็นนี้
Date : 2017-04-03 16:04:42 By : fonfire
 

   

ค้นหาข้อมูล


   
 

แสดงความคิดเห็น
Re : C# WinApp สอบถามเรื่องการทำ HeaderCheckBox ใน DataGridView ครับ
 
 
รายละเอียด
 
ตัวหนา ตัวเอียง ตัวขีดเส้นใต้ ตัวมีขีดกลาง| ตัวเรืองแสง ตัวมีเงา ตัวอักษรวิ่ง| จัดย่อหน้าอิสระ จัดย่อหน้าชิดซ้าย จัดย่อหน้ากึ่งกลาง จัดย่อหน้าชิดขวา| เส้นขวาง| ขนาดตัวอักษร แบบตัวอักษร
ใส่แฟลช ใส่รูป ใส่ไฮเปอร์ลิ้งค์ ใส่อีเมล์ ใส่ลิ้งค์ 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 03
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 อัตราราคา คลิกที่นี่