Click here to Skip to main content
15,888,236 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I want to run
C#
private void cmdCopy_Click(object sender, EventArgs e)
when a cell is double-clicked in an Excel workbook. I have tried DataGridView but it seems that is for data bases only.

What I have tried:

I have tried to use DataGridView but it seems that is for DataBases only and won't let me specify an Excel file,
Posted
Updated 17-Jul-23 14:21pm
v2

You can easily load the data from Excel files into a DataGridView by either:
- Using the Microsoft.Office.Interop.Excel Namespace | Microsoft Learn[^].

- Oledb as discussed in Working with MS Excel(xls / xlsx) Using MDAC and Oledb[^].
 
Share this answer
 
v2
DataGridView can work with DataTables, Lists, and so on - so it's just a case of getting the Excel data in a form it can work with.

How you do that will depend on how you access the Excel data in the first place - but a simple Google gives a lot of hits: datagridview from excel workbook c - Google Search[^]

Start by thinking about how you get the Excel data currently, what constructs it it in, and so forth - then refine your search appropriately.
 
Share this answer
 
Comments
Ralf Meier 16-Jul-23 16:55pm    
... but when the OP doubleclicks a Excel-Cell then the Action must come from a VB-Script inside the Excel-Workbook which catches the Event from the Cell ... or am I wrong ?
OriginalGriff 17-Jul-23 1:56am    
Gawd knows - the question isn't at all clear ...
private void GridViewSetup()
{
int intDate = RegGlobals.intDateCol;
int int1stNam = RegGlobals.intFirstNameCol;
int int2ndNam = RegGlobals.intLastNameCol;
int intCity = RegGlobals.intCityCol;
int intZip = RegGlobals.intZipCodeCol;
int intPhone = RegGlobals.intPhoneCol;
int intNumInFam = RegGlobals.intNumInFamilyCol;
int intActiveSheet = frmFoodPantry.theWorkbook.ActiveSheet.Index;

Excel.Range xlRange = frmFoodPantry.theWorkbook.Sheets[intActiveSheet].UsedRange;
dgvExcel.ColumnCount = xlRange.Columns.Count;
int intRowIndex = 0;

for(int x1Row = 2; x1Row <= xlRange.Rows.Count; x1Row++)
{
if (intRowIndex == 0)
{
dgvExcel.Rows.Add(xlRange.Cells[x1Row, intDate].Text, xlRange.Cells[x1Row, int1stNam].Text, xlRange.Cells[x1Row, int2ndNam].Text,
xlRange.Cells[x1Row, intCity].Text, xlRange.Cells[x1Row, intZip].Text, xlRange.Cells[x1Row, intPhone].Text, xlRange.Cells[x1Row, intNumInFam].Text);
}
else
{
dgvExcel.Rows.Add(intRowIndex, xlRange.Cells[x1Row, intDate].Text, xlRange.Cells[x1Row, int1stNam].Text, xlRange.Cells[x1Row, int2ndNam].Text,
xlRange.Cells[x1Row, intCity].Text, xlRange.Cells[x1Row, intZip].Text, xlRange.Cells[x1Row, intPhone].Text, xlRange.Cells[x1Row, intNumInFam].Text);
}
intRowIndex++;
}
frmFoodPantry.theWorkbook.Close();
}
 
Share this answer
 
Comments
Richard Deeming 18-Jul-23 3:35am    
An unformatted, unexplained code-dump does not make a good solution. How would you feel if someone else had just dumped a wall of unexplained code as a solution to your question?

Edit your solution, format the code properly, and add an explanation of how this solves the question.
PaulaJoannAllen 18-Jul-23 5:56am    
Sorry about that. I will do that but the real solution resulted from the comment by OriginalGriff, which I accepted, it helped with the right search phrase. You are right I should of waited until I completed commenting the code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900