search
尋找貓咪~QQ 地點 桃園市桃園區 Taoyuan , Taoyuan

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

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-及-header-字型-及-大小-控制/

 

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()

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);//
aire//https://vectus.wordpress.com/2011/03/20/datagridview-cell-及-header-字型-及-大小-控制/

            //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;//MTise

            dataGridView1.AllowUserToAddRows
= false;//
±RX¢GADOC//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;//
aiDDMm?//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; //
]wXa¡Ñ//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 = os”;

            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();//±iLuAE

           
dataGridView1.Rows[e.RowIndex].Cells[2].Value = “50”;//
gJAE

            MessageBox.Show(Strbuf);

        }

 

        public DataGridViewColumnSortMode NotSortable { get; set; }

 

        private void button4_Click(object sender, EventArgs e)

        {

            try

            {

                dataGridView1.Rows.Clear();//DuMX¢GMeAAaiUb//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!MX¢G//https://msdn.microsoft.com/zh-tw/library/system.windows.forms.datagridviewcolumncollection.clear(v=vs.110).aspx

            }

        }

    }

}

 

 

 


 




熱門推薦

本文由 jashliaoeuwordpress 提供 原文連結

寵物協尋 相信 終究能找到回家的路
寫了7763篇文章,獲得2次喜歡
留言回覆
回覆
精彩推薦