ICollection Conversion.

Hi,I can't speak English,If I speak wrong,please help me improve.

Purpose:Print DataGridView,If has selected rows,Only print them,otherwise,print all.

private void printDoc_PrintPage(object sender, PrintPageEventArgs e)
{
// Can't pass,generates complier error CS0173.
ICollection rowCollection = dataGridView1.SelectedRows.Count > 0 dataGridView1.SelectedRows : dataGridView1.Rows;

// Pass,Run normally.
// ICollection rowCollection = null;
// if (dataGridView1.SelectedRows.Count > 0)
// {
// rowCollection = dataGridView1.SelectedRows;
// }
// else
// {
// rowCollection = dataGridView1.Rows;
// }


foreach (DataGridViewRow perRow in rowCollection)
{
// Print rows.
}

// Why What's the different about them
}



Answer this question

ICollection Conversion.

  • bettyday

    An expression must have a specific type, but

    dataGridView1.SelectedRows.Count > 0 dataGridView1.SelectedRows : dataGridView1.Rows

    doesn't since the type SelectedRows returns is different from what Rows returns. The solution is to cast to a common type

    dataGridView1.SelectedRows.Count > 0 (ICollection)dataGridView1.SelectedRows : (ICollection)dataGridView1.Rows



  • ICollection Conversion.