时间:2019-03-17 标签:c#datagridcellextraction
嘿,我正在尝试将所选项目的字符串存储在数据网格中,因为它有一个文件路径 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑答案
很遗憾地告诉你,我还没有玩过 WPF,所以对于你的问题,我不得不稍微玩一下它:) 我回答你的问题时假设 WPF 几乎与 WinForms 相似。
所以这就是答案:)
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 :)
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 theSelectionMode
toSingle
.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 :)
您的“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.