Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/304.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#数据网格索引超出范围_C#_.net_Wpf_Wpfdatagrid - Fatal编程技术网

C#数据网格索引超出范围

C#数据网格索引超出范围,c#,.net,wpf,wpfdatagrid,C#,.net,Wpf,Wpfdatagrid,继续获取此行的错误: var imagePath = dataGrid1.SelectedItems[5].ToString(); 索引超出范围。必须为非负数且小于集合的大小。参数名称:索引 尝试将数据网格中的单元格存储在var/字符串中(它包含路径+文件名),以便通过ftp删除它 private void button2_Click(object sender, RoutedEventArgs e) { var imagePath = dataGrid

继续获取此行的错误:

var imagePath = dataGrid1.SelectedItems[5].ToString();
索引超出范围。必须为非负数且小于集合的大小。参数名称:索引

尝试将数据网格中的单元格存储在var/字符串中(它包含路径+文件名),以便通过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();
    }

}

}

我认为您将它混淆在SelectedItems和SelectedItem之间--看起来您正在尝试访问SelectedItem的一列

如果学生有一个ImagePath,您可以重新订购一点,只需:

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

我的datagrid看起来像这样| ID | Number | FirstName | LastName | Current | imagePath[5]我试图从datagrid中获取用户的图像路径名,然后将其发送到我的ftpdeletei如果我这样做,我会收到一个错误,说不能对“object”类型的表达式应用带[]的索引将对象转换到DataGridColumn中,也许这会对我的答案的实时编辑有帮助-你几乎是靠你自己的steam完成的。克里斯托,你能在这里帮忙吗?:看起来你可能想要所选项目的第6列?