1

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?

soldiershin
  • 1,612
  • 1
  • 18
  • 29

3 Answers3

2

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.

gzaxx
  • 17,312
  • 2
  • 36
  • 54
0

Button method pass a boolean field as parameter and set it false when you click Translate button to create row.
Then set the Visibility property to this boolean field

set default value true otherwise.

ridoy
  • 6,274
  • 2
  • 29
  • 60
Rajeev Ranjan
  • 1,006
  • 8
  • 18
0

Ok, I know it is an old post, but anyhow... This is what works for me.

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;
    }
}
Chieleman
  • 83
  • 5