且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在datagridview单元格中选择一行?

更新时间:2023-02-05 14:55:31

发布这个答案是因为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.