pengmu123 2017-04-05 01:59 采纳率: 20%
浏览 3594
已结题

winform如何把textbox作为列放到datagridview中

图片说明

如题和图中红框所示,winform中怎么实现这种输入框列啊,datagridviewtextboxcolumn列没有边框啊,怎么实现这种效果啊,急急急!

  • 写回答

2条回答 默认 最新

  • 专家-郭老师 Java领域新星创作者 2017-04-05 03:21
    关注

    转:这个我刚好会,以前看了别人的博客学会的
    先从工具箱拖一个textbox到窗体form1中.然后设置其为visible属性为false
    在窗体加载中写
    private void Form1_Load(object sender, EventArgs e)
    {
    textBox1.Visible = false;
    textBox1.Width = 0;
    dataGridView1.Controls.Add(textBox1); //这句很重要的
    }
    datagridview1的CurrentCellChanged的事件中写
    private void dataGridView1_CurrentCellChanged(object sender, EventArgs e)
    {
    this.textBox1.Visible = false;
    this.textBox1.Width = 0;
    try
    {
    //前提是你的datagridview1一定要有数据呀
    if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].HeaderText == "你选中的单元格所在的列名")
    {
    //以下是把把textbox1的位置与datagridview1的单元格的位置一样,不过长宽均比单元格的短2
    this.textBox1.Left = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, true).Left;
    this.textBox1.Top = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, true).Top;
    this.textBox1.Width = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, true).Width-2;
    this.textBox1.Height = dataGridView1.GetCellDisplayRectangle(dataGridView1.CurrentCell.ColumnIndex, dataGridView1.CurrentCell.RowIndex, true).Height - 2;

                    string str = Convert.ToString(this.dataGridView1.CurrentCell.Value);                                                this.textBox1.Text = str;//将单元格的值赋给文本框
                    this.textBox1.Visible = true;
                }
            }
            catch
            {
            }
        }
    

    在文本框的Validating事件中
    private void textBox1_Validating(object sender, CancelEventArgs e)
    {
    this.dataGridView1.CurrentCell.Value = this.textBox1.Text;//如果文本框输入内容后,把其值赋给其对应的单元格
    }

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog