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) { }
                }
            }

  • 相关阅读:
    视频直播:Windows中各类画面源的截取和合成方法总结
    短视频技术详解:Android端的短视频开发技术
    视频直播关键技术:流畅、拥塞和延时追赶
    视频直播技术详解:直播的推流调度
    直播技术:从性能参数到业务大数据,浅谈直播CDN服务监控
    音视频通话:小议音频处理与压缩技术
    pip命令报错“no perl script found in input”
    python常见面试题讲解(三)明明的随机数
    如何使用photoshop修改图片的像素大小(分辨率)
    VMware Workstation如何修改弹出释放快捷键
  • 原文地址:https://www.cnblogs.com/windy3417/p/14312040.html
Copyright © 2011-2022 走看看