1

I am populating a listview from code behind, why I am doing this scenario is, I need to create columns dynamically from code behind(one column,two,three....). My code looks like below

XAML Code :

<ListView  ItemsSource="{Binding}" Name="lvLOv" Width="400"></ListView>

C# Code:

Output:

enter image description here

But I need to place checkboxes before every row like as below(I need to do this in code behind like above code/ integrate the checkbox code in the above code). I need to place checkall option also in the header

public TestForm() { this.InitializeComponent(); Test(); }

public void Test()
{
    try
    {
        DataTable dt = new DataTable();

        //Create Columns
        dt.Columns.Add("Initial", typeof(string));
        dt.Columns.Add("Name", typeof(string));

        //Adding Rows
        for (int i = 0; i < 3; i++)
        {
            dt.Rows.Add("K" + i, "David" + i);
        }

        GridView gv = new GridView();


        // Create the GridView Columns
        foreach (DataColumn item in dt.Columns)
        {
            GridViewColumn gvc = new GridViewColumn();
            gvc.DisplayMemberBinding = new Binding(item.ColumnName);
            gvc.Header = item.ColumnName;
            gvc.Width = Double.NaN;
            gv.Columns.Add(gvc);
        }

        lvLOv.View = gv;

        //Binding to Listview
        lvLOv.DataContext = dt.DefaultView;

    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }

}

Expected Output:

enter image description here

Can you please let me know how can I achieve this functionality

daniele3004
  • 13,072
  • 12
  • 67
  • 75
Surya
  • 127
  • 3
  • 13
  • 2
    This is *not* how to write WPF. When using WPF, we do *not* programmatically add UI elements. Instead, we use data binding and manipulate data elements. See the [Data Binding Overview](http://msdn.microsoft.com/en-us/library/ms752347(v=vs.110).aspx) page on MSDN for more information. – Sheridan Nov 24 '14 at 14:24
  • You have to assign `ListView.ItemTemplate`. It may not be easy in code behind ([click](http://stackoverflow.com/q/14053804/1997232), [click](http://stackoverflow.com/q/12560249/1997232)). – Sinatr Nov 24 '14 at 14:24
  • Delete all that horrible code and use proper DataBinding and Data Templating. – Federico Berasategui Nov 24 '14 at 14:29
  • Thanks for your suggestions, I know but I am getting different data(with different column count). I know the columns at run time then how can I bind those columns to listview – Surya Nov 24 '14 at 15:42
  • Why is this community so abrasive? – Rei Miyasaka Oct 15 '17 at 18:42
  • did you solve it? I need the same,... cause I need to create dynamic gridview columns, and some of them could be checkboxes – KillemAll Jun 21 '18 at 16:13

1 Answers1

1

Its really easy just do this..

In Design view (Xaml):

<Window.Resources> 
   <DataTemplate x:Key="Chk_Field" DataType="{x:Type GridViewColumn}">
      <CheckBox IsChecked="{Binding chk}" />
   </DataTemplate>
</Window.Resources>

In Code behind:

GridView gridview = new GridView();
Window window = Application.Current.MainWindow;
DataTemplate s = (DataTemplate)window.FindResource("Chk_Field");
gridview.Columns.Add(new GridViewColumn { Header = "Head", CellTemplate = s });

By doing this you can change the celltemplate for each column in code behind..

ArchAngel
  • 634
  • 7
  • 16