Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I make a Grid with multiple Rows and Columns.

I want to apply different types of Dividers and Alignment to that Grid.

enter image description here

If you can observe above picture,

1.First row Alignment is Left,and remaining all the rows Alignment is center.

2.in the second row only Dividers are appeared

I want like this,for this purpose I wrote a simple code but it was nor working

 Grid[{{Style["Life Alerts", Bold, 20], SpanFromLeft, SpanFromLeft}, 
       {Style["Alert", Bold, 15], Style["<>", Bold, 15], 
        Style["%Life", Bold, 15], 
        Style["OnDay", Bold, 15]
       },
       {Style["L1", Bold, 15], 
        Style["<", Bold, 15], Style["90", Bold, 15], 
        Style["0.35", Bold, 15]
       },
       {Style["L2", Bold, 15], 
        Style[">", Bold, 15], Style["90", Bold, 15], 
        Style["0.35", Bold, 15]
       },
       {Style["L3", Bold, 15], 
        Style[">", Bold, 15], 
        Style["90", Bold, 15], 
        Style["0.35", Bold, 15]
       }
      },
     Alignment -> {{Left}, Center}, Background -> {None, {Yellow}}, Frame -> True, 
   FrameStyle -> Directive[Thickness[4], Black], Spacings -> {1, 1}, 
   Dividers -> {{}, {True, True, True}}]

Already I seen documentation still I didn't get it

can anybody help me?

share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

another way

  Grid[{
  {Item["life alerts",Frame->True, Background -> Green,Alignment -> Left], SpanFromLeft},
  {Item["Alert", Frame -> True], Item["% Life",Frame->True],Item["On day",Frame-> True]},
  {"L1", 98, .52},
  {"L2", 97, .77},
  {"L3", 95, 1.27}
  }, Alignment -> {Center}, Frame -> True]

Mathematica graphics

This reference here http://reference.wolfram.com/mathematica/tutorial/GridsRowsAndColumns.html

has good table to keep handy. Here is a copy of it.

Mathematica graphics

Notice that Dividers only work on the whole grid at once. Not on specific rows/columns. So to do the above in another way, need to use Frame, not Dividers. Either way, I always had hard time with this, so you are not alone. I found the Item trick by accident one day when I was trying something, and I use it when needed for special lines like in this case. Item is an amazing function. I use it for another very important thing inside Manipulate.

You'll find Another useful function on this page to make it easy to add divider between rows written by Heike. I used it few times myself. It makes it very easy to add a line between rows.

http://stackoverflow.com/questions/8875970/any-trick-to-add-a-divider-inside-a-grid-between-rows-at-the-point-it-is-needed

share|improve this answer
    
Dividers definitely do work on specific rows and columns. Descriptions of this are given in the docs. –  Mike Honeychurch Dec 24 '12 at 9:07
    
I was commenting about how dividers can be used. I never said any tables were wrong. –  Mike Honeychurch Dec 24 '12 at 21:33
    
ok now we are on the same page. The table is clearly wrong because divider usage for rows and columns appears in the documentation-- in fact even in the very same tutorial. And I also notice that is says that Row does not take any options which is also incorrect. –  Mike Honeychurch Dec 25 '12 at 0:05
    
They may mean "elements within a row or column" ...which would be correct. But then again there is the statement about Row not having options which is unambiguous (and wrong) so who knows what the intentions of the author were. –  Mike Honeychurch Dec 25 '12 at 0:27
add comment

If you want to style a grid it is better IMO to do this with Grid options and keep content and styling separate: Grid[content, options(styling)]. Wrapping data elements in Item is (manually) inefficient and soon becomes impractical as the size of the data grows. It is also inefficient/impractical to do this if you are generating data dynamically in some way. Therefore it is best to leave data as it is and use it directly as the first argument to Grid and then adjust your styling with the Grid options.

If you want a grid like in the image in your question then try these Grid options -- using your code with the second column removed (not sure why the second column is there).

Grid[{{Style["Life Alerts", Bold, 20], SpanFromLeft, 
   SpanFromLeft}, {Style["Alert", Bold, 15], Style["%Life", Bold, 15],
    Style["OnDay", Bold, 15]}, {Style["L1", Bold, 15], 
   Style["90", Bold, 15], 
   Style["0.35", Bold, 15]}, {Style["L2", Bold, 15], 
   Style["90", Bold, 15], 
   Style["0.35", Bold, 15]}, {Style["L3", Bold, 15], 
   Style["90", Bold, 15], Style["0.35", Bold, 15]}},

 (* options *) 
 Alignment -> {{Center, 1 -> Left}, Center}, 
 Background -> {None, {Green}}, 
 Frame -> {Automatic, Automatic, {{2, 1} -> Blue, {2, 3} -> True}}, 
 FrameStyle -> Directive[Thickness[4], Black], 
 Dividers -> {{1 -> True, -1 -> True}, {1 -> True, 2 -> Red, 
    3 -> Directive[AbsoluteThickness[1], Orange], -1 -> True}},
Spacings -> {1, 1}]

Note that I made a few departures with the styling so you could see how to make specific one off changes to divider styling. Also rather than wrap each element in an identical Style it would be better to set an ItemStyle (or BaseStyle, you could also add a BaseStyle choosing font and font size etc.):

Grid[{{"Life Alerts", SpanFromLeft, SpanFromLeft}, {"Alert", "%Life", 
   "OnDay"},
  {"L1", 98, .52}, {"L2", 97, .77}, {"L3", 95, 1.27},
  {"L4", 85, 3.21}, {"L5", 60, 6.23}},(*options*)
 Alignment -> {Center, Center, {{1, 1} -> Left}},
 Background -> {None, {Lighter[Green, 0.6]}}, 
 Frame -> {Automatic, Automatic, {{2, 1} -> True, {2, 3} -> True}}, 
 FrameStyle -> Directive[Thickness[4], Black], 
 Dividers -> {{1 -> True, -1 -> True}, {1 -> True, 2 -> True, 
    3 -> True, -1 -> True}},
 ItemStyle -> {Automatic, {Automatic, 1 -> Bold}},
 ItemSize -> 6,
 Spacings -> {1, 1}]

The advantage of this approach is that for large grids it is easier to maintain than wrapping elements in Item -- grid content is separate from the styling (which is controlled by the grid options).

enter image description here

share|improve this answer
    
@NasserM.Abbasi grid styling is something I do regularly but initially it was trial an error. –  Mike Honeychurch Dec 24 '12 at 21:38
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.