blah blah blah is here! blah blah » Close

up0down
link

In my code below i can click on two columns of a datagrid cells that contain buttons, it works great, how ever some of the row cells contain no data, how can i adapt the bellow to ignore nulls and do nothing if the cell button is clicked?

private void appsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0)
{
//perform function here on click but if null ignore
salesDataGridView.Rows[newIndex].Cells[0].Value = appsDataGridView.Rows[e.RowIndex].Cells[0].Value;
}

else if (e.ColumnIndex == 1)
{
//perform function here on click but if null ignore
salesDataGridView.Rows[newIndex].Cells[0].Value = appsDataGridView.Rows[e.RowIndex].Cells[1].Value;
}
}

last answered 2 years ago

1 answers

link

Try:

private void appsDataGridView_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.ColumnIndex == 0 && appsDataGridView[0, e.RowIndex].Value != null)
{
//perform function here on click if null ignore
salesDataGridView.Rows[newIndex].Cells[0].Value = appsDataGridView.Rows[e.RowIndex].Cells[0].Value;
}
else if (e.ColumnIndex == 1 && appsDataGridView[1, e.RowIndex].Value != null)
{
//perform function here on click if null ignore
salesDataGridView.Rows[newIndex].Cells[0].Value = appsDataGridView.Rows[e.RowIndex].Cells[1].Value;
}
}

vulpes
17279

I'v edited my original attempt as it will be the Value property that will be null if there's no data, not its string representation.

Thanks vulpes, i've edited my question at the top as i'm copying data from one datagrid to another - but sometimes in datagrid1 there may be a null button, if that's the case i don't want a blank line copying to datagrid2, the above code still copies over a blank line?

vulpes
17279

Doh, I've got the row and column indices the wrong way around in the appsDataGridView indexer, which may explain why it's not working properly. Try it again now I've re-edited it.

Yes it works a treat now - nice one Thanks, much appreciated.

Feedback