如何从listview/gridview获取数据并填充另一个listview/gridview?
使用带有绑定的 XAML listview/gridview 我使用 linq 查询和 C# 代码填充网格:
AdventureWorkEntities awDatabase = new AdventureWorkEntities();
var products = from p in awDatabase.Products
from i in awDatabase.ProductInventories
where p.ProductID == i.ProductID && p.ListPrice > 0
&& p.Name.Contains(search.Text.Trim())
select new
{
p.ListPrice,
p.Name,
p.ProductNumber,
p.DaysToManufacture,
i.Quantity
};
IListSource query = (IListSource)products;
ProductsList.ItemsSource = query.GetList();
我注意到所选项目的双击事件。能够双击一行并将该行传输到其下方的另一个网格中的语法是什么?
主要是我希望能够通过双击一个网格中的所选项目来将一个网格中的一行添加到另一个网格中。
编辑:XAML代码:
<ListView Name="ProductsList" IsSynchronizedWithCurrentItem="True" DataContext="{Binding}"
Margin="6,76,6,220" Width="726" MouseDoubleClick="ProductsList_MouseDoubleClick">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="85" Header="Product Number"
DisplayMemberBinding="{Binding Path=ProductNumber}"/>
<GridViewColumn Width="225" Header="Name"
DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Width="135" Header="Days To Manufacture"
DisplayMemberBinding="{Binding Path=DaysToManufacture}"/>
<GridViewColumn Width="75" Header="Quantity"
DisplayMemberBinding="{Binding Path=Quantity}"/>
<GridViewColumn Width="75" Header="List Price"
DisplayMemberBinding="{Binding Path=ListPrice}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
和我的尝试:
private void ProductsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//code to place the contents of top grid to bottome grid
List<IQueryable> selectedContents = new List<IQueryable>();
selectedContents.Add((IQueryable)ProductsList.SelectedValue);
IListSource query = (IListSource)selectedContents;
OrderContents.ItemsSource= query.GetList();
}
Using the XAML listview/gridview with binding I populate grid using linq query and C# code:
AdventureWorkEntities awDatabase = new AdventureWorkEntities();
var products = from p in awDatabase.Products
from i in awDatabase.ProductInventories
where p.ProductID == i.ProductID && p.ListPrice > 0
&& p.Name.Contains(search.Text.Trim())
select new
{
p.ListPrice,
p.Name,
p.ProductNumber,
p.DaysToManufacture,
i.Quantity
};
IListSource query = (IListSource)products;
ProductsList.ItemsSource = query.GetList();
I noticed a double click event for selected items. What is the syntax to be able to double click a row and transfer that row into another grid below it?
The jist is that I'd like to be able to add one row from one grid to the other grid by double clicking the selected item from one grid.
Edit: XAML Code:
<ListView Name="ProductsList" IsSynchronizedWithCurrentItem="True" DataContext="{Binding}"
Margin="6,76,6,220" Width="726" MouseDoubleClick="ProductsList_MouseDoubleClick">
<ListView.View>
<GridView>
<GridView.Columns>
<GridViewColumn Width="85" Header="Product Number"
DisplayMemberBinding="{Binding Path=ProductNumber}"/>
<GridViewColumn Width="225" Header="Name"
DisplayMemberBinding="{Binding Path=Name}"/>
<GridViewColumn Width="135" Header="Days To Manufacture"
DisplayMemberBinding="{Binding Path=DaysToManufacture}"/>
<GridViewColumn Width="75" Header="Quantity"
DisplayMemberBinding="{Binding Path=Quantity}"/>
<GridViewColumn Width="75" Header="List Price"
DisplayMemberBinding="{Binding Path=ListPrice}"/>
</GridView.Columns>
</GridView>
</ListView.View>
</ListView>
And my Attempt:
private void ProductsList_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
//code to place the contents of top grid to bottome grid
List<IQueryable> selectedContents = new List<IQueryable>();
selectedContents.Add((IQueryable)ProductsList.SelectedValue);
IListSource query = (IListSource)selectedContents;
OrderContents.ItemsSource= query.GetList();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
由于缺乏语法知识,我无法弄清楚这一行。
My lack of knowledge of the syntax prevented me from figuring out this one liner.
您需要创建实体的可观察集合...并在双击事件中从 listView 获取所选实体并将所选实体添加到该可观察集合中并将该集合与第二个列表视图绑定...
You need to make the Observable Collection of the Entity ...and in the Double click event get the selected entity from the listView and add the selected Entity into that Observable Collection and bind the collection with the second listview....