user2793090
user2793090

Reputation: 183

How to select a row inside a datagridview cell?

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

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.

enter image description here

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

Upvotes: 3

Views: 1324

Answers (2)

Fede
Fede

Reputation: 44038

Posting this answer because the OP requested it.

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>

Code Behind:

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:

enter image description here

  • 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.

Upvotes: 2

Huy Nguyen
Huy Nguyen

Reputation: 928

DataGridView don't supports sub-rows in a row. So, my advice is that you should use two DataGridViews:

  • One contains list of Names and Prices.
  • Another one display list of Detail lines that belong to current selected row in the first DataGridView.

Upvotes: 0

Related Questions