c# - Create a custom DataGrid's ItemsSource -


i working datagrids struggling binding data since number of columns varies depending of info has showed.

so, have tried create , object contains columns , rows need @ point , binding object itemssource property. since have worked datagridviews in windowsforms have in mind this:

datatable mytable = new datatable();  datacolumn col01 = new datacolumn("col 01"); mytable.columns.add(col01); datacolumn col02 = new datacolumn("col 02"); mytable.columns.add(col02);  datarow row = mytable.newrow(); row[0] = "data01"; row[1] = "data02"; mytable.rows.add(row);  row = mytable.newrow(); row[0] = "data01"; row[1] = "data02"; mytable.rows.add(row); 

but haven't been able find way same thing in wpf since need columns datagridcomboboxcolumns example.

actually have read many post in site, none of them helped me. lost.

could me? need able create table may contain datagridtextcolumns or `datagridcomboboxcolumns, etc, in order bind final object datagrid's itemssource property.

hope can me.

okay, let me try take example similar needs

let's assume use class:

public class myobject {    public int myid;    public string mystring;    public icommand mycommand; } 

and willing display datagrid listing id, , having second column button, property mystring content, which, when clicked, launches icommand mycommand opens in new window whatever want.

here should have on view side:

    <datagrid itemssource="{binding mylist}" autogeneratecolumns="false">         <datagrid.columns>             <datagridtextcolumn header="id" binding="{binding myid}" />             <datagridtemplatecolumn header="buttons">                 <datagridtemplatecolumn.celltemplate>                     <datatemplate>                         <button content="{binding mystring}" command="{binding mycommand}" />                     </datatemplate>                 </datagridtemplatecolumn.celltemplate>             </datagridtemplatecolumn>         </datagrid.columns>     </datagrid> 

this show datagrid taking content in ienumerable<myobject> named 'mylist', , shows 2 columns defined before.

now if need define command. first, recommend read this introductory link mvvm , take relaycommand class (that's we're gonna use problem)

so, in viewmodel, 1 defines mylist, here how should define of useful objects:

public observablecollection<myobject> mylist { get; set; }  // blah blah blah  public void initializemylist() {   mylist = new observablecollection<myobject>();   (int = 0; < 5; i++)   {     mylist.add(initializemyobject(i));   } }  public myobject initializemyobject(int i) {   myobject theobject = new myobject();   theobject.myid = i;   theobject.mystring = "the object " + i;   theobject.mycommand = new relaycommand(param =< this.showwindow(i));   return theobject }  private void showwindow(int i) {   // exammple, here show messagebox   messagebox.show("you clicked on object " + + "!!!"); } 

Comments

Popular posts from this blog

ubuntu - PHP script to find files of certain extensions in a directory, returns populated array when run in browser, but empty array when run from terminal -

php - How can i create a user dashboard -

javascript - How to detect toggling of the fullscreen-toolbar in jQuery Mobile? -