时间:2019-03-17 标签:c#datagridcellextraction

发布于 2024-10-05 20:56:32 字数 1542 浏览 0 评论 0原文

嘿,我正在尝试将所选项目的字符串存储在数据网格中,因为它有一个文件路径 ID,可用于删除。

不确定我是否以正确的方式处理这件事。

我的专栏看起来像这样

|身份证 |用户编号 |名字 |姓氏 |当前 |图像路径 |

..01...454656.........哈利.........波特.........巫师....ftp://192.168.1.8/ Jellyfish.jpg

我试图在我的数据网格中“选择”,当我删除时,我也会从我的 ftp 服务器中删除。我需要将信息存储在 imagePath coloum 下,这样我就可以进行 ftp 删除。

    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();
}

}

}

我得到的错误:

索引超出范围。必须为非负数且小于集合的大小。参数名称:

此行的索引:

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

我也尝试过

var imagePath = dataGrid1.SelectedItems[6].ToString();< /code>

运气不好:( 我以为我快要拥有它了!!

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!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

む无字情书 2024-10-12 20:56:32

编辑答案

很遗憾地告诉你,我还没有玩过 WPF,所以对于你的问题,我不得不稍微玩一下它:) 我回答你的问题时假设 WPF 几乎与 WinForms 相似。

所以这就是答案:)

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

dataGrid1.SelectedItems 是一个包含所有选定行的数组。所以你想要其中的第一个。如果您不希望用户选择多行,请将 SelectionMode 设置为 Single

首先将 SelectedItem 转换为 DataRowView 类型,然后可以使用它来访问该特定行的列。

抱歉迟到了,希望这会有所帮助:)

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 :)

浮华 2024-10-12 20:56:32

您的“SelectedItems[6]”超出范围。请记住,数组中从“0”而不是“1”开始。

字符串 imagePath = dataGrid1.SelectedItems[5].ToString();

应该有效。

Your "SelectedItems[6]" is out of range. Remember you start with "0" and not "1" in arrays.

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

should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文