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

时间:2013-09-20 22:01:38

标签: c# winforms datagridview

亲爱的用户:我有一个数据网格视图,我在一个带有c sharp的windows窗体中使用,这个datagridview的列如下:

  • 名称
  • 细节

我有按钮,将为每个单元格设置值,一行只能包含一个名称和价格,但是有几个细节线,用于将每个条目与environment.newline分开,以防条目为的信息。

enter image description here

好的,所以我希望你能得到这个想法,现在真正有趣的部分是我希望用户能够clic并选择这个项目的datagriview行中的一个子行。此数据网格也没有绑定到任何表。可以这样做吗?谢谢大家先进的

2 个答案:

答案 0 :(得分:2)

发布此答案是因为OP要求它。

这就是你在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);
    }
}

视图模型:

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

结果:

enter image description here

  • MVVM,表示数据与UI分开,UI与数据分开。
  • 没有“所有者抽奖”,没有“P / Invoke”(无论这意味着什么),也没有可怕的程序性黑客。只有漂亮的XAML和DataBinding。
  • 每行中每个ListBox的选择是不相同的,您可能希望通过使用简单的LINQ查询和迭代将所有其他项设置为null来仅保留1个所选项。
  • 单击按钮以查看ListView中当前所选行的当前所选详细信息。
  • WPF晃动,将我的代码复制并粘贴到File -> New Project -> WPF Application中,然后自行查看结果。
  • 忘记winforms。这毫无用处。它不支持任何级别的自定义,并且需要很多可怕的黑客。它不支持(真正的)DataBinding,并迫使你采用无聊的程序方法。

答案 1 :(得分:0)

DataGridView不支持连续的子行。所以,我的建议是你应该使用两个DataGridViews:

  • 一个包含名称和价格列表。
  • 属于第一个DataGridView中当前所选行的详细信息行的另一个显示列表。
相关问题