[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Exporting a highlighted region from the DataGridView control in Win Forms...

I am trying to add the ability for a user to be able to highlight a region on my datagridview and right click and export to a file.  I currently have it to where they can highlight, right click and export the data to the file, but I am using DataGridView1.SelectedCells.  I want to also give them the column headings when it is dumped, but the only way I know to do that is by using the OwnerColumn property.  

I thought about just doing a copy and paste type action, but I was really trying to work through what I have.  I am open to better suggestions if anyone has done this before and is happy with the result.

I am just having trouble coming up the the column headings and knowing how many columns the user has selected.   I see there is a column index, but that only gives me the index of the column in the grid...

Thanks for the help...
0
rgn2121
Asked:
rgn2121
  • 5
  • 2
2 Solutions
 
Priest04Commented:
0
 
rgn2121Author Commented:
Okay...there are a couple things in there that will keep me from making in mistakes when selecting the cells, but I still don't see how that is helping me get the Column headings. and number of columns.
In the grid I have on my form, when I click and drag across a group of cells and highlight them and then right-click export.  The SelectedRows.Count=0 and the SelectedColumns.Count=0.  So let's assume I have 20 highlighted cells.  I can get the Column Heading by SelectedCells.Item(index).OwnerColumn, but all I know is that the item in index 20 is under that Column name.  How do I tell how many columns I have?
0
 
rgn2121Author Commented:
Maybe I should add that right now I am just trying to dump what is user selected to a csv file.  I currently have code that dumps my entire datatable to a csv file, but I wanted to allow the functionality that I mentioned above.
0
Free recovery tool for Microsoft Active Directory

Veeam Explorer for Microsoft Active Directory provides fast and reliable object-level recovery for Active Directory from a single-pass, agentless backup or storage snapshot — without the need to restore an entire virtual machine or use third-party tools.

 
Priest04Commented:
I am not following you. There is a difference between selecting columns  4 columns (whole columns) and selecting 20 cells withing 4 columns. WHen user highlight cells, why would you want to know the header name? If a user highlights whole column by clicking on its header, then you can use SelectedColumns.Count property to get the number of selected columns.

This all above is assuming you have set SortMode to ColumnHeaderSelect. If not, then the user cannot select Column by pclicking on its header, but highlighting cells. This is most of the time not appropriate solution, because datagridview can have many rows, and it is difficult for user to select whole column. But, if for some reason you canntot set SortMode to ColumnHeaderSelect, then you would need to loop through all SelectedCells and store ColumnIndeces in some array. It would look like this (c#)

System.Collections.ArrayList arr = new System.Collections.ArrayList();
 
foreach (DataGridViewCell cell in dg.SelectedCells)
{
    if (! arr.Contains(cell.ColumnIndex)) arr.Add(cell.ColumnIndex);
}

Open in new window

0
 
rgn2121Author Commented:
I will look at this tonight....The reason I am doing this is so a user can select a region that they want.  I have allowed them to reorder columns so that they can move things around and next to each other so it is easier on them, but most times they will want to just select a region of cells...not full columns and not full rows.  I then wanted it so that when the clicked 'Export Selection' on the ContextMenu, that it would dump the data and column headings to a csv.
Also with that, I have had trouble writing out the data so it lines up, because I can't tell the number of columns or rows.  I might get a selection of 20 cells, but I can't tell if that is 2 rows of 10 or 4 rows of 5...
So far what I have come up with is adding the OwnerColumn.Name to List and then doing List.Count to get a number of columns.  Still having trouble exporting that since the data in the selection is indexed from the far right bottom cell, to the top left cell, starting at the bottom of each column and going up.
 
0
 
rgn2121Author Commented:
Sorry, I have been tied up with other things the past few days.  I think what you wrote, similar to what I was doing, will help me get a number of Comuns and Rows, but it will still be tricky to do.  
For now I will give you the appoints for the help and your code and I will post again to the end of this if I get a full solution...
Thanks..
0
 
rgn2121Author Commented:
I might follow up with a full solution if I get a chance to work it out...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 5
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now