C#表格(DataGridView)整合其他控制項範例

C#表格(DataGridView)整合其他控制項範例

C#表格(DataGridView)整合其他控制項範例


 



GITHUB: https://github.com/jash-git/CS_DataGridView_CheckBox


資料來源: http://stackoverflow.com/questions/7916919/adding-a-button-to-a-winforms-datagridview

 

https://vectus.wordpress.com/2011/03/20/datagridview-cell-%E5%8F%8A-header-%E5%AD%97%E5%9E%8B-%E5%8F%8A-%E5%A4%A7%E5%B0%8F-%E6%8E%A7%E5%88%B6/

 

https://msdn.microsoft.com/zh-tw/library/z2akwyy7(v=vs.110).aspx

 

https://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridviewbuttoncell(v=vs.110).aspx

http://www.programmer-club.com.tw/showSameTitleN/csharp/5354.html

 

https://social.msdn.microsoft.com/Forums/zh-TW/9c555078-d4e9-44b2-a430-ffd92d237314/vb2008-datagridview?forum=232

 

http://www.programmer-club.com.tw/ShowSameTitleN/csharp/9183.html

 

http://csharp.net-informations.com/datagridview/csharp-datagridview-image.htm

 

https://social.msdn.microsoft.com/Forums/vstudio/en-US/78f584ba-53b4-4272-9d67-5f7fdf0c85ab/how-to-clear-datagridview-all-rows?forum=csharpgeneral

 

https://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridviewcolumncollection.clear(v=vs.110).aspx

 

DataGridViewDataTableReadOnly、禁止編輯、刪除空白列、欄位標題置中、設定高度、全部清除、DataGridViewImageColumnDataGridViewButtonColumnDateTimePickerDataGridViewCheckBoxCellDataGridViewTextBoxCellDataGridViewComboBoxCellDataSourcedataGridView1.ColumnCountHeaderTextUseColumnTextForButtonValueDataGridViewCellEventHandlerDataGridViewCellEventArgsColumns.Clear()Rows.Clear()Rows.Add()


Code

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 CS_DataGridView_CheckBox

