如何在datagridview单元格中选择一行? [英] How to select a row inside a datagridview cell?

查看:215
本文介绍了如何在datagridview单元格中选择一行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的用户:我有一个datagridview,它使用的窗体是c sharp,这个datagridview有如下列:

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



  • 价格

  • 细节

将为每个单元格设置值的按钮,一行可以只包含一个名称和价格,但是几个细节行,用于将每个条目与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行内的这个subrows之一。此datagrid也不绑定到任何表。这可以做吗感谢你所有的高级

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:

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

数据项:

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

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

    public string SelectedDetail { get; set; }
}

结果:


  • MVVM,这意味着数据与UI分离,UI与数据分开。

  • 没有所有者绘制,没有P / Invoke(无论如何)没有可怕的程序性黑客。只有美丽的XAML和DataBinding。

  • 每行内的每个 ListBox 的选择是单独的,您可能只想保留1个选定项通过使用简单的LINQ查询和迭代将所有其他人设置为 null

  • 点击按钮查看当前选定的详细信息对于ListView中当前选定的行。

  • WPF摇滚,将我的代码复制并粘贴到文件中 - >新项目 - > WPF应用程序,并自行查看结果。

  • 忘记winforms。没用的。它不支持任何级别的定制,并需要很多可怕的黑客的一切。它不支持(真实的)DataBinding,并强制您进入无聊的程序方法。

  • 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天全站免登陆