Formatting a DataGrid

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jeff Cook

    Formatting a DataGrid

    Hi

    I have a DataGrid that I am using to display a .xml file (that has a schema
    in a .xsd), like this:-

    ds.ReadXmlSchem a(sDataPath + "Data.xsd") ;
    ds.ReadXml(sDat aPath + "Data.xml", XmlReadMode.Inf erSchema);
    grdXXXX.DataSou rce = ds;
    FormatGridColum ns(grdXXXX, ds.Tables["XXXX"]); // my routine below.

    OK so far. Now I want to format the columns in the grid. I can't hard code
    this as I don't know in advance what will be in the xml file.
    I want to do check boxes if the column is boolean otherwise plain boxes,
    except that I want to right justify decimal columns and left justify the
    rest.

    This what I have done so far (not much!):-

    private void FormatGridColum ns(DataGrid Grid, DataTable Table)
    {
    DataGridTableSt yle ts = new DataGridTableSt yle();
    foreach (DataColumn c in Table.Columns)
    {
    ts.GridColumnSt yles.Add(...... .......somethin g
    // test the datatype and set up the formatting ....
    }
    ..........
    }

    Does that make sense? I need a pointer to get me going again!

    Cheers

    Jeff


  • Dmitriy Lapshin [C# / .NET MVP]

    #2
    Re: Formatting a DataGrid

    Jeff,

    In my opinion, this definitely makes sense. You'll also need to tune
    MappingName-s for the table style and for the column styles, as well as keep
    the right sequence of table style/column style creation (MSDN should have
    something on this).

    --
    Dmitriy Lapshin [C# / .NET MVP]
    X-Unity Test Studio

    Bring the power of unit testing to VS .NET IDE

    "Jeff Cook" <jeffcATaspectD OTcoDOTnz> wrote in message
    news:edb9j71cDH A.3948@TK2MSFTN GP11.phx.gbl...[color=blue]
    > Hi
    >
    > I have a DataGrid that I am using to display a .xml file (that has a[/color]
    schema[color=blue]
    > in a .xsd), like this:-
    >
    > ds.ReadXmlSchem a(sDataPath + "Data.xsd") ;
    > ds.ReadXml(sDat aPath + "Data.xml", XmlReadMode.Inf erSchema);
    > grdXXXX.DataSou rce = ds;
    > FormatGridColum ns(grdXXXX, ds.Tables["XXXX"]); // my routine below.
    >
    > OK so far. Now I want to format the columns in the grid. I can't hard[/color]
    code[color=blue]
    > this as I don't know in advance what will be in the xml file.
    > I want to do check boxes if the column is boolean otherwise plain boxes,
    > except that I want to right justify decimal columns and left justify the
    > rest.
    >
    > This what I have done so far (not much!):-
    >
    > private void FormatGridColum ns(DataGrid Grid, DataTable Table)
    > {
    > DataGridTableSt yle ts = new DataGridTableSt yle();
    > foreach (DataColumn c in Table.Columns)
    > {
    > ts.GridColumnSt yles.Add(...... .......somethin g
    > // test the datatype and set up the formatting ....
    > }
    > ..........
    > }
    >
    > Does that make sense? I need a pointer to get me going again!
    >
    > Cheers
    >
    > Jeff
    >
    >[/color]

    Comment

    • Jeff Cook

      #3
      Re: Formatting a DataGrid


      "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
      in message news:u9FjBf4cDH A.2564@TK2MSFTN GP09.phx.gbl...[color=blue]
      > Jeff,
      >
      > In my opinion, this definitely makes sense. You'll also need to tune
      > MappingName-s for the table style and for the column styles, as well as[/color]
      keep[color=blue]
      > the right sequence of table style/column style creation (MSDN should have
      > something on this).[/color]

      Dmitriy

      I'm sure that MSDN does ... I just can't find it! Guess it must be my
      Delphi heritage - I'm not using the right keywords in searches!

      Cheers

      Jeff
      [color=blue]
      >
      > --
      > Dmitriy Lapshin [C# / .NET MVP]
      > X-Unity Test Studio
      > http://x-unity.miik.com.ua/teststudio.aspx
      > Bring the power of unit testing to VS .NET IDE
      >
      > "Jeff Cook" <jeffcATaspectD OTcoDOTnz> wrote in message
      > news:edb9j71cDH A.3948@TK2MSFTN GP11.phx.gbl...[color=green]
      > > Hi
      > >
      > > I have a DataGrid that I am using to display a .xml file (that has a[/color]
      > schema[color=green]
      > > in a .xsd), like this:-
      > >
      > > ds.ReadXmlSchem a(sDataPath + "Data.xsd") ;
      > > ds.ReadXml(sDat aPath + "Data.xml", XmlReadMode.Inf erSchema);
      > > grdXXXX.DataSou rce = ds;
      > > FormatGridColum ns(grdXXXX, ds.Tables["XXXX"]); // my routine below.
      > >
      > > OK so far. Now I want to format the columns in the grid. I can't hard[/color]
      > code[color=green]
      > > this as I don't know in advance what will be in the xml file.
      > > I want to do check boxes if the column is boolean otherwise plain boxes,
      > > except that I want to right justify decimal columns and left justify the
      > > rest.
      > >
      > > This what I have done so far (not much!):-
      > >
      > > private void FormatGridColum ns(DataGrid Grid, DataTable Table)
      > > {
      > > DataGridTableSt yle ts = new DataGridTableSt yle();
      > > foreach (DataColumn c in Table.Columns)
      > > {
      > > ts.GridColumnSt yles.Add(...... .......somethin g
      > > // test the datatype and set up the formatting ....
      > > }
      > > ..........
      > > }
      > >
      > > Does that make sense? I need a pointer to get me going again!
      > >
      > > Cheers
      > >
      > > Jeff
      > >
      > >[/color]
      >[/color]


      Comment

      • Jeff Cook

        #4
        Re: Formatting a DataGrid

        Hi

        I have made progress on displaying my XML data in a DataGrid. I have worked
        out how to set the column formats depending on the contents of the XML file
        and its schema.

        BUT ... I have a mystery. Why doesn't the column alignment work for
        deciaml fields - I try to make them right align, but it they come out left
        aligned like all the other fields.

        Code pasted below.

        Cheers

        Jeff
        =============== =============== ==
        private void FormatGridColum ns(DataGrid Grid, DataTable Table)
        {
        DataGridTableSt yle ts = new DataGridTableSt yle();
        ts.MappingName = Table.Namespace ;
        foreach (DataColumn c in Table.Columns)
        {
        if (c.DataType == System.Type.Get Type("System.Bo olean"))
        {
        DataGridBoolCol umn bc = new DataGridBoolCol umn();
        bc.MappingName = c.ColumnName;
        ts.GridColumnSt yles.Add(bc);
        }
        else
        {
        DataGridTextBox Column tc = new DataGridTextBox Column();
        tc.MappingName = c.ColumnName;
        if (c.DataType == System.Type.Get Type("System.De cimal"))
        {
        tc.Alignment = HorizontalAlign ment.Right; // why doesn't this
        work?
        tc.Format = "c"; // but this DOES work!
        }
        ts.GridColumnSt yles.Add(tc);
        }
        }
        Grid.TableStyle s.Add(ts);
        }


        Comment

        • Dmitriy Lapshin [C# / .NET MVP]

          #5
          Re: Formatting a DataGrid

          Hi Jeff,

          First, about the sequence. The "DataGridTableS tyle Class" MSDN topic gives
          this order in the code example

          a) Populate GridColumnStyle s collections for each TableStyle
          b) Add created TableStyles to the TableStyles collection of the data grid.

          And the "DataGridColumn Style Class" topic adds to that:

          --------------------- Quote begins ---------------------
          CAUTION Always create DataGridColumnS tyle objects and add them to the
          GridColumnStyle sCollection before adding DataGridTableSt yle objects to
          the GridTableStyles Collection. When you add an empty DataGridTableSt yle
          to the collection, DataGridColumnS tyle objects are automatically generated
          for you. Consequently, an exception will be thrown if you try to add new
          DataGridColumnS tyle objects with duplicate MappingName values to the
          GridColumnStyle sCollection.
          ---------------------- Quote ends ----------------------

          As for the decimal formatting, I really don't know what to say. Try setting
          the format first and alignment second, probably - it might be that setting
          the Format property somehow affects the alignment.

          --
          Dmitriy Lapshin [C# / .NET MVP]
          X-Unity Test Studio

          Bring the power of unit testing to VS .NET IDE

          "Jeff Cook" <jeffcATaspectD OTcoDOTnz> wrote in message
          news:%234ZyPbcd DHA.2416@TK2MSF TNGP09.phx.gbl. ..[color=blue]
          > Hi
          >
          > I have made progress on displaying my XML data in a DataGrid. I have[/color]
          worked[color=blue]
          > out how to set the column formats depending on the contents of the XML[/color]
          file[color=blue]
          > and its schema.
          >
          > BUT ... I have a mystery. Why doesn't the column alignment work for
          > deciaml fields - I try to make them right align, but it they come out left
          > aligned like all the other fields.
          >
          > Code pasted below.
          >
          > Cheers
          >
          > Jeff
          > =============== =============== ==
          > private void FormatGridColum ns(DataGrid Grid, DataTable Table)
          > {
          > DataGridTableSt yle ts = new DataGridTableSt yle();
          > ts.MappingName = Table.Namespace ;
          > foreach (DataColumn c in Table.Columns)
          > {
          > if (c.DataType == System.Type.Get Type("System.Bo olean"))
          > {
          > DataGridBoolCol umn bc = new DataGridBoolCol umn();
          > bc.MappingName = c.ColumnName;
          > ts.GridColumnSt yles.Add(bc);
          > }
          > else
          > {
          > DataGridTextBox Column tc = new DataGridTextBox Column();
          > tc.MappingName = c.ColumnName;
          > if (c.DataType == System.Type.Get Type("System.De cimal"))
          > {
          > tc.Alignment = HorizontalAlign ment.Right; // why doesn't this
          > work?
          > tc.Format = "c"; // but this DOES work!
          > }
          > ts.GridColumnSt yles.Add(tc);
          > }
          > }
          > Grid.TableStyle s.Add(ts);
          > }
          >
          >[/color]

          Comment

          • Jeff Cook

            #6
            Re: Formatting a DataGrid

            Dmitriy

            "Dmitriy Lapshin [C# / .NET MVP]" <x-code@no-spam-please.hotpop.c om> wrote
            in message news:%23KP%231U ddDHA.2416@TK2M SFTNGP09.phx.gb l...[color=blue]
            > Hi Jeff,
            >
            > First, about the sequence. The "DataGridTableS tyle Class" MSDN topic gives
            > this order in the code example
            >
            > a) Populate GridColumnStyle s collections for each TableStyle
            > b) Add created TableStyles to the TableStyles collection of the data grid.
            >
            > And the "DataGridColumn Style Class" topic adds to that:
            >
            > --------------------- Quote begins ---------------------
            > CAUTION Always create DataGridColumnS tyle objects and add them to the
            > GridColumnStyle sCollection before adding DataGridTableSt yle objects to
            > the GridTableStyles Collection. When you add an empty DataGridTableSt yle
            > to the collection, DataGridColumnS tyle objects are automatically generated
            > for you. Consequently, an exception will be thrown if you try to add new
            > DataGridColumnS tyle objects with duplicate MappingName values to the
            > GridColumnStyle sCollection.
            > ---------------------- Quote ends ----------------------[/color]

            I have looked at what you have quoted above and at my code below and I must
            be missing the point. As far as I can see, I am creating a
            DataGridTableSt yle, then adding GridColumnStyle s to that and finally adding
            the DataGridTableSt yle to the DataGrid.TableS tyle. I have tried
            DataGrid.TableS tyle.Clear() before adding as well. Maybe I misunderstand
            the quoted stuff? I can't be doing it VERY wrong as the code does make the
            boolean fields into check boxes and the decimal fields do display as
            currency - it is just this alignment that is wrong.
            [color=blue]
            >
            > As for the decimal formatting, I really don't know what to say. Try[/color]
            setting[color=blue]
            > the format first and alignment second, probably - it might be that setting
            > the Format property somehow affects the alignment.
            >[/color]

            I thought "YES", that'll be it. Changed and it didn't make any difference
            :-(

            Thanks for your help

            Cheers

            Jeff
            [color=blue]
            > "Jeff Cook" <jeffcATaspectD OTcoDOTnz> wrote in message
            > news:%234ZyPbcd DHA.2416@TK2MSF TNGP09.phx.gbl. ..[color=green]
            > > Hi
            > >
            > > I have made progress on displaying my XML data in a DataGrid. I have[/color]
            > worked[color=green]
            > > out how to set the column formats depending on the contents of the XML[/color]
            > file[color=green]
            > > and its schema.
            > >
            > > BUT ... I have a mystery. Why doesn't the column alignment work for
            > > deciaml fields - I try to make them right align, but it they come out[/color][/color]
            left[color=blue][color=green]
            > > aligned like all the other fields.
            > >
            > > Code pasted below.
            > >
            > > Cheers
            > >
            > > Jeff
            > > =============== =============== ==
            > > private void FormatGridColum ns(DataGrid Grid, DataTable Table)
            > > {
            > > DataGridTableSt yle ts = new DataGridTableSt yle();
            > > ts.MappingName = Table.Namespace ;
            > > foreach (DataColumn c in Table.Columns)
            > > {
            > > if (c.DataType == System.Type.Get Type("System.Bo olean"))
            > > {
            > > DataGridBoolCol umn bc = new DataGridBoolCol umn();
            > > bc.MappingName = c.ColumnName;
            > > ts.GridColumnSt yles.Add(bc);
            > > }
            > > else
            > > {
            > > DataGridTextBox Column tc = new DataGridTextBox Column();
            > > tc.MappingName = c.ColumnName;
            > > if (c.DataType == System.Type.Get Type("System.De cimal"))
            > > {
            > > tc.Alignment = HorizontalAlign ment.Right; // why doesn't this
            > > work?
            > > tc.Format = "c"; // but this DOES work!
            > > }
            > > ts.GridColumnSt yles.Add(tc);
            > > }
            > > }
            > > Grid.TableStyle s.Add(ts);
            > > }
            > >
            > >[/color]
            >[/color]


            Comment

            Working...