Reputation: 2754
I have a DataGridView and I wish to provide a feature of Exporting Selected Rows and Columns to excel
However, My Selected row count was always coming out to be ZERO, so I did
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
Now, I can not select Individual columns, whenever I select any row, It selects all columns being a full row select
Same is the case with Columns as well If I do
dataGridView1.SelectionMode = DataGridViewSelectionMode.FullColumnSelect;
It selects the whole column.
if I don't do this selection Modes, then
dataGridView1.SelectedRows.Count & dataGridView1.SelectedColumns.Count
always return zero.
How Can I make something like in the Image ?
Upvotes: 0
Views: 1358
Reputation: 54447
Here's how you can get the selected rows:
var selectedRows =
this.dataGridView1.SelectedCells.Cast<DataGridViewCell>()
.Select(cell => cell.OwningRow)
.Distinct()
.OrderBy(row => row.Index);
You can either call Count
on that to get the count but, rather than enumerating twice, call ToArray
and then get the Length
of the array.
Upvotes: 2