【问题标题】:Make Button disappear in the new row in datagridview c#使按钮在datagridview c#的新行中消失
【发布时间】:2013-07-07 23:55:43
【问题描述】:

这是我的datagridview的视图..

这是翻译按钮,当我单击它时,它旁边会出现一个新行,并且该行中也会初始化一个新按钮。

现在我想要的是,如果单击翻译按钮,出现的新行不应该包含翻译按钮..我已经动态初始化了按钮..这是我的代码。

   private void button()
    {
        var buttonCol = new DataGridViewButtonColumn();
        buttonCol.Name = "ButtonColumnName";
        buttonCol.HeaderText = "Header";
        buttonCol.Text = "Translate";
        buttonCol.Name = "Edit";
        buttonCol.Width = 30;
        buttonCol.UseColumnTextForButtonValue = true;
        dataGridView1.Columns.Add(buttonCol);
    }

这是点击按钮时的事件..

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
       // button();
        Column_handling();
        if (e.ColumnIndex == 10)
        {
            DataRow dt = datarows.NewRow();
            dt[0] = this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
            dt[1] = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
            dt[2] = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
            dt[3] = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();
            dt[4] = this.dataGridView1.CurrentRow.Cells[4].Value.ToString();
            dt[5] = this.dataGridView1.CurrentRow.Cells[5].Value.ToString();
            dt[6] = this.dataGridView1.CurrentRow.Cells[6].Value.ToString();
            dt[7] = this.dataGridView1.CurrentRow.Cells[7].Value.ToString();
            dt[8] = this.dataGridView1.CurrentRow.Cells[8].Value.ToString();
            dt[9] = this.dataGridView1.CurrentRow.Cells[9].Value.ToString();
            datarows.Rows.InsertAt(dt, e.RowIndex+1);
            dataGridView1.Refresh();
        }    

有什么想法吗?

【问题讨论】:

    标签: c# button datagridview datagridviewbuttoncolumn


    【解决方案1】:

    您必须将单元格类型更改为TextBox

    private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
    {
            Column_handling();
            if (e.ColumnIndex == 10)
            {
                DataRow dt = datarows.NewRow();
                //fillign vlaues
                datarows.Rows.InsertAt(dt, e.RowIndex + 1);
    
                var row = this.dataGridView1.Rows[e.RowIndex + 1];
                var cell = new DataGridViewTextBoxCell();
                cell.Value = string.Empty;
    
                row.Cells["ButtonColumnName"] = cell;
    
                cell.ReadOnly = true;
    
                dataGridView1.Refresh();
            }
    }
    

    我创建了单元格ReadOnly 以阻止用户向其中输入任何内容。

    【讨论】:

      【解决方案2】:

      按钮方法传递一个 boolean 字段作为参数,并在您单击翻译按钮创建行时将其设置为 false。
      然后将Visibility属性设置为这个boolean字段

      设置默认值否则为真。

      【讨论】:

        【解决方案3】:

        好的,我知道这是一篇旧帖子,但无论如何......这对我有用。

        private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
        {
            if ((dataGridView1.Columns[e.ColumnIndex] as DataGridViewButtonColumn) != null && dataGridView1.Rows[e.RowIndex].IsNewRow)
            {
                DataGridViewCellStyle emptyCellStyle = new DataGridViewCellStyle();
                emptyCellStyle.Padding = new Padding(75, 0, 0, 0);
                dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Style = emptyCellStyle;
            }
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-06-29
          • 2019-08-30
          • 1970-01-01
          • 1970-01-01
          • 2016-05-04
          相关资源
          最近更新 更多