setting datagrid column widths in code?

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

    #1

    setting datagrid column widths in code?

    Hello,

    I have a one datagrid that will be based on different datatables. One
    datatable may have 7 columns, another 15... With the tables that have more
    columns, I have been manually dragging column widths to narrow them. I want
    to record these widths so I can reapply them when I bind the datagrid to the
    table with 15 columns. I have searched around for articles on this and saw
    some that talked about datagrid.TableS tyle.GridColumn Style something, but on
    intellisense of my datagrid I got TableStyles and no GridColumnStyle s. I
    would be very grateful if someone could set me straight on how I can retrieve
    and set column widths for a datagrid from code. Do I need to use a dataview
    maybe to do this?

    Thanks,
    Rich
  • I Don't Like Spam

    #2
    Re: setting datagrid column widths in code?

    Rich wrote:[color=blue]
    > Hello,
    >
    > I have a one datagrid that will be based on different datatables. One
    > datatable may have 7 columns, another 15... With the tables that have more
    > columns, I have been manually dragging column widths to narrow them. I want
    > to record these widths so I can reapply them when I bind the datagrid to the
    > table with 15 columns. I have searched around for articles on this and saw
    > some that talked about datagrid.TableS tyle.GridColumn Style something, but on
    > intellisense of my datagrid I got TableStyles and no GridColumnStyle s. I
    > would be very grateful if someone could set me straight on how I can retrieve
    > and set column widths for a datagrid from code. Do I need to use a dataview
    > maybe to do this?
    >
    > Thanks,
    > Rich[/color]

    I can tell you how to do the GridColumnStyle (it's
    datagrid.TableS tyle(0).GridCol umnStyle) but there is a better way.

    If you add one TableStyle for each of the datatables you are using the
    grid will change automatically depending on which datatable is bound to
    it. The key that seems to get missed often on doing this is to make
    sure the TableStyle.Mapp ingName matches the name of the datatable.

    Hope this helps.
    Chris

    Comment

    • Rich

      #3
      Re: setting datagrid column widths in code?

      I am having a problem setting a table style for my datatable. How do I do
      that?

      Dim dt As DataTable
      dt = Dataset1.Tables ("tbl1")
      dt.???????
      datagrid1.DataS ource = dt

      [color=blue]
      >
      > I can tell you how to do the GridColumnStyle (it's
      > datagrid.TableS tyle(0).GridCol umnStyle) but there is a better way.
      >
      > If you add one TableStyle for each of the datatables you are using the
      > grid will change automatically depending on which datatable is bound to
      > it. The key that seems to get missed often on doing this is to make
      > sure the TableStyle.Mapp ingName matches the name of the datatable.
      >
      > Hope this helps.
      > Chris
      >[/color]

      Comment

      • Chris

        #4
        Re: setting datagrid column widths in code?

        Rich wrote:[color=blue]
        > I am having a problem setting a table style for my datatable. How do I do
        > that?
        >
        > Dim dt As DataTable
        > dt = Dataset1.Tables ("tbl1")
        > dt.???????
        > datagrid1.DataS ource = dt
        >
        >
        >[color=green]
        >>I can tell you how to do the GridColumnStyle (it's
        >>datagrid.Tabl eStyle(0).GridC olumnStyle) but there is a better way.
        >>
        >>If you add one TableStyle for each of the datatables you are using the
        >>grid will change automatically depending on which datatable is bound to
        >>it. The key that seems to get missed often on doing this is to make
        >>sure the TableStyle.Mapp ingName matches the name of the datatable.
        >>
        >>Hope this helps.
        >>Chris
        >>[/color][/color]


        You set the tablestyle of the datagrid.
        (doing this from memory, but you'll get the idea)

        'Do this for each table
        Dim DT as new DataGridTableSt yle
        DT.MappingName = "tbl1" '<--Important!

        'Add one for each column you want displayed
        Dim Column as new DataGridTextBox Column
        Column.Width = 100
        Column.DisplayN ame = ... 'This might be wrong property
        Column.MappingN ame = ... 'This might be wrong property
        DT.DataGridColu mnStyle.Add(Col umn)
        Column = new DataGridTextBox Column
        Column.Width = 150
        Column.DisplayN ame = ... 'This might be wrong property
        Column.MappingN ame = ... 'This might be wrong property
        DT.DataGridColu mnStyle.Add(Col umn)

        DataGrid1.Table Styles.Add(DT)

        Dim dt As DataTable
        datagrid1.DataS ource = Dataset1.Tables ("tbl1")

        Hope this helps
        Chris

        Comment

        • Rich

          #5
          Re: setting datagrid column widths in code?

          Thanks. This should do the trick. I might have to tweak it a little bit.
          But yes I get the idea now.

          Rich

          "Chris" wrote:
          [color=blue]
          > Rich wrote:[color=green]
          > > I am having a problem setting a table style for my datatable. How do I do
          > > that?
          > >
          > > Dim dt As DataTable
          > > dt = Dataset1.Tables ("tbl1")
          > > dt.???????
          > > datagrid1.DataS ource = dt
          > >
          > >
          > >[color=darkred]
          > >>I can tell you how to do the GridColumnStyle (it's
          > >>datagrid.Tabl eStyle(0).GridC olumnStyle) but there is a better way.
          > >>
          > >>If you add one TableStyle for each of the datatables you are using the
          > >>grid will change automatically depending on which datatable is bound to
          > >>it. The key that seems to get missed often on doing this is to make
          > >>sure the TableStyle.Mapp ingName matches the name of the datatable.
          > >>
          > >>Hope this helps.
          > >>Chris
          > >>[/color][/color]
          >
          >
          > You set the tablestyle of the datagrid.
          > (doing this from memory, but you'll get the idea)
          >
          > 'Do this for each table
          > Dim DT as new DataGridTableSt yle
          > DT.MappingName = "tbl1" '<--Important!
          >
          > 'Add one for each column you want displayed
          > Dim Column as new DataGridTextBox Column
          > Column.Width = 100
          > Column.DisplayN ame = ... 'This might be wrong property
          > Column.MappingN ame = ... 'This might be wrong property
          > DT.DataGridColu mnStyle.Add(Col umn)
          > Column = new DataGridTextBox Column
          > Column.Width = 150
          > Column.DisplayN ame = ... 'This might be wrong property
          > Column.MappingN ame = ... 'This might be wrong property
          > DT.DataGridColu mnStyle.Add(Col umn)
          >
          > DataGrid1.Table Styles.Add(DT)
          >
          > Dim dt As DataTable
          > datagrid1.DataS ource = Dataset1.Tables ("tbl1")
          >
          > Hope this helps
          > Chris
          >[/color]

          Comment

          Working...