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!!
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 🙂
dataGrid1.SelectedItemsis 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 theSelectionModetoSingle.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 🙂