2010-12-04 4 views
0

キープしました。負でなく、コレクションのサイズより小さくなければなりません。パラメータ名:indexC#のデータグリッドインデックスが範囲外のこの行のエラーを取得

私のdatagridからvar/string(パス+ファイル名を保持)にセルを保存しようとしていますので、ftpで削除できます。

 private void button2_Click(object sender, RoutedEventArgs e) 
    { 
     var imagePath = dataGrid1.SelectedItems[5].ToString(); 
     Student selected = dataGrid1.SelectedItem as Student; 
     if (selected == null) 
      MessageBox.Show("You must select a student"); 
     else 
     { 
      if (MessageBoxResult.Yes == MessageBox.Show("Are you sure", "delete student", 
       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(); 
    } 

} 

}

+0

はあなたのように見える**選択したアイテムの第六は、**コラムをお勧めします:学生のImagePathを持っている場合

、あなたは少しだけ持っているが、順序再だろうか? –

答えて

1

私はあなたがそれをselectedItemsのとのSelectedItemの間で混同持っていると思います - SelectedItems should return a IEnumerable of the selected items - あなたはのSelectedItemの列にアクセスしようとしているように見えます。

Student selected = dataGrid1.SelectedItem as Student; 
var imagePath = selected.ImagePath; 
+0

私のデータグリッドはこのように見える| ID |番号|ファーストネーム|姓現在の| ImagePath [5]データグリッドからユーザの画像パス名を取得しようとしています。それを私のftpdeleteに入れます。 –

+0

もし私がそれをしたら、[オブジェクト型]の式に[]を付けてインデックス付けすることができないというエラーが出る –

+0

オブジェクトをキャストDataGridColumnに、多分それが助けになるでしょう – Machinarius

関連する問題