如何选择一个DataGridView细胞内一排? [英] How to select a row inside a datagridview cell?

查看:164
本文介绍了如何选择一个DataGridView细胞内一排?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的用户:我有使用与C锐利Windows窗体,即时通讯一个DataGridView,这DataGridView中有列如下:

Dear users: I have a datagridview that im using in a windows form with c sharp, this datagridview has columns as follows:


  • 名称

  • 价格

  • 详细信息

我有按钮,将每个这种细胞的设置值,一行只能包含一个名称和价格,但一些细节的线条,为至极目的IVE分离与environmental.newline每个条目,以防条目的详细信息。

I have buttons that will set values for each of this cells, a row can contain only one name and price, but several detail lines, for wich purpose ive separated each entry with a environmental.newline, in case the entry is for details.

行,所以我希望你的想法,现在真正有趣的部分是我希望用户能够CLIC并选择该子行属于datagriview排为这个项目里面之一。也是这个数据网格不绑定任何表。可以这样做?感谢ü所有先进

Ok so i hope u get the idea, now the real interesting part is i want the user to be able to clic and select one of this subrows that are inside the datagriview row for this item. also this datagrid is not binded to any table. Can this be done? thank u all in advanced

推荐答案

发布这个答案,因为OP请求它。

Posting this answer because the OP requested it.

这是你会怎么做,在WPF:

This is how you would do that in WPF:

<Window x:Class="MiscSamples.ListBoxInCell"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="ListBoxInCell" Height="300" Width="300">
    <DockPanel>
        <Button Content="Show Selected Detail" DockPanel.Dock="Bottom"
                Click="ShowDetail"/>

        <ListView ItemsSource="{Binding Items}"
              SelectedItem="{Binding SelectedItem}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="Producto" DisplayMemberBinding="{Binding Product}"/>
                    <GridViewColumn Header="Detalle">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <ListBox ItemsSource="{Binding Details}"
                                     SelectedItem="{Binding SelectedDetail}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </DockPanel>
</Window>

代码背后:

public partial class ListBoxInCell : Window
{
    public ViewModel ViewModel { get; set; }

    public ListBoxInCell()
    {
        InitializeComponent();

        DataContext = ViewModel = new ViewModel();
    }

    private void ShowDetail(object sender, RoutedEventArgs e)
    {
        MessageBox.Show(ViewModel.SelectedItem.SelectedDetail);
    }
}



视图模型:

ViewModel:

public class ViewModel
{
    public List<Data> Items { get; set; }

    public Data SelectedItem { get; set; }

    public ViewModel()
    {
        //Sample Data
        Items = Enumerable.Range(0, 100).Select(x => new Data
            {
                Product = "Product" + x.ToString(),
                Details = Enumerable.Range(0, 3)
                                    .Select(d => "Detail" + x.ToString() + "-" + d.ToString())
                                    .ToList()
            }).ToList();
        SelectedItem = Items.First();
    }
}



数据项:

Data Item:

public class Data
{
    public string Product { get; set; }

    public List<string> Details { get; set; } 

    public string SelectedDetail { get; set; }
}



结果:

Result:


  • MVVM,这意味着数据是从UI分离和UI是从数据分开的。

  • 没有业主抽奖,也没有的P / Invoke(知道是什么意思),并没有可怕的黑客程序。只有美丽的XAML和数据绑定。

  • 每个的ListBox 里面的每一行都是indiviual的选择,您可能希望只保留1所选项目所有其他的用一个简单的LINQ查询和迭代设置为

  • 单击按钮查看当前选择的详细信息以便在ListView当前选中的行

  • WPF岩石,复制和粘贴在我的代码文件 - 过夜。;新建项目 - > WPF应用程序并查看结果自己。

  • 忘记的WinForms。没用的。它不支持自定义任何级别和需要的一切了很多可怕的黑客。它不支持(实际)数据绑定和强制你进入一个无聊的程序方法。

  • MVVM, which means data is separate from UI and UI is separate from data.
  • No "owner draw", no "P/Invoke" (whatever that means), and no horrible procedural hacks. Only beautiful XAML and DataBinding.
  • The selection of each ListBox inside each row is indiviual, you may want to keep only 1 selected Item by setting all the others to null with a simple LINQ query and an iteration.
  • click on the button to see the currently selected Detail for the currently selected Row in the ListView.
  • WPF Rocks, copy and paste my code in a File -> New Project -> WPF Application and see the results for yourself.
  • Forget winforms. It's useless. It doesn't support any level of customization and requires a lot of horrible hacks for everything. It doesn't support (real) DataBinding and forces you into a boring procedural approach.

这篇关于如何选择一个DataGridView细胞内一排?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