|
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)
{
strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel 8.0;HDR=Yes\"";
}
else
{
strConnectionString = @"Provider=Microsoft.Jet.OLEDB.4.0;" +
"Data Source=" + strFilePath + ";Jet OLEDB:Engine Type=5;" +
"Extended Properties=\"Excel 8.0;HDR=No\"";
}
OleDbConnection cnCSV = new OleDbConnection(strConnectionString);
cnCSV.Open();
OleDbCommand cmdSelect = new OleDbCommand(@"SELECT * FROM [Sheet1$]", cnCSV);
OleDbDataAdapter daCSV = new OleDbDataAdapter();
daCSV.SelectCommand = cmdSelect;
dtCSV = new DataTable("Batch");
daCSV.Fill(dtCSV);
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.Count - 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?
|
|
|
|
|
Got a solution if anyone is interested:
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<datagridviewcolumn>GetDisplayOrderEnumeration(DataGridViewColumnCollection columns)
{
// Get the first column.
DataGridViewColumn column = columns.GetFirstColumn(DataGridViewElementStates.None);
// Continue while there is a column.
while (column != null)
{
// Yield the column.
yield return column;
// Get the next column.
column = columns.GetNextColumn(column,DataGridViewElementStates.None, DataGridViewElementStates.None);
}
}
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.Count - 2; r++)
{
foreach (DataGridViewColumn c in GetDisplayOrderEnumeration(dgvMain.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.
Thanks to Nicholas over at the msdn forums.
|
|
|
|
|
hi all,
I am USING WINDOWS, C#.Net(2.0)
all i need is, my client is asking me to put a Update Preferences feature where is which i need to check for update software(which is on server) and the checking preferences or
either daily or weekly or monthly
please suggest me how to fulfil this task...
i will be happy if u can provide how to update also(i don't want using ClickOnce--coz its not supporting my windows project)
thnx in advance
prashanth
prashanth,
s/w Engineer,
Syfnosys.
|
|
|
|
|
Beyond the obvious user interface considerations that allow the user to configure the schedule interval, you need to store that preference somewhere. If you want something that is automatic (similar to Windows Update), you will need to write a Windows Service that is responsible for checking to see if an update is available, downloading it, and (optionally) installing it.
This service needs to know about the schedule settings and set some sort of a timer based "wakeup" event to occur x amount of time from today, where x is your schedule interval.
Another option would be to create a separate executable that you then schedule through the Windows Scheduled Tasks interface, but I'm not sure how easy/difficult it is to programatically do that.
As far as downloading the update, you will need to provide either an HTTP or FTP location to host the updated file (or files) and then the client will need to access that location and download the file. You can look at this article for a URL based file downloader:
http://www.codeproject.com/useritems/filedownloader.asp[^]
If the server hosting the update is guaranteed to always be local, you could also do this using file shares and copy the file over the network. Keep in mind that this is the least scalable approach.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
Hi!
I m building a windows application in visual studio.net 2003....i want to display my data in the format like we can display in table control of asp.net web application.
Basically i want to read one row from database and display it like its first column in first row of control,second column in second row of control and so on alongwith their column names as its heading.
Can u help me in this context or can u give me the control name in which i can access its particular cell like we can in table of asp. i tried datagrid but first problem is that its displays one row of database in row format and secondly i can't format its one cell acc to my requirement like making that cell's text bold etc.
Thanx
|
|
|
|
|
I'm not an expert on Winforms, but once you have a DataSet, shouldn't you be able to manipulate it the way you want?
EEmaan wrote: secondly i can't format its one cell acc to my requirement like making that cell's text bold etc
Have you checked out a DataGrid's events?
Cheers,
Vıkram.
Déjà moo - The feeling that you've seen this bull before.
Join the CP group at NationStates. Password: byalmightybob
|
|
|
|
|
hi i am gonna create msi and give it to client
i want to track the installation date(when clients installs my application i need to catch that date PROGRAMATICALLY Now--before i handover to clinet)so that i can give customized check for software update daily/weekly/monthly
hope u got my question
help me plz
prashanth,
s/w Engineer,
Syfnosys.
|
|
|
|
|
One possible approach is to write a custom action in the installer that encrypts a string version of the date/time and writes it to the registry. Since it is running in the installer, you will capture the date (and approximate time) when the client installed the software. Making it encrypted (and possibly using a non-human-readable registry key name, like a guid) and storing it is a binary registry value will help prevent someone from tampering with the value.
You can then read the value from the registry, decrypt it, and do any date comparisons you need.
-----------------------------
In just two days, tomorrow will be yesterday.
|
|
|
|
|
can anyone tell me the error inthis chat code.I think it was donein .net2003 and i converted it to 2005.but"PageModule" causes the error.
if ( TB_ToSend.Text.Length > 0)
{
PageModule.Chat.AddMessage(sChannel,
sUser,
TB_ToSend.Text);
Rhydo
|
|
|
|
|
what is PageModule ? What is the error ?
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
Hi all,
i'm having troubles in selecting a specific node in a treeview after selecting it from a datagridview
i.e. i want to select from the tree by selecting in the datagridview
thanx alot
|
|
|
|
|
plz tell me the directive of clsdataclass
I got a code for disscussion forum in .net2003.
when i converted it to 2005.Error"missing directive for clsDtaClass"
Rhydo
|
|
|
|
|
Seems likely to me that you need to buy a VB.NET book and work through it, because you don't really know what you're doing.
clsDtaClass would be a variable in your code. Without seeing the code, it's impossible to tell you what the error is. I tried google and it seems you didn't give the exact text of the error message, or you're the first person ever to get it and need help with it.
Christian Graus - Microsoft MVP - C++
Metal Musings - Rex and my new metal blog
"I am working on a project that will convert a FORTRAN code to corresponding C++ code.I am not aware of FORTRAN syntax" ( spotted in the C++/CLI forum )
|
|
|
|
|
I mean a resource is created in a thread and now I want to access it from another thread.
|
|
|
|
|
You're going to have to explain what you mean by "resource".
|
|
|
|
|
Hello,
I have a windows application. After exiting the application, I also want to kill the exe running. How to do this?
Please reply. Its urgent.
Thanks in advanced
Nana
|
|
|
|
|
Which exe do you want to kill? When you exit your application the running exe is killed. If you want to kill other exe use Process class.
|
|
|
|
|
Yeah I want to kill running exe.
It doesnt get killed.
I can see it in task manager.
Nana
|
|
|
|
|
you have to use the process class like giorgi suggested.
using System.Diagnostics;
Process[] currentProcesses = Process.GetProcessesByName("iexplore");
foreach (Process process in currentProcesses)
process.Kill();
Don't be overcome by evil, but overcome evil with good
|
|
|
|
|
If you're talking about closing your .EXE and you find it still running in TaskManager, you've done something in your code that's preventing it from quitting. This could be cause by any number of problems, like not terminating a thread you've created or properly disposed an object you created, or you haven't shutdown some COM server you instantied, ...
|
|
|
|
|
Or simply the xxx.vshost.exe is still running, but the xxx.exe should not be.
|
|
|
|
|
And if he's talking about the Release version on a client machine?
|
|
|
|
|
First of all Thanks for all replies.
Ok. I found that there an object of a class which I am using.
If I comment this object, it exe gets killed.
What should I do? I want to use this object in my code.
Nana
|
|
|
|
|
Then you have to get the documentation on that object to find out how to shut it down properly before your app closes.
|
|
|
|
|
Thanks.
I found out how to shut it down.
Thanks once again
Nana
|
|
|
|