{

    public partial class Form1 : Form

    {

        public Form1()

        {

            InitializeComponent();

        }

 

        private void button1_Click(object sender, EventArgs e)

        {

            dataGridView1.ColumnCount = 3;

            dataGridView1.Columns[0].Name = "Product ID";

            dataGridView1.Columns[1].Name = "Product Name";

            dataGridView1.Columns[2].Name = "Product Price";

 

            string[] row = new string[] { "1", "Product 1" , "1000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "2", "Product 2", "2000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "3", "Product 3", "3000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "4", "Product 4", "4000" };

            dataGridView1.Rows.Add(row);

 

            DataGridViewCheckBoxColumn chk = new DataGridViewCheckBoxColumn();

            dataGridView1.Columns.Add(chk);

            chk.HeaderText = "Check Data";

            chk.Name = "chk";

            dataGridView1.Rows[2].Cells[3].Value = true;

        }

 

        private void button2_Click(object sender, EventArgs e)

        {

            //http://stackoverflow.com/questions/7916919/adding-a-button-to-a-winforms-datagridview

            DataTable dt = new DataTable();

            dt.Columns.Add("name");

            for (int j = 0; j < 10; j++)

            {

                dt.Rows.Add("");

            }

            this.dataGridView1.DataSource = dt;

            this.dataGridView1.Columns[0].Width = 200;

 

            /*

             * First method : Convert to an existed cell type such ComboBox cell,etc

             */

 

            DataGridViewComboBoxCell ComboBoxCell = new DataGridViewComboBoxCell();

            ComboBoxCell.Items.AddRange(new string[] { "aaa", "bbb", "ccc" });

            this.dataGridView1[0, 0] = ComboBoxCell;

            this.dataGridView1[0, 0].Value = "bbb";

 

            DataGridViewTextBoxCell TextBoxCell = new DataGridViewTextBoxCell();

            this.dataGridView1[0, 1] = TextBoxCell;

            this.dataGridView1[0, 1].Value = "some text";

 

            DataGridViewCheckBoxCell CheckBoxCell = new DataGridViewCheckBoxCell();

            CheckBoxCell.Style.Alignment = DataGridViewContentAlignment.MiddleCenter;

            this.dataGridView1[0, 2] = CheckBoxCell;

            this.dataGridView1[0, 2].Value = true;

 

            /*

             * Second method : Add control to the host in the cell

             */

            DateTimePicker dtp = new DateTimePicker();

            dtp.Value = DateTime.Now.AddDays(-10);

            //add DateTimePicker into the control collection of the DataGridView

            this.dataGridView1.Controls.Add(dtp);

            //set its location and size to fit the cell

            dtp.Location = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Location;

            dtp.Size = this.dataGridView1.GetCellDisplayRectangle(0, 3, true).Size;

        }

 

        private void button3_Click(object sender, EventArgs e)

        {

            dataGridView1.ColumnHeadersDefaultCellStyle.Font = new Font("Tahoma", 15);//欄a位i字r體e//https://vectus.wordpress.com/2011/03/20/datagridview-cell-%E5%8F%8A-header-%E5%AD%97%E5%9E%8B-%E5%8F%8A-%E5%A4%A7%E5%B0%8F-%E6%8E%A7%E5%88%B6/

            //https://msdn.microsoft.com/zh-tw/library/z2akwyy7(v=vs.110).aspx

            dataGridView1.DefaultCellStyle.Font = new Font("Tahoma", 15);

            dataGridView1.DefaultCellStyle.ForeColor = Color.Blue;

            dataGridView1.DefaultCellStyle.BackColor = Color.Beige;

            //https://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridviewbuttoncell(v=vs.110).aspx

            dataGridView1.ReadOnly = true;//禁MT止i編s輯e

            dataGridView1.AllowUserToAddRows = false;//刪±R除X¢G空A白DO列C//http://www.programmer-club.com.tw/showSameTitleN/csharp/5354.html

            dataGridView1.ColumnCount = 3;

            dataGridView1.Columns[0].Name = "Product ID";

            dataGridView1.Columns[1].Name = "Product Name";

            dataGridView1.Columns[2].Name = "Product Price";

            dataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//欄a位i標D題D置Mm中?//https://social.msdn.microsoft.com/Forums/zh-TW/9c555078-d4e9-44b2-a430-ffd92d237314/vb2008-datagridview?forum=232

            dataGridView1.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridView1.RowTemplate.Height = 50; //設]定w高Xa度¡Ñ//http://www.programmer-club.com.tw/ShowSameTitleN/csharp/9183.html

            dataGridView1.Columns[0].Width = 200;

            dataGridView1.Columns[1].Width = 200;

            dataGridView1.Columns[2].Width = 200;

 

 

            string[] row = new string[] { "1", "Product 1" , "1000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "2", "Product 2", "2000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "3", "Product 3", "3000" };

            dataGridView1.Rows.Add(row);

            row = new string[] { "4", "Product 4", "4000" };

            dataGridView1.Rows.Add(row);

           

            DataGridViewButtonColumn buttonColumn =new DataGridViewButtonColumn();

            buttonColumn.Width = 200;

            buttonColumn.HeaderText = "按o鈕s";

            buttonColumn.Name = "Status Request";

            buttonColumn.Text = "Request Status";

            buttonColumn.UseColumnTextForButtonValue = true;

            dataGridView1.Columns.Add(buttonColumn);

            // Add a CellClick handler to handle clicks in the button column.

            dataGridView1.CellClick +=new DataGridViewCellEventHandler(dataGridView1_CellClick);

 

            /*

            //http://csharp.net-informations.com/datagridview/csharp-datagridview-image.htm

            DataGridViewImageColumn img = new DataGridViewImageColumn();

            Image image = Image.FromFile("Image Path");

            img.Image = image;

            dataGridView1.Columns.Add(img);

            img.HeaderText = "Image";

            img.Name = "img";

            */

 

        }

        public void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)

        {

            // Ignore clicks that are not on button cells.

            if (e.RowIndex < 0 || e.ColumnIndex !=dataGridView1.Columns["Status Request"].Index) return;

 

            // Retrieve the Employee object from the "Assigned To" cell.

            String Strbuf = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();//抓±i取Lu數A值E

            dataGridView1.Rows[e.RowIndex].Cells[2].Value = "50";//寫g入J數A值E

            MessageBox.Show(Strbuf);

        }

 

        public DataGridViewColumnSortMode NotSortable { get; set; }

 

        private void button4_Click(object sender, EventArgs e)

        {

            try

            {

                dataGridView1.Rows.Clear();//只Du清M除X¢G資Me料A,A欄a位i還U在b//https://social.msdn.microsoft.com/Forums/vstudio/en-US/78f584ba-53b4-4272-9d67-5f7fdf0c85ab/how-to-clear-datagridview-all-rows?forum=csharpgeneral

            }

            finally

            {

                dataGridView1.Columns.Clear(); //全Dt部!清M除X¢G//https://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridviewcolumncollection.clear(v=vs.110).aspx

            }

        }

    }

}

發表迴響

你的電子郵件位址並不會被公開。 必要欄位標記為 *