好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

C#中自定义DataGridView列并设置文本或按钮的绑定功能

C#中自定义DataGridView列并设置文本或按钮的绑定功能

    partial class EditClient

    {

        /// <summary>

        /// Required designer variable.

        /// </summary>

        private System.ComponentModel.IContainer components = null;

        /// <summary>

        /// Clean up any resources being used.

        /// </summary>

        /// <param name="disposing">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()

        {

            DataGridViewCellStyle dataGridViewCellStyle1 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle5 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle6 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle2 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle3 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle4 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle7 = new DataGridViewCellStyle();

            DataGridViewCellStyle dataGridViewCellStyle8 = new DataGridViewCellStyle();

            tbsearch = new TextBox();

            button1 = new Button();

            DataGridView1 = new DataGridView();

            ID = new DataGridViewTextBoxColumn();

            name = new DataGridViewTextBoxColumn();

            code = new DataGridViewTextBoxColumn();

            area = new DataGridViewTextBoxColumn();

            type = new DataGridViewTextBoxColumn();

            tel = new DataGridViewTextBoxColumn();

            contacts = new DataGridViewTextBoxColumn();

            addr = new DataGridViewTextBoxColumn();

            groupBox1 = new GroupBox();

            label7 = new Label();

            labmust = new Label();

            comboBox1 = new ComboBox();

            label6 = new Label();

            tbtel = new TextBox();

            label5 = new Label();

            label4 = new Label();

            tbper = new TextBox();

            label3 = new Label();

            tbaddr = new TextBox();

            tbarea = new TextBox();

            label2 = new Label();

            tbcode = new TextBox();

            label1 = new Label();

            btn_cancel = new Button();

            btn_submit = new Button();

            tbname = new TextBox();

            lab3 = new Label();

            lab2 = new Label();

            la1 = new Label();

            labid = new Label();

            UpCol = new DataGridViewButtonColumn();

            DelCol = new DataGridViewButtonColumn();

            ((System.ComponentModel.ISupportInitialize)DataGridView1).BeginInit();

            groupBox1.SuspendLayout();

            SuspendLayout();

            // 

            // tbsearch

            // 

            tbsearch.Location = new Point(38, 169);

            tbsearch.Name = "tbsearch";

            tbsearch.PlaceholderText = "请输入客户";

            tbsearch.Size = new Size(150, 23);

            tbsearch.TabIndex = 0;

            // 

            // button1

            // 

            button1.Location = new Point(200, 165);

            button1.Name = "button1";

            button1.Size = new Size(75, 27);

            button1.TabIndex = 1;

            button1.Text = "筛选";

            button1.UseVisualStyleBackColor = true;

            button1.Click += button1_Click;

            // 

            // DataGridView1

            // 

            DataGridView1.AllowUserToAddRows = false;

            DataGridView1.AllowUserToDeleteRows = false;

            dataGridViewCellStyle1.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridViewCellStyle1.BackColor = SystemColors.Control;

            dataGridViewCellStyle1.Font = new Font("Microsoft YaHei UI", 9F);

            dataGridViewCellStyle1.ForeColor = SystemColors.WindowText;

            dataGridViewCellStyle1.SelectionBackColor = SystemColors.Highlight;

            dataGridViewCellStyle1.SelectionForeColor = SystemColors.HighlightText;

            dataGridViewCellStyle1.WrapMode = DataGridViewTriState.True;

            DataGridView1.ColumnHeadersDefaultCellStyle = dataGridViewCellStyle1;

            DataGridView1.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.AutoSize;

            DataGridView1.Columns.AddRange(new DataGridViewColumn[] { ID, name, code, area, type, tel, contacts, addr });

            dataGridViewCellStyle5.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridViewCellStyle5.BackColor = SystemColors.Window;

            dataGridViewCellStyle5.Font = new Font("Microsoft YaHei UI", 9F);

            dataGridViewCellStyle5.ForeColor = SystemColors.ControlText;

            dataGridViewCellStyle5.SelectionBackColor = SystemColors.Highlight;

            dataGridViewCellStyle5.SelectionForeColor = SystemColors.HighlightText;

            dataGridViewCellStyle5.WrapMode = DataGridViewTriState.False;

            DataGridView1.DefaultCellStyle = dataGridViewCellStyle5;

            DataGridView1.Location = new Point(28, 199);

            DataGridView1.Name = "DataGridView1";

            DataGridView1.ReadOnly = true;

            dataGridViewCellStyle6.BackColor = SystemColors.Control;

            dataGridViewCellStyle6.Font = new Font("Microsoft YaHei UI", 9F);

            dataGridViewCellStyle6.ForeColor = SystemColors.WindowText;

            dataGridViewCellStyle6.SelectionBackColor = SystemColors.Highlight;

            dataGridViewCellStyle6.SelectionForeColor = SystemColors.HighlightText;

            dataGridViewCellStyle6.WrapMode = DataGridViewTriState.True;

            DataGridView1.RowHeadersDefaultCellStyle = dataGridViewCellStyle6;

            DataGridView1.RowTemplate.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;

            DataGridView1.Size = new Size(926, 264);

            DataGridView1.TabIndex = 2;

            DataGridView1.CellContentClick += DataGridView1_CellContentClick;

            DataGridView1.CellFormatting += DataGridView1_CellFormatting;

            // 

            // ID

            // 

            ID.DataPropertyName = "ID";

            dataGridViewCellStyle2.Alignment = DataGridViewContentAlignment.MiddleCenter;

            ID.DefaultCellStyle = dataGridViewCellStyle2;

            ID.Frozen = true;

            ID.HeaderText = "编号";

            ID.Name = "ID";

            ID.ReadOnly = true;

            ID.Visible = false;

            // 

            // name

            // 

            name.DataPropertyName = "name";

            dataGridViewCellStyle3.Padding = new Padding(0, 0, 15, 0);

            name.DefaultCellStyle = dataGridViewCellStyle3;

            name.HeaderText = "客户";

            name.Name = "name";

            name.ReadOnly = true;

            name.Width = 150;

            // 

            // code

            // 

            code.DataPropertyName = "code";

            dataGridViewCellStyle4.Alignment = DataGridViewContentAlignment.MiddleCenter;

            dataGridViewCellStyle4.Padding = new Padding(20, 0, 0, 0);

            code.DefaultCellStyle = dataGridViewCellStyle4;

            code.HeaderText = "简称";

            code.Name = "code";

            code.ReadOnly = true;

            code.SortMode = DataGridViewColumnSortMode.NotSortable;

            // 

            // area

            // 

            area.DataPropertyName = "area";

            area.HeaderText = "地区";

            area.Name = "area";

            area.ReadOnly = true;

            area.SortMode = DataGridViewColumnSortMode.NotSortable;

            area.Width = 80;

            // 

            // type

            // 

            type.DataPropertyName = "type";

            type.HeaderText = "类型";

            type.Name = "type";

            type.ReadOnly = true;

            type.SortMode = DataGridViewColumnSortMode.NotSortable;

            // 

            // tel

            // 

            tel.DataPropertyName = "tel";

            tel.HeaderText = "电话";

            tel.Name = "tel";

            tel.ReadOnly = true;

            tel.SortMode = DataGridViewColumnSortMode.NotSortable;

            tel.Width = 130;

            // 

            // contacts

            // 

            contacts.DataPropertyName = "contacts";

            contacts.HeaderText = "联系人";

            contacts.Name = "contacts";

            contacts.ReadOnly = true;

            contacts.SortMode = DataGridViewColumnSortMode.NotSortable;

            // 

            // addr

            // 

            addr.DataPropertyName = "addr";

            addr.HeaderText = "地址";

            addr.Name = "addr";

            addr.ReadOnly = true;

            addr.SortMode = DataGridViewColumnSortMode.NotSortable;

            addr.Width = 160;

            // 

            // groupBox1

            // 

            groupBox1.BackColor = Color.Gainsboro;

            groupBox1.BackgroundImageLayout = ImageLayout.None;

            groupBox1.Controls.Add(label7);

            groupBox1.Controls.Add(labmust);

            groupBox1.Controls.Add(comboBox1);

            groupBox1.Controls.Add(label6);

            groupBox1.Controls.Add(tbtel);

            groupBox1.Controls.Add(label5);

            groupBox1.Controls.Add(label4);

            groupBox1.Controls.Add(tbper);

            groupBox1.Controls.Add(label3);

            groupBox1.Controls.Add(tbaddr);

            groupBox1.Controls.Add(tbarea);

            groupBox1.Controls.Add(label2);

            groupBox1.Controls.Add(tbcode);

            groupBox1.Controls.Add(label1);

            groupBox1.Controls.Add(btn_cancel);

            groupBox1.Controls.Add(btn_submit);

            groupBox1.Controls.Add(tbname);

            groupBox1.Controls.Add(lab3);

            groupBox1.Controls.Add(lab2);

            groupBox1.Controls.Add(la1);

            groupBox1.Location = new Point(38, 3);

            groupBox1.Name = "groupBox1";

            groupBox1.Size = new Size(894, 158);

            groupBox1.TabIndex = 3;

            groupBox1.TabStop = false;

            groupBox1.Text = "编辑客户";

            groupBox1.Paint += GroupBox_Paint;

            // 

            // label7

            // 

            label7.AutoSize = true;

            label7.ForeColor = Color.Red;

            label7.Location = new Point(288, 57);

            label7.Name = "label7";

            label7.Size = new Size(21, 17);

            label7.TabIndex = 20;

            label7.Text = "(*)";

            // 

            // labmust

            // 

            labmust.AutoSize = true;

            labmust.ForeColor = Color.Red;

            labmust.Location = new Point(57, 55);

            labmust.Name = "labmust";

            labmust.Size = new Size(21, 17);

            labmust.TabIndex = 19;

            labmust.Text = "(*)";

            // 

            // comboBox1

            // 

            comboBox1.FormattingEnabled = true;

            comboBox1.Location = new Point(758, 54);

            comboBox1.Name = "comboBox1";

            comboBox1.Size = new Size(121, 25);

            comboBox1.TabIndex = 18;

            // 

            // label6

            // 

            label6.AutoSize = true;

            label6.Location = new Point(717, 58);

            label6.Name = "label6";

            label6.Size = new Size(44, 17);

            label6.TabIndex = 17;

            label6.Text = "类型:";

            // 

            // tbtel

            // 

            tbtel.Location = new Point(315, 83);

            tbtel.MaxLength = 11;

            tbtel.Name = "tbtel";

            tbtel.PlaceholderText = "请输入电话";

            tbtel.Size = new Size(150, 23);

            tbtel.TabIndex = 16;

            tbtel.KeyPress += tbtel_KeyPress;

            // 

            // label5

            // 

            label5.AutoSize = true;

            label5.Location = new Point(22, 85);

            label5.Name = "label5";

            label5.Size = new Size(56, 17);

            label5.TabIndex = 15;

            label5.Text = "联系人:";

            // 

            // label4

            // 

            label4.AutoSize = true;

            label4.Location = new Point(258, 86);

            label4.Name = "label4";

            label4.Size = new Size(44, 17);

            label4.TabIndex = 14;

            label4.Text = "电话:";

            // 

            // tbper

            // 

            tbper.Location = new Point(85, 83);

            tbper.MaxLength = 11;

            tbper.Name = "tbper";

            tbper.PlaceholderText = "请输入联系人";

            tbper.Size = new Size(150, 23);

            tbper.TabIndex = 13;

            // 

            // label3

            // 

            label3.AutoSize = true;

            label3.Location = new Point(494, 86);

            label3.Name = "label3";

            label3.Size = new Size(44, 17);

            label3.TabIndex = 12;

            label3.Text = "地址:";

            // 

            // tbaddr

            // 

            tbaddr.Location = new Point(544, 83);

            tbaddr.MaxLength = 25;

            tbaddr.Name = "tbaddr";

            tbaddr.PlaceholderText = "请输入地址";

            tbaddr.Size = new Size(150, 23);

            tbaddr.TabIndex = 11;

            // 

            // tbarea

            // 

            tbarea.Location = new Point(544, 54);

            tbarea.MaxLength = 10;

            tbarea.Name = "tbarea";

            tbarea.PlaceholderText = "请输入地区";

            tbarea.Size = new Size(150, 23);

            tbarea.TabIndex = 10;

            // 

            // label2

            // 

            label2.AutoSize = true;

            label2.Location = new Point(494, 56);

            label2.Name = "label2";

            label2.Size = new Size(44, 17);

            label2.TabIndex = 9;

            label2.Text = "地区:";

            // 

            // tbcode

            // 

            tbcode.Location = new Point(315, 54);

            tbcode.MaxLength = 10;

            tbcode.Name = "tbcode";

            tbcode.PlaceholderText = "请输入简称";

            tbcode.Size = new Size(150, 23);

            tbcode.TabIndex = 8;

            tbcode.KeyPress += tbcode_KeyPress;

            // 

            // label1

            // 

            label1.AutoSize = true;

            label1.Location = new Point(256, 57);

            label1.Name = "label1";

            label1.Size = new Size(44, 17);

            label1.TabIndex = 7;

            label1.Text = "简称:";

            // 

            // btn_cancel

            // 

            btn_cancel.BackColor = SystemColors.AppWorkspace;

            btn_cancel.FlatStyle = FlatStyle.Popup;

            btn_cancel.ForeColor = Color.White;

            btn_cancel.Location = new Point(161, 122);

            btn_cancel.Name = "btn_cancel";

            btn_cancel.Size = new Size(75, 23);

            btn_cancel.TabIndex = 6;

            btn_cancel.Text = "取消";

            btn_cancel.UseVisualStyleBackColor = false;

            btn_cancel.Click += btn_cancel_Click;

            // 

            // btn_submit

            // 

            btn_submit.FlatStyle = FlatStyle.Popup;

            btn_submit.ForeColor = Color.White;

            btn_submit.Location = new Point(25, 122);

            btn_submit.Name = "btn_submit";

            btn_submit.Size = new Size(75, 23);

            btn_submit.TabIndex = 5;

            btn_submit.Text = "添加";

            btn_submit.UseVisualStyleBackColor = true;

            btn_submit.Click += btn_submit_Click;

            // 

            // tbname

            // 

            tbname.Location = new Point(87, 54);

            tbname.MaxLength = 15;

            tbname.Name = "tbname";

            tbname.PlaceholderText = "请输入客户";

            tbname.Size = new Size(150, 23);

            tbname.TabIndex = 4;

            // 

            // lab3

            // 

            lab3.AutoSize = true;

            lab3.Location = new Point(96, 29);

            lab3.Name = "lab3";

            lab3.Size = new Size(0, 17);

            lab3.TabIndex = 2;

            // 

            // lab2

            // 

            lab2.AutoSize = true;

            lab2.Location = new Point(22, 57);

            lab2.Name = "lab2";

            lab2.Size = new Size(44, 17);

            lab2.TabIndex = 1;

            lab2.Text = "客户:";

            // 

            // la1

            // 

            la1.AutoSize = true;

            la1.Location = new Point(22, 29);

            la1.Name = "la1";

            la1.Size = new Size(68, 17);

            la1.TabIndex = 0;

            la1.Text = "当前状态:";

            // 

            // labid

            // 

            labid.AutoSize = true;

            labid.Location = new Point(770, 475);

            labid.Name = "labid";

            labid.Size = new Size(15, 17);

            labid.TabIndex = 4;

            labid.Text = "0";

            labid.Visible = false;

            // 

            // UpCol

            // 

            dataGridViewCellStyle7.Padding = new Padding(3);

            UpCol.DefaultCellStyle = dataGridViewCellStyle7;

            UpCol.FlatStyle = FlatStyle.Popup;

            UpCol.HeaderText = "修改";

            UpCol.Name = "UpCol";

            UpCol.ReadOnly = true;

            UpCol.Text = "选中";

            UpCol.UseColumnTextForButtonValue = true;

            UpCol.Width = 65;

            // 

            // DelCol

            // 

            dataGridViewCellStyle8.Padding = new Padding(3);

            DelCol.DefaultCellStyle = dataGridViewCellStyle8;

            DelCol.FlatStyle = FlatStyle.Popup;

            DelCol.HeaderText = "操作";

            DelCol.Name = "DelCol";

            DelCol.ReadOnly = true;

            DelCol.Text = "删除";

            DelCol.UseColumnTextForButtonValue = true;

            DelCol.Width = 65;

            // 

            // EditClient

            // 

            AutoScaleDimensions = new SizeF(7F, 17F);

            AutoScaleMode = AutoScaleMode.Font;

            ClientSize = new Size(1249, 493);

            Controls.Add(labid);

            Controls.Add(groupBox1);

            Controls.Add(DataGridView1);

            Controls.Add(button1);

            Controls.Add(tbsearch);

            FormBorderStyle = FormBorderStyle.None;

            Name = "EditClient";

            Text = "List";

            FormClosing += EditClient_FormClosing;

            ((System.ComponentModel.ISupportInitialize)DataGridView1).EndInit();

            groupBox1.ResumeLayout(false);

            groupBox1.PerformLayout();

            ResumeLayout(false);

            PerformLayout();

        }

        #endregion

        private TextBox tbsearch;

        private Button button1;

        private DataGridView DataGridView1;

        private GroupBox groupBox1;

        private Label la1;

        private Button btn_submit;

        private TextBox tbname;

        private Label lab3;

        private Label lab2;

        private Button btn_cancel;

        private TextBox tbcode;

        private Label label1;

        private Label label4;

        private TextBox tbper;

        private Label label3;

        private TextBox tbaddr;

        private TextBox tbarea;

        private Label label2;

        private TextBox tbtel;

        private Label label5;

        private Label label6;

        private ComboBox comboBox1;

        private Label labmust;

        private Label label7;

        private Label labid;

        private DataGridViewTextBoxColumn ID;

        private DataGridViewTextBoxColumn name;

        private DataGridViewTextBoxColumn code;

        private DataGridViewTextBoxColumn area;

        private DataGridViewTextBoxColumn type;

        private DataGridViewTextBoxColumn tel;

        private DataGridViewTextBoxColumn contacts;

        private DataGridViewTextBoxColumn addr;

        private DataGridViewButtonColumn UpCol;

        private DataGridViewButtonColumn DelCol;

    }

        /// <summary>

        /// 事件-删除、修改点击

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void DataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)

        {

            if ((e.RowIndex >= 0) && (e.ColumnIndex >= 0))

            {

                //说明点击的列是DataGridViewButtonColumn列

                DataGridViewRow Row = DataGridView1.Rows[e.RowIndex];

                int index_id = 0;

                int index_name = 0;

                //查看哪一列是ID

                for (int i = 0; i < Row.Cells.Count; i++)

                {

                    if (DataGridView1.Columns[i].Name == "ID") index_id = i;

                    if (DataGridView1.Columns[i].Name == "name") index_name = i;

                }

                //选中-->到编辑区

                if (DataGridView1.Columns[e.ColumnIndex].Name == "UpCol" && e.RowIndex >= 0)

                {

                    int id = Int32.Parse(Row.Cells[index_id].Value.ToString());

                    string name = Row.Cells[index_name].Value.ToString();

                    UpGroup(id, name);//状态切换

                }

                //删除

                if (DataGridView1.Columns[e.ColumnIndex].Name == "DelCol" && e.RowIndex >= 0)

                {

                    int id = Int32.Parse(Row.Cells[index_id].Value.ToString());

                    Del_Step(id);  //执行删除

                }

            }

        }


查看更多关于C#中自定义DataGridView列并设置文本或按钮的绑定功能的详细内容...

  阅读:25次