Welcome to Dream.In.Code
Getting C# Help is Easy!

Join 136,837 C# Programmers for FREE! Get instant access to thousands of C# experts, tutorials, code snippets, and more! There are 1,578 people online right now. Registration is fast and FREE... Join Now!




File names input to List Views

 
Reply to this topicStart new topic

File names input to List Views

Typewriter
1 Oct, 2008 - 02:51 PM
Post #1

New D.I.C Head
*

Joined: 1 Oct, 2008
Posts: 2


My Contributions

Hi, I am new to using C# but sadly quite stuck.

I'm trying to populate a column in my list view with the name of any selected files which the user has picked from within a dialog box.

Currently I can get the name of the file to be input to a label but not in to any of the columns in the list view.

I have used the following code to add to the label:



CODE

OpenFileDialog opensinglefile_dr = new OpenFileDialog();
opensinglefile_dr.ShowDialog();

label1.Text = opensinglefile_dr.FileName;


I have tried replacing the lastline from above, which puts the name in to the label with:

CODE

fileinfoViewer.filename.Text = opensinglefile_dr.FileName;


I thought it would be [listview name].[column header name].Text = dialog box but this doesn't work.

Any suggestions at all?

Thanks


User is offlineProfile CardPM
+Quote Post

PsychoCoder
RE: File Names Input To List Views
1 Oct, 2008 - 03:16 PM
Post #2

using DIC.Core;
Group Icon

Joined: 26 Jul, 2007
Posts: 8,997



Thanked: 125 times
Dream Kudos: 8625
Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, Boo.Net

My Contributions
When looking to change the text in an item in a ListView in a specific column you need to be looking into the SubItem property. Here's an example. I put a ListView on a form, set it's view to View.Details (that shows column headers, etc), I then created two ListViewItems and added SubItems to them. At the end I changed the text of the second column in the first row by using ListView1.Items(0).SubItems(1).Text = "Hello". Here's the example:

csharp

private void Form2_Load(object sender, System.EventArgs e)
{
ListView1.View = View.Details;
ListViewItem item1 = new ListViewItem("item1");
item1.SubItems.Add("Item 1");
item1.SubItems.Add("Item 2");


ListViewItem item2 = new ListViewItem("item2");
item2.SubItems.Add("Item 1");
item2.SubItems.Add("Item 2");

ListView1.Columns.Add("Column 1");
ListView1.Columns.Add("Column 2");

ListView1.Items.AddRange(new ListViewItem[] {item1, item2});
ListView1.Items[0].SubItems[1].Text = "Hello";
}


EDIT: Oops, posted VB.NET code, sorry. Updated to C# blush.gif
User is offlineProfile CardPM
+Quote Post

Typewriter
RE: File Names Input To List Views
4 Oct, 2008 - 09:44 AM
Post #3

New D.I.C Head
*

Joined: 1 Oct, 2008
Posts: 2


My Contributions

Thanks for the reply there. I was able to understand part of what you meant and used the following line to get the dialog box to add text to the first column in the list view.


CODE


fileinfoViewer.Items.Add(openSingleFile.FileName);




The problem I have now encountered is that this does not just add the name of the file but the path to the file as well so it adds:

C:\New Folder\TestImport.doc

When all I want to add is:

TestImport.doc

I've tried using all sorts of things as well like:

CODE


string fileNameOnly = System.IO.Path.GetFileName(openSingleFile);
fileinfoViewer.Items.Add(fileNameOnly);



I can't get anything at all to work though, I just keep getting error/debug messages no matter what I try. This is only the start of my problems as well!

Anything at all to point me in the right direction would be a great help!




User is offlineProfile CardPM
+Quote Post

gabehabe
RE: File Names Input To List Views
4 Oct, 2008 - 03:04 PM
Post #4

Donkey DIC
Group Icon

Joined: 6 Feb, 2008
Posts: 5,551



Thanked: 98 times
Dream Kudos: 2650
Expert In: ruling the world.

My Contributions
Simple. Rather than using FullName, just use Name

smile.gif

Example:
openSingleFile.Name
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 12/3/08 05:05PM

Live C# Help!

C# Tutorials

Reference Sheets

C# Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month