目標: DataGridView UI Style, 實作DataGridView的CheckBox單選, 指定Row的BackColor顏色, 各種Column Type
Forn1.Designer.cs
namespace DataGridViewTest
{
partial class Form1
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param>true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
//建立DataGridView object
this.dgv1 = new System.Windows.Forms.DataGridView();
//建立DataGridView Column的類型 object
this.dgv_LinkButtonColumn1 = new System.Windows.Forms.DataGridViewLinkColumn();
this.dgv_CheckBoxColumn1 = new System.Windows.Forms.DataGridViewCheckBoxColumn();
this.dgv_TextBoxColumn1 = new System.Windows.Forms.DataGridViewTextBoxColumn();
this.dgv_ButtonColumn1 = new System.Windows.Forms.DataGridViewButtonColumn();
this.dgv_ComboBoxColumn1 = new System.Windows.Forms.DataGridViewComboBoxColumn();
this.dgv_ImageColumn1 = new System.Windows.Forms.DataGridViewImageColumn();
//建立DataGridView的Cell Style Object,此Cell Style主要以每個Column為主
this.dgvcs1 = new System.Windows.Forms.DataGridViewCellStyle();
//設定文字置中
this.dgvcs1.Alignment = System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter;
//設定背景顏色
this.dgvcs1.BackColor = System.Drawing.Color.FloralWhite;
//設定字型
this.dgvcs1.Font = new System.Drawing.Font("新細明體", 12F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
//設定字型顏色
this.dgvcs1.ForeColor = System.Drawing.SystemColors.WindowText;
//設定被選擇時背景顏色
this.dgvcs1.SelectionBackColor = System.Drawing.SystemColors.Highlight;
//設定被選擇時字型顏色
this.dgvcs1.SelectionForeColor = System.Drawing.SystemColors.HighlightText;
this.dgvcs1.WrapMode = System.Windows.Forms.DataGridViewTriState.True;
//建立DataGridView的Cell Style Object,此Cell Style主要以整個DGV為主
this.dgvcs2 = new System.Windows.Forms.DataGridViewCellStyle();
this.dgvcs2.BackColor = System.Drawing.Color.Cornsilk;
//建立DataGridView的Cell Style Object,此Cell Style主要以每個row為主
this.dgvcs3 = new System.Windows.Forms.DataGridViewCellStyle();
this.dgvcs3.BackColor = (System.Drawing.Color)System.Drawing.Color.FromArgb(((int)(((byte)(157)))), ((int)(((byte)(157)))), ((int)(((byte)(161)))));
((System.ComponentModel.ISupportInitialize)(this.dgv1)).BeginInit();
this.SuspendLayout();
//
// dgv1
//
//加入每個Column
this.dgv1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgv1.Columns.AddRange(new System.Windows.Forms.DataGridViewColumn[] {
this.dgv_LinkButtonColumn1,
this.dgv_CheckBoxColumn1,
this.dgv_TextBoxColumn1,
this.dgv_ButtonColumn1,
this.dgv_ComboBoxColumn1,
this.dgv_ImageColumn1});
//設定Column的Style為dgvcs1
this.dgv1.ColumnHeadersDefaultCellStyle = dgvcs1;
this.dgv1.ColumnHeadersHeightSizeMode = System.Windows.Forms.DataGridViewColumnHeadersHeightSizeMode.AutoSize;
this.dgv1.MultiSelect = false;
this.dgv1.RowHeadersVisible = false;//不顯示左側Header
this.dgv1.BackgroundColor = System.Drawing.Color.WhiteSmoke;
//設定Row的Style為dgvcs1
this.dgv1.RowsDefaultCellStyle = dgvcs2;
this.dgv1.RowTemplate.Height = 24;
//設定被選擇的背景顏色範圍為一整個Row
this.dgv1.SelectionMode = System.Windows.Forms.DataGridViewSelectionMode.FullRowSelect;
//設定Columns[0]的寬度為30
this.dgv1.Columns[0].Width = 30;
this.dgv1.Location = new System.Drawing.Point(12, 12);
this.dgv1.Name = "dgv1";
this.dgv1.RowTemplate.Height = 24;
this.dgv1.Size = new System.Drawing.Size(640, 165);
this.dgv1.TabIndex = 0;
this.dgv1.CellContentClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dgv1_CellContentClick);
this.dgv1.DefaultValuesNeeded += new System.Windows.Forms.DataGridViewRowEventHandler(this.dgv1_DefaultValuesNeeded);
//
// dgv_LinkButtonColumn1
//
this.dgv_LinkButtonColumn1.HeaderText = "Column1";
this.dgv_LinkButtonColumn1.Name = "dgv_LinkButtonColumn1";
//
// dgv_CheckBoxColumn1
//
this.dgv_CheckBoxColumn1.HeaderText = "Column2";
this.dgv_CheckBoxColumn1.Name = "dgv_CheckBoxColumn1";
//
// dgv_TextBoxColumn1
//
this.dgv_TextBoxColumn1.HeaderText = "Column3";
this.dgv_TextBoxColumn1.Name = "dgv_TextBoxColumn1";
//
// dgv_ButtonColumn1
//
this.dgv_ButtonColumn1.HeaderText = "Column4";
this.dgv_ButtonColumn1.Name = "dgv_ButtonColumn1";
//
// dgv_ComboBoxColumn1
//
this.dgv_ComboBoxColumn1.HeaderText = "Column4";
this.dgv_ComboBoxColumn1.Name = "dgv_ComboBoxColumn1";
//
// dgv_ImageColumn1
//
this.dgv_ImageColumn1.HeaderText = "Column4";
this.dgv_ImageColumn1.Name = "dgv_ImageColumn1";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(672, 320);
this.Controls.Add(this.dgv1);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dgv1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.DataGridView dgv1;
private System.Windows.Forms.DataGridViewLinkColumn dgv_LinkButtonColumn1;
private System.Windows.Forms.DataGridViewCheckBoxColumn dgv_CheckBoxColumn1;
private System.Windows.Forms.DataGridViewTextBoxColumn dgv_TextBoxColumn1;
private System.Windows.Forms.DataGridViewButtonColumn dgv_ButtonColumn1;
private System.Windows.Forms.DataGridViewComboBoxColumn dgv_ComboBoxColumn1;
private System.Windows.Forms.DataGridViewImageColumn dgv_ImageColumn1;
System.Windows.Forms.DataGridViewCellStyle dgvcs1;//for column
System.Windows.Forms.DataGridViewCellStyle dgvcs2;//for row
System.Windows.Forms.DataGridViewCellStyle dgvcs3;//for special row
}
}
Form1.cs
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 DataGridViewTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//設定某Column的值
private void dgv1_DefaultValuesNeeded(object sender, DataGridViewRowEventArgs e)
{
e.Row.Cells[0].Value = "del";
}
private void dgv1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
//移除Rows
if (e.ColumnIndex == 0 && e.RowIndex != -1 && dgv1.Rows[e.RowIndex].IsNewRow == false)
{
DialogResult result = MessageBox.Show("r u sure?", "delete", MessageBoxButtons.OKCancel);
if (result == DialogResult.OK)
{
dgv1.Rows.RemoveAt(e.RowIndex);
}
}
//實作CheckBox單選
if (e.ColumnIndex == 1) //checkbox為index 1
{
for (int i = 0; i < dgv1.RowCount; i++) //先設定全部的checkbox為false
dgv1.Rows[i].Cells[1].Value = false;
dgv1.Rows[e.RowIndex].Cells[1].Value = true;//設定被選取的checkbox為true
}
}
private void Form1_Load(object sender, EventArgs e)
{
object[][] data = new object[][]
{new object[]{"del", null, DateTime.Now, null, null, null},
new object[]{"del", null, DateTime.Now, null, null, null},
new object[]{"del", null, DateTime.Now, null, null, null}};
//fill data into gridview
for (int i = 0; i < data.Length; i++)
{
dgv1.Rows.Add(data[i]);//DataGridView加入data
if (i % 2 == 0)
dgv1.Rows[i].DefaultCellStyle = dgvcs3; //將row加入底色
}
}
}
}
留言列表