zoukankan      html  css  js  c++  java
  • 在dataGridView的单元格中托管Button控件

    一、在dataGridView的cell单击事件中构造含Button控件的文本框无法显示在dataGridView中的单元格中

       private void dgvAccountContrast_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == accountCode.Index)
                {
                    try
                    {
                        Rectangle rect = this.dgv_accountCodeContrast.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
    
                        //adding textboxbutton control at code column
                        //在dataGridView的cell单击事件中构造含Button控件的文本框无法显示在
                        //dataGridView中的单元格中
                        this.textBoxWithButton = new TextBoxWithButton();
    
                        this.textBoxWithButton.btnCode.Click += new EventHandler(btnCode_Click);
                        this.textBoxWithButton.Leave += new EventHandler(txtbtnControl_Leave);
                        this.textBoxWithButton.txtCode.TextChanged += new EventHandler(txtCode_TextChanged);
    
                        this.textBoxWithButton.Visible = false;
                        this.dgv_accountCodeContrast.Controls.Add(this.textBoxWithButton);
                        this.textBoxWithButton.Location = rect.Location;
                        this.textBoxWithButton.Size = rect.Size;
    
                        this.textBoxWithButton.btnCode.Text = "...";
                        //textBoxWithButton.Refresh();
                        this.textBoxWithButton.Visible = true;
                        this.textBoxWithButton.Focus();
                        if (!string.IsNullOrEmpty(this.dgv_accountCodeContrast.CurrentRow.Cells[accountCode.Name].Value.ToString()))
                        {
                            this.textBoxWithButton.txtCode.Text = this.dgv_accountCodeContrast.CurrentRow.Cells[accountCode.Name].Value.ToString();
                        }
                        else
                        {
                            this.textBoxWithButton.txtCode.Text = "";
                        }
                    }
                    catch (Exception) { }
                }
            }
    View Code

    二、构造窗体时即构造含Button控件的文本框(自定义控件),在dataGridView的Cell单击事件中仅设定含Button控件的文本框可见于事件中的单元格,

    此时,可以显示在dataGridView中的单元格中,但是无法更改自定义控件的大小,即使编程中更改的自定义控件的大小

    2.1 构造窗体时即构造含Button控件的文本框(自定义控件)代码:

     void InitializeControlsState()
            {
                //表头样式
                DataGridViewCellStyle style = new DataGridViewCellStyle();
                style.Alignment =
                    DataGridViewContentAlignment.MiddleCenter;
                style.ForeColor = Color.IndianRed;
                style.BackColor = Color.Ivory;
                style.Font = new Font(dgv_accountCodeContrast.Font, FontStyle.Bold);
    
                dgv_accountCodeContrast.ColumnHeadersDefaultCellStyle = style;
    
                this.accountCode = new DataGridViewTextBoxColumn();
                this.accountCode.Name = "本地科目编码";
                //定义列的排列顺序
                accountCode.DisplayIndex = 0;
            
                this.accountCode.Width = 270;
                //this.accountCode.Resizable = DataGridViewTriState.False;
                this.dgv_accountCodeContrast.Columns.Add(accountCode);
    
                this.Description = new DataGridViewTextBoxColumn();
                this.Description.Name = "本地科目名称";
                Description.DisplayIndex = 1;
               
                this.Description.Width = 150;
                this.dgv_accountCodeContrast.Columns.Add(Description);
    
                //adding textboxbutton control at code column...
                this.textBoxWithButton = new TextBoxWithButton();
                this.textBoxWithButton.Visible = false;
                this.dgv_accountCodeContrast.Controls.Add(this.textBoxWithButton);
            }

    2.2设定自定义控件的显示位置的代码:

       private void dgvAccountContrast_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                if (e.ColumnIndex == accountCode.Index)
                {
                    try
                    {
                        Rectangle rect = this.dgv_accountCodeContrast.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true);
    
                        //adding textboxbutton control at code column
                        //在dataGridView的cell单击事件中构造含Button控件的文本框无法显示在
                        //dataGridView中的单元格中
                        //所以只在此处设定自定义控件的显示位置
                        
                        this.textBoxWithButton.Location = rect.Location;
    
                        //虽然更改了自定义件的大小,但不生效
                        this.textBoxWithButton.Size = rect.Size;
    
                        this.textBoxWithButton.btnCode.Text = "...";
                      
                        this.textBoxWithButton.Visible = true;
                        this.textBoxWithButton.Focus();
                        if (!string.IsNullOrEmpty(this.dgv_accountCodeContrast.CurrentRow.Cells[accountCode.Name].Value.ToString()))
                        {
                            this.textBoxWithButton.txtCode.Text = this.dgv_accountCodeContrast.CurrentRow.Cells[accountCode.Name].Value.ToString();
                        }
                        else
                        {
                            this.textBoxWithButton.txtCode.Text = "";
                        }
                    }
                    catch (Exception) { }
                }
            }

  • 相关阅读:
    utils:一个通用枚举类
    代码片段(二):SQL片段
    Scala:(一) 特点及安装环境配置
    Scala:(二) 语言基础-数据结构、表达式(判断、循环、块表达式)
    11-docker搭建mysql一主一从
    10-docker搭建rabbitmq集群
    尚硅谷周阳面试第二季
    docker 修改mysql 表大小写铭感
    volatile的理解
    消息队列优缺点及其选型
  • 原文地址:https://www.cnblogs.com/windy3417/p/14312040.html
Copyright © 2011-2022 走看看