Search code examples
c#buttondatagridviewdatagridviewbuttoncolumn

Make Button disappear in the new row in datagridview c#


This is the view of my datagridview..

enter image description here

This is the translate button when i click it a new row appears next to it and a new button is also initialized in that row. When the translation button is pressed

Now what i want is that if the translate button is clicked the new row that appears shouldn't contain the translate button..i have initialized the button dynamically..Here is my code.

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

This is the event when the button is clicked..

    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();
        }    

any ideas?


Solution

  • You have to change cell type to 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();
            }
    }
    

    I made cell ReadOnly to stop user from inputting anything into it.