Сделать кнопку исчезнуть в новой строке в datagridview C#
Это мнение моего 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();
}
есть идеи?
3 ответа
Вы должны изменить тип ячейки на 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
чтобы запретить пользователю вводить что-либо в него.
Метод Button передает булево поле в качестве параметра и устанавливает его в false, когда вы нажимаете кнопку Translate для создания строки.
Затем установите Visibility
свойство этого логического поля
в противном случае установите значение по умолчанию true.
Хорошо, я знаю, что это старый пост, но в любом случае... Это то, что работает для меня.
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;
}
}