[英]C# Retrieving data from ListView with GridView
I have found some articles to get data to be put into a listview with a databinding on gridviewcolums but I am needing to retrieve this data when a line is selected from the list, this is the code i have to fill the list and "attempt" to retrieve the data. 我发现一些文章可以将数据放入带有gridviewcolums的数据绑定的列表视图中,但是当我从列表中选择一行时,我需要检索此数据,这是我必须填写列表并“尝试”的代码检索数据。 As far as i have found, please have very different ways of putting this data into a listview and I assume my problem is that I do not know the correct way to retrieve it with it formatted in the XAML this way.
据我发现,请使用非常不同的方法将此数据放入列表视图,我想我的问题是我不知道用XAML格式格式化的数据检索数据的正确方法。
private void Button_Click_1(object sender, RoutedEventArgs e)
{ // This is When a button is clicked to populate the List
Operate op = new Operate();
List<object> users = op.GetUser();
if (users != null)
{
ResultsView.Items.Clear();
foreach (UserPrincipal user in users)
{
ResultsView.Items.Add(new {Col1 = user.GivenName, Col2 = user.Surname, Col3 = user.SamAccountName});
}
}
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{ // This is When a line is selected and should get the info from the line
if (ResultsView.SelectedItem != null)
{
string sel = ResultsView.SelectedItem.Col1;
}
}
And this is the XAML for the list 这是列表的XAML
<ListView
x:Name="ResultsView"
Margin="5,5,5,5"
SelectionMode="Single"
SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="H1" DisplayMemberBinding="{Binding Col1}"/>
<GridViewColumn Width="100" Header="H2" DisplayMemberBinding="{Binding Col2}"/>
<GridViewColumn Width="100" Header="H3" DisplayMemberBinding="{Binding Col3}"/>
</GridView>
</ListView.View>
</ListView>
Very sorry about any weird formatting or errors, just starting with this a few days ago. 对于任何奇怪的格式或错误,我们非常抱歉,就从几天前开始。 Here is a picture of the list when it is populated, looks as I expect it to.
这是列表的填充图片,看起来与我期望的一样。
My answer was in Travis J's comment somewhat. 我的回答是特拉维斯·J的评论。 I knew that I may have needed to create this table in a different way so that the output of SelectedItem does not come up as anonymous type.
我知道我可能需要以其他方式创建此表,以使SelectedItem的输出不会出现为匿名类型。 So I have redone how the table is constructed and am now using DataTable.
因此,我已经重做了表的构造方式,现在正在使用DataTable。
private void Button_Click_1(object sender, RoutedEventArgs e)
{
Operate op = new Operate();
DataTable users = op.GetUser();
if (users != null)
{
ResultsView.ItemsSource = users.DefaultView;
}
}
private void ListView_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ResultsView.SelectedItem != null)
{
DataRowView line = ResultsView.SelectedItem as DataRowView;
string stuff = line.Row.ItemArray[2].ToString();
MessageBox.Show(stuff);
}
}
And this made a need for the XAML to change so that the binding is looking for a path from the datatable. 这就需要更改XAML,以便绑定从数据表中查找路径。
<ListView
x:Name="ResultsView"
Margin="5,5,5,5"
SelectionMode="Single"
SelectionChanged="ListView_SelectionChanged">
<ListView.View>
<GridView>
<GridViewColumn Width="100" Header="H1" DisplayMemberBinding="{Binding Path=FirstName}"/>
<GridViewColumn Width="100" Header="H2" DisplayMemberBinding="{Binding Path=LastName}"/>
<GridViewColumn Width="100" Header="H3" DisplayMemberBinding="{Binding Path=UserName}"/>
</GridView>
</ListView.View>
</ListView>
I have decided not to include my own method for creating the datatable but I will provide the link that showed me the quickest way. 我决定不包括自己的创建数据表的方法,但是我将提供向我展示最快方法的链接。
http://www.dotnetperls.com/datatable http://www.dotnetperls.com/datatable
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.