Reputation: 213
I need to cast DataGridViewTextBoxCell to DatagridVIewButtonCell on the specifics rows. I tried to do this by using the following code:
foreach (DataGridViewRow row in dataGridViewImport.Rows)
{
if(row.Cells["Active"].Value.Equals("false"))
{
DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
row.Cells["check"] = buttonCell;
}
}
But it didn't work. Somebody knows what could be the problem? This behaviour had started after I bound the dataSource.
Also i had tried to create new column and its works,but i need buttons only for specific rows,not for all column
DataGridViewButtonColumn btnColumn = new DataGridViewButtonColumn();
btnColumn.Name = "test";
dataGridViewImport.Columns.Add(btnColumn);
All databindcode:
public void prepareDataGridView()
{
dataTable = new DataTable();
addColumnsToDataTable(dataTable);
fillDataTable();
dataGridViewImport.DataSource = dataTable;
addHeaderCheckBox();
addButtonsToUnactiveRows();
styleDataGrid();
}
public void addColumnsToDataTable(DataTable dataTable)
{
dataTable.Columns.Add("check");
dataTable.Columns.Add("Position");
dataTable.Columns.Add("Positionsindex");
dataTable.Columns.Add("Textindex");
dataTable.Columns.Add("Stichwort");
dataTable.Columns.Add("Menge");
dataTable.Columns.Add("EH");
dataTable.Columns.Add("Active");
}
private void fillDataTable()
{
//FILL DATA
}
public void addButtonsToUnactiveRows()
{
foreach (DataGridViewRow row in dataGridViewImport.Rows)
{
if(row.Cells["Active"].Value.Equals("false"))
{
DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
row.Cells["check"] = buttonCell;
}
}
}
Upvotes: 0
Views: 917
Reputation: 213
The problem was in event handling,because first of all,i bound data,then draw DataGrid and then try to add buttons.But i need to add buttons before drawing.So i find the solution with this event
private void dataGridViewImport_DataBindingComplete(object sender, DataGridViewBindingCompleteEventArgs e)
{
addButtonsToUnactiveRows();
}
Upvotes: 1
Reputation: 422
I tried your code with somechange (didn't know if you mis-typed some name or intended) as follow, it work.
DataTable dataTable;
private void Form1_Load(object sender, EventArgs e)
{
prepareDataGridView();
}
public void prepareDataGridView()
{
dataTable = new DataTable();
addColumnsToDataTable(dataTable);
fillDataTable();
avaEditDataGridViewImport.DataSource = dataTable;
//addHeaderCheckBox();
addButtonsToUnactiveRows();
//styleDataGrid();
}
public void addColumnsToDataTable(DataTable dataTable)
{
dataTable.Columns.Add("check");
dataTable.Columns.Add("Position");
dataTable.Columns.Add("Positionsindex");
dataTable.Columns.Add("Textindex");
dataTable.Columns.Add("Stichwort");
dataTable.Columns.Add("Menge");
dataTable.Columns.Add("EH");
dataTable.Columns.Add("Active");
}
private void fillDataTable()
{
for (int j = 1; j <= 10; j++)
{
dataTable.Rows.Add(j.ToString(), j, j, j, j, j, j, (j % 2 == 0 ? true : false));
}
}
public void addButtonsToUnactiveRows()
{
foreach (DataGridViewRow row in avaEditDataGridViewImport.Rows)
{
if (row.Cells["Active"].Value.ToString().Equals("False"))
{
DataGridViewButtonCell buttonCell = new DataGridViewButtonCell();
row.Cells["check"] = buttonCell;
}
}
}
There are some differences between yours code and mine (which work):
I don't use addHeaderCheckBox() and styleDataGrid() (you did not show it here).
fillDataTable()'s inside code.
The DataGridView
in addButtonsToUnactiveRows() and the if
statement.
Hope you can solve soon :)
Upvotes: 1