Hi All;
I'm working on a simple desktop application that does the following:
1 - Reads in an Excel spreadsheet into a DataGridView as follows:
try
{
string strConnectionSt ring = "";
if (header)
{
strConnectionSt ring =
@"Provider=Micr osoft.Jet.OLEDB .4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Ex cel 8.0;HDR=Yes\"";
}
else
{
strConnectionSt ring =
@"Provider=Micr osoft.Jet.OLEDB .4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Ex cel 8.0;HDR=No\"";
}
OleDbConnection cnCSV = new
OleDbConnection (strConnectionS tring);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@" SELECT * FROM
[Sheet1$]", cnCSV);
OleDbDataAdapte r daCSV = new OleDbDataAdapte r();
daCSV.SelectCom mand = cmdSelect;
dtCSV = new DataTable("Batc h");
daCSV.Fill(dtCS V);
cnCSV.Close();
daCSV = null;
return dtCSV;
}
2 - allows the user to add columns, delete columns, and re-order columns. (I
dont update the underlying datatable as if they add a column, it is unbound.
But not sure if this is correct)
3 - I then parse through the grid verifying the values in the cells match a
specified length, type, etc.
4 - output the grid values to a csv file.
Everything is pretty much working except for parsing the values, and
outputing the csv. After the columns are re-ordered, it seems like they
retain their original index. So when I parse through like this:
for (int r = 0; r <= dgvMain.Rows.Co unt - 2; r++)
{
for (int c = 0; c <= dgvMain.Columns .Count; c++)
{
value = dgvMain.Rows[r].Cells[c].Value.ToString ();
}
}
The variable value contains the value of the original cell. In otherwords,
say I have 2 columns, 0 and 1. I then switch their positions, so now have 1,
0. If I parse through as above, I will still be looking at them as 0, 1, not
1, 0. Or, if I move column 10 to position 1, and read that, I wont read the
value until c = 10, but I need to read it when c = 1. What I need to do is to
reset the indexes of the columns to be in the order that they are displayed.
Has anyone done anything like this before? 5 2376
Rahvyn,
In order to do this, you will need to call the GetFirstColumn method on
the Columns collection, as this method will take display order into account
(while the iterator will not). You need to follow that up with a call to
GetNextColumn using the previous column. This makes it perfect for an
iterator:
private static IEnumerable<Dat aGridViewColumn >
GetDisplayOrder Enumeration(Dat aGridViewColumn Collection columns)
{
// Get the first column.
DataGridViewCol umn column =
columns.GetFirs tColumn(DataGri dViewElementSta tes.None);
// Continue while there is a column.
while (column != null)
{
// Yield the column.
yield return column;
// Get the next column.
column = columns.GetNext Column(column,
DataGridViewEle mentStates.None , DataGridViewEle mentStates.None );
}
}
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Rahvyn" <Ra****@discuss ions.microsoft. comwrote in message
news:D1******** *************** ***********@mic rosoft.com...
Hi All;
I'm working on a simple desktop application that does the following:
1 - Reads in an Excel spreadsheet into a DataGridView as follows:
try
{
string strConnectionSt ring = "";
if (header)
{
strConnectionSt ring =
@"Provider=Micr osoft.Jet.OLEDB .4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Ex cel
8.0;HDR=Yes\"";
}
else
{
strConnectionSt ring =
@"Provider=Micr osoft.Jet.OLEDB .4.0;" +
"Data Source=" + strFilePath + ";Jet
OLEDB:Engine Type=5;" +
"Extended Properties=\"Ex cel
8.0;HDR=No\"";
}
OleDbConnection cnCSV = new
OleDbConnection (strConnectionS tring);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@" SELECT * FROM
[Sheet1$]", cnCSV);
OleDbDataAdapte r daCSV = new OleDbDataAdapte r();
daCSV.SelectCom mand = cmdSelect;
dtCSV = new DataTable("Batc h");
daCSV.Fill(dtCS V);
cnCSV.Close();
daCSV = null;
return dtCSV;
}
2 - allows the user to add columns, delete columns, and re-order columns.
(I
dont update the underlying datatable as if they add a column, it is
unbound.
But not sure if this is correct)
3 - I then parse through the grid verifying the values in the cells match
a
specified length, type, etc.
4 - output the grid values to a csv file.
Everything is pretty much working except for parsing the values, and
outputing the csv. After the columns are re-ordered, it seems like they
retain their original index. So when I parse through like this:
for (int r = 0; r <= dgvMain.Rows.Co unt - 2; r++)
{
for (int c = 0; c <= dgvMain.Columns .Count; c++)
{
value = dgvMain.Rows[r].Cells[c].Value.ToString ();
}
}
The variable value contains the value of the original cell. In
otherwords,
say I have 2 columns, 0 and 1. I then switch their positions, so now have
1,
0. If I parse through as above, I will still be looking at them as 0, 1,
not
1, 0. Or, if I move column 10 to position 1, and read that, I wont read
the
value until c = 10, but I need to read it when c = 1. What I need to do is
to
reset the indexes of the columns to be in the order that they are
displayed.
Has anyone done anything like this before?
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
Also, how would I use something like this as I loop through the rows /
columns to get the values?
"Rahvyn" wrote:
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
The yield keyword will help create an IEnumerable implementation which
you can use a foreach statement to cycle though. You can use it to get the
name of the column to get the value of from the row:
for (int r = 0; r <= dgvMain.Rows.Co unt - 2; r++)
{
foreach (DataGridViewCo lumn c in
GetDisplayOrder Enumeration(dgv Main.Columns))
{
value = dgvMain.Rows[r].Cells[c.Name].Value.ToString ();
}
}
The method I gave you will give you an enumeration you can cycle through
to get the rows in the display order, which you can then use to access the
values in the same order in the underlying data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Rahvyn" <Ra****@discuss ions.microsoft. comwrote in message
news:86******** *************** ***********@mic rosoft.com...
Also, how would I use something like this as I loop through the rows /
columns to get the values?
"Rahvyn" wrote:
>Thanks Nicholas. I am unfamiliar with the yield keyword. What does this return?
Thank you Nicholas, that works perfectly, very elegant solution.
"Nicholas Paldino [.NET/C# MVP]" wrote:
The yield keyword will help create an IEnumerable implementation which
you can use a foreach statement to cycle though. You can use it to get the
name of the column to get the value of from the row:
for (int r = 0; r <= dgvMain.Rows.Co unt - 2; r++)
{
foreach (DataGridViewCo lumn c in
GetDisplayOrder Enumeration(dgv Main.Columns))
{
value = dgvMain.Rows[r].Cells[c.Name].Value.ToString ();
}
}
The method I gave you will give you an enumeration you can cycle through
to get the rows in the display order, which you can then use to access the
values in the same order in the underlying data source.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m
"Rahvyn" <Ra****@discuss ions.microsoft. comwrote in message
news:86******** *************** ***********@mic rosoft.com...
Also, how would I use something like this as I loop through the rows /
columns to get the values?
"Rahvyn" wrote:
Thanks Nicholas. I am unfamiliar with the yield keyword. What does this
return?
This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Allison Bailey |
last post by:
Hi Folks,
I'm a brand new Python programmer, so please point me in the right
direction if this is not the best forum for this question....
I would like to open an existing MS Excel spreadsheet and extract
information from specific worksheets and cells.
I'm not really sure how to get started with this process.
I ran the COM Makepy utility from my PythonWin (IDE from ActiveSTate),
|
by: Otie |
last post by:
I found the following under the GetObject help notes and in the
example for GetObject:
"This example uses the GetObject function to get a reference to a
specific Microsoft Excel worksheet (MyXL). It uses the worksheet's
Application property to make Microsoft Excel visible, to close it, and
so on. Using two API calls, the DetectExcel Sub procedure looks for
Microsoft Excel, and if it is running, enters it in the Running Object
Table. The...
|
by: Matthew Wieder |
last post by:
I have the following requirements:
Build a stand-alone C# application that asks the user to click in a cell
in an Excel spreadsheet, and then displays the address of that cell in
the C# application. It seems simple enough, but the problem I'm
encountering is as follows:
In order for the user to select the cell from Excel, they must first
click once on the Excel window to give it focus and then their second
click is what changes the cell...
|
by: pmud |
last post by:
Hi,
I need to use an Excel Sheet in ASP.NET application so that the users can
enter (copy, paste ) large number of rows in this Excel Sheet. Also, Whatever
the USER ENETRS needs to go to the SQL DATABASE, probably by the click of a
button.
Is this possible? & what is the BEST APPROACH for doing this? & also if any
links are there do tell those to me too coz I have no idea how to go about
doing it.
|
by: Howard Kaikow |
last post by:
There's a significant problem in automating Excel from VB .NET.
Reminds me of a problem I encountered almost 3 years ago that was caused by
the Norton Auntie Virus Office plug-in.
Can anybody reproduce the behavior described below?
For this example, I am using Excel 2002 and VS .NET 2002 and VB 6.
MSFT KB article 304661 gives a trivial example of early and late binding to
Excel
from VB .NET. Note that there is a variable naming...
| |
by: Anthony |
last post by:
To me, creating Excel 2003 spreadsheets programmatically via VB.NET hasn't
really changed since the days of VB6.
That is, I'd do something similar to this
Code:
Dim ExcelApp As Excel.Application
Dim ExcelWB As Excel.Workbook
|
by: stj911 |
last post by:
http://counterpunch.org/rahni04072007.html
Test Tube Zealots: The American Chemical Society Terminates the
Membership of Chemists from Iran
By DAVID N. RAHNI
The American Chemical Society (ACS) has once again led the way, with
its "zealot" interpretation of "embargo" by the Department of
Treasury's Office of Foreign Asset Control, by terminating the
|
by: rahvyn |
last post by:
Hi All;
I'm working on a simple desktop application that does the following:
1 - Reads in an Excel spreadsheet into a DataGridView as follows:
try
{
string strConnectionString = "";
if (header)
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |