c# - Make some of DataGrid cells span multiple columns -


ok, have searched pretty long time solution of problem. developing simple printing system wpf datagrids , have managed print tables uniform cell placement using datatable , setting datagrid's itemsource.

however, needed some rows contain 1 cell (you can think of "row group header" inside table).

so, since haven't found datatable's cells spanning multiple columns (if can made, great thing know how), figured have add rows datagrid manually, , solve this:

  • make new datagrid desired columns
  • add rows 1 one, setting datagridcellpanel spans or not spans through rows

the second point have problem (if it's right, is). need add row datagrid uses simple array of strings cell data (index in array should mach cell index). there easy way that?

so after more fiddling this, have reached nice solution.

the best , easiest thing apply data template particular rows after datagrid loaded. so, stuck original idea datatables , remembered indices needed have template changed. took datagridrows these indices , applied template had custom made itemspaneltemplate spans multiple columns.

edit: on daniel's request, i'm including code.

first thing need template spanning row:

<controltemplate targettype='{x:type datagridrow}'             xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation'             xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml'             >             <border>                 <datagridcellspresenter foreground='black'>                     <datagridcellspresenter.itemspanel>                         <itemspaneltemplate>                             <local:datagridspannedcellpanel />                         </itemspaneltemplate>                     </datagridcellspresenter.itemspanel>                 </datagridcellspresenter>             </border>         </controltemplate> 

note: local:datagridspannedcellpanel custom datagridcellspanel overridden arrangeoverride method makes first cell span entire size.

you can make string in code-behind , load template it, example. next thing creating grid , initializing of rows new template:

var newgrid = makenewdatagrid(); newgrid.itemssource = mytable.asdataview(); var template = xamlreader.parse(headerrowtemplate) controltemplate;  foreach (int index in myheaderindices)                     {                         var container = newgrid.itemcontainergenerator.containerfromindex(index);                         var row = container datagridrow;                         if (row != null)                         {                             row.template = template;                         }                     } 

also note, rows in table need made follows:

if (bindablequote.isgroup())                         {                              table.rows.add("header");                         }                         else table.rows.add(rowdata.toarray()); 

that's it, thing left figure out how implement datagridspannedcellpanel.


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -