c# datagrid cell extraction

Hey im trying to store a string from a selected item in a datagrid as it has a filepath id like to use to delete.

Not sure im going about it the right way.

my colums look like this

| ID | UserNumber | FirstName | LastName | Current | imagePath |

..01...454656.........Harry..........Potter.........Wizard....ftp://192.168.1.8/Jellyfish.jpg

im trying to "on selection" in my datagrid when i delete i also delete from my ftp server. I need the info stored under imagePath coloum so I can then do my ftp delete.

    private void button2_Click(object sender, RoutedEventArgs e)
{
    string imagePath = dataGrid1.SelectedItems.ToString();
    Student selected = dataGrid1.SelectedItem as Stu;
    if (selected == null)
        MessageBox.Show("You must select a user");
    else
    {
        if (MessageBoxResult.Yes == MessageBox.Show("Are you sure", "delete user",
            MessageBoxButton.YesNo, MessageBoxImage.Warning))
        {
            FTPdelete(imagePath, "Administrator", "commando");
            Class1.DeleteStudent(selected);
            Window_Loaded(null, null);
        }
    }
}
private void FTPdelete(String imagePath, String inUsername, String inPassword)
{
    var req = (FtpWebRequest)WebRequest.Create(imagePath);
    req.Proxy = null;
    req.Credentials = new NetworkCredential(inUsername, inPassword);

    req.Method = WebRequestMethods.Ftp.DeleteFile;

    req.GetResponse().Close();
}

}

}

the error i get:

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

on this line:

string imagePath = dataGrid1.SelectedItems[6].ToString();

Ive also tryed

var imagePath = dataGrid1.SelectedItems[6].ToString();

No luck :( Thought I almost had it!!

Answers


Edited Answer

Sorry to tell you I have not played with WPF, so just for your question i had to play with it a lil bit :) I answered your question assuming WPF would be almost similar to WinForms.

So here is the answer :)

DataRowView dr = (DataRowView)(dataGrid1.SelectedItems[0]);
MessageBox.Show(dr.Row.ItemArray[5].ToString());

dataGrid1.SelectedItems is an array that contains ALL the rows that are selected. So you want the first one from it. If you don't want your user to select more than one row, set the SelectionMode to Single.

You first cast the SelectedItem to the type DataRowView, and then you can use it to access your columns of that particular row.

Sorry for been late and hope this helps :)


Need Your Help

How can you insure your code runs with no variability in execution time due to cache?

caching time profiling processor

In an embedded application (written in C, on a 32-bit processor) with hard real-time constraints, the execution time of critical code (specially interrupts) needs to be constant.

Image uploading integration test of RESTful webservice with spring mvc

java rest spring-mvc integration-testing mockmvc

How to write a integration test when I'm uploading a image to the server. I've already written a test following this question and it's answer but mine is not working properly. I used JSON to send ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.