Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何选择datagridview单元格内的行?_C#_Winforms_Datagridview - Fatal编程技术网

C# 如何选择datagridview单元格内的行?

C# 如何选择datagridview单元格内的行?,c#,winforms,datagridview,C#,Winforms,Datagridview,亲爱的用户:我有一个datagridview,我在windows窗体中与c sharp一起使用,这个datagridview有如下列: 名字 价格 细部 我有为每个单元格设置值的按钮,一行只能包含一个名称和价格,但有几行详细信息,为了方便起见,我用environment.newline分隔了每个条目,以防条目用于详细信息 好的,我希望你能理解,现在真正有趣的是,我希望用户能够clic并选择datagriview行中该项目的一个子视图。此外,此datagrid未绑定到任何表。这能做到吗?感谢

亲爱的用户:我有一个datagridview,我在windows窗体中与c sharp一起使用,这个datagridview有如下列:

  • 名字
  • 价格
  • 细部
我有为每个单元格设置值的按钮,一行只能包含一个名称和价格,但有几行详细信息,为了方便起见,我用environment.newline分隔了每个条目,以防条目用于详细信息


好的,我希望你能理解,现在真正有趣的是,我希望用户能够clic并选择datagriview行中该项目的一个子视图。此外,此datagrid未绑定到任何表。这能做到吗?感谢您所有的高级

DataGridView不支持一行中的子行。因此,我的建议是您应该使用两个DataGridView:

  • 一个包含名称和价格的列表
  • 另一个显示属于第一个DataGridView中当前选定行的详图线列表

    • 因为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 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();
          }
      }
      
      公共类视图模型
      {
      公共列表项{get;set;}
      公共数据SelectedItem{get;set;}
      公共视图模型()
      {
      //样本数据
      Items=Enumerable.Range(01100)。选择(x=>newdata
      {
      Product=“Product”+x.ToString(),
      详细信息=可枚举范围(0,3)
      .选择(d=>“详细信息”+x.ToString()+“-”+d.ToString())
      托利斯先生()
      }).ToList();
      SelectedItem=Items.First();
      }
      }
      
      数据项:

      public class Data
      {
          public string Product { get; set; }
      
          public List<string> Details { get; set; } 
      
          public string SelectedDetail { get; set; }
      }
      
      公共类数据
      {
      公共字符串乘积{get;set;}
      公共列表详细信息{get;set;}
      公共字符串SelectedDetail{get;set;}
      }
      
      结果:

      • MVVM,这意味着数据与UI分离,UI与数据分离
      • 没有“所有者绘制”,没有“P/Invoke”(不管这意味着什么),也没有可怕的程序性黑客。只有漂亮的XAML和数据绑定
      • 每行中每个
        ListBox
        的选择都是独立的,您可能希望通过简单的LINQ查询和迭代将所有其他项设置为
        null
        ,只保留一个选定项
      • 单击按钮可查看ListView中当前选定行的当前选定详细信息
      • WPF Rocks,将我的代码复制并粘贴到
        文件->新项目->WPF应用程序中
        ,然后自己查看结果
      • 忘了winforms吧。没用。它不支持任何级别的定制,需要对所有内容进行大量可怕的黑客攻击。它不支持(真实的)数据绑定,并迫使您采用枯燥的过程方法

      欢迎来到Stackoverflow。我已经编辑了你的问题。如果您使用的是winforms,请标记您的问题
      [winforms]
      。并包括您遇到问题的任何代码示例。好的,thx,下次我会小心这样做:)如果您能给我一些代码id,请欣赏它=D,我很想在wpf中使用它
      public class Data
      {
          public string Product { get; set; }
      
          public List<string> Details { get; set; } 
      
          public string SelectedDetail { get; set; }
      }