Change order of columns in DataGrid dynamically

Discussion in 'ASP .Net' started by tshad, Aug 23, 2007.

  1. tshad

    tshad Guest

    I am trying to allow my clients to specify the order that columns show in a
    datagrid.

    If I have a Datagrid like so:

    <asp:DataGrid
    Visible=true
    AllowSorting="false"
    AutoGenerateColumns="false"
    CellPadding="0"
    CellSpacing="0"
    ID="DataGrid2"
    runat="server"
    ShowFooter="false"
    ShowHeader="true"
    OnSortCommand="SortDataGrid"
    BorderWidth="0"
    BorderColor="#999999"
    Width="701px"
    style="padding-right:5px">
    <headerstyle Font-Bold="true" />
    <alternatingitemstyle CssClass="alternateRows" />
    <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6" Font-Bold="true"
    />
    <pagerstyle BackColor="white" />
    <columns>
    <asp:TemplateColumn sortexpression="JobTitle"
    ItemStyle-Width="190px" HeaderStyle-Width="190px"
    headertext="Job Title" ItemStyle-VerticalAlign="Top"
    runat="server">
    <ItemTemplate>
    <asp:HyperLink ID="JobTitle"
    NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    Container.DataItem("PositionID") %>'
    Text='<%# Container.DataItem("JobTitle")%>'
    OnPreRender="FixHyperLink"
    runat="server"/>
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    HeaderText="Company"
    ReadOnly="true"
    Visible="True"
    ItemStyle-VerticalAlign="Top"
    SortExpression="Company"/>
    <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    HeaderText="Location"
    ReadOnly="true"
    Visible="True"
    ItemStyle-VerticalAlign="Top"
    SortExpression="Location"/>
    </columns>
    </asp:DataGrid>

    I have 3 columns: JobTitle, Company and Location - in that order.

    But one client may want the client to be Company, Location, JobTitle and
    another may want it to be Location, JobTitle,Company.

    Is there a way to change the order of the columns as they are displayed?

    I mentioned in another post that I change the size of the DataGrid and hide
    columns in my PreRender event, like so:

    DataGrid1.Width = new Unit("551px")
    for each col as DataGridColumn in DataGrid1.Columns
    if col.HeaderText = "Company" orElse col.HeaderText = "Posted" then
    col.Visible = false
    end if
    next

    Can I do something like this to also change the order in the table?

    Thanks,

    Tom
    tshad, Aug 23, 2007
    #1
    1. Advertising

  2. You just need to operate on DataGrid.Columns collection in code-behind. Look
    at the DataGridColumnCollection class, it has a few methods like AddAt that
    can help you.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "tshad" <> wrote in message
    news:...
    >I am trying to allow my clients to specify the order that columns show in a
    >datagrid.
    >
    > If I have a Datagrid like so:
    >
    > <asp:DataGrid
    > Visible=true
    > AllowSorting="false"
    > AutoGenerateColumns="false"
    > CellPadding="0"
    > CellSpacing="0"
    > ID="DataGrid2"
    > runat="server"
    > ShowFooter="false"
    > ShowHeader="true"
    > OnSortCommand="SortDataGrid"
    > BorderWidth="0"
    > BorderColor="#999999"
    > Width="701px"
    > style="padding-right:5px">
    > <headerstyle Font-Bold="true" />
    > <alternatingitemstyle CssClass="alternateRows" />
    > <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    > Font-Bold="true" />
    > <pagerstyle BackColor="white" />
    > <columns>
    > <asp:TemplateColumn sortexpression="JobTitle"
    > ItemStyle-Width="190px" HeaderStyle-Width="190px"
    > headertext="Job Title" ItemStyle-VerticalAlign="Top"
    > runat="server">
    > <ItemTemplate>
    > <asp:HyperLink ID="JobTitle"
    > NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    > Container.DataItem("PositionID") %>'
    > Text='<%# Container.DataItem("JobTitle")%>'
    > OnPreRender="FixHyperLink"
    > runat="server"/>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    > <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    > HeaderText="Company"
    > ReadOnly="true"
    > Visible="True"
    > ItemStyle-VerticalAlign="Top"
    > SortExpression="Company"/>
    > <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    > HeaderText="Location"
    > ReadOnly="true"
    > Visible="True"
    > ItemStyle-VerticalAlign="Top"
    > SortExpression="Location"/>
    > </columns>
    > </asp:DataGrid>
    >
    > I have 3 columns: JobTitle, Company and Location - in that order.
    >
    > But one client may want the client to be Company, Location, JobTitle and
    > another may want it to be Location, JobTitle,Company.
    >
    > Is there a way to change the order of the columns as they are displayed?
    >
    > I mentioned in another post that I change the size of the DataGrid and
    > hide columns in my PreRender event, like so:
    >
    > DataGrid1.Width = new Unit("551px")
    > for each col as DataGridColumn in DataGrid1.Columns
    > if col.HeaderText = "Company" orElse col.HeaderText = "Posted" then
    > col.Visible = false
    > end if
    > next
    >
    > Can I do something like this to also change the order in the table?
    >
    > Thanks,
    >
    > Tom
    >
    Eliyahu Goldin, Aug 23, 2007
    #2
    1. Advertising

  3. tshad

    tshad Guest

    "Eliyahu Goldin" <> wrote in
    message news:...
    > You just need to operate on DataGrid.Columns collection in code-behind.
    > Look at the DataGridColumnCollection class, it has a few methods like
    > AddAt that can help you.


    I will look at that but I am not using code behind. I build all my pages in
    DW and am using .net 1.1.

    Also, AddAt or Insert is for creating new columns than adding them to the
    DataGrid.

    What I was hoping to do was to change the order of the columns already set
    up in the DataGrid object below, either in the Page_Load or Page_PreRender
    event. Since you can add a new column at the beginning of the column list
    using AddAt or Insert (which would in effect change the order of the
    columns) - is there a way to say move columns(2) to columns(1) some way?

    Thanks,

    Tom
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "tshad" <> wrote in message
    > news:...
    >>I am trying to allow my clients to specify the order that columns show in
    >>a datagrid.
    >>
    >> If I have a Datagrid like so:
    >>
    >> <asp:DataGrid
    >> Visible=true
    >> AllowSorting="false"
    >> AutoGenerateColumns="false"
    >> CellPadding="0"
    >> CellSpacing="0"
    >> ID="DataGrid2"
    >> runat="server"
    >> ShowFooter="false"
    >> ShowHeader="true"
    >> OnSortCommand="SortDataGrid"
    >> BorderWidth="0"
    >> BorderColor="#999999"
    >> Width="701px"
    >> style="padding-right:5px">
    >> <headerstyle Font-Bold="true" />
    >> <alternatingitemstyle CssClass="alternateRows" />
    >> <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    >> Font-Bold="true" />
    >> <pagerstyle BackColor="white" />
    >> <columns>
    >> <asp:TemplateColumn sortexpression="JobTitle"
    >> ItemStyle-Width="190px" HeaderStyle-Width="190px"
    >> headertext="Job Title" ItemStyle-VerticalAlign="Top"
    >> runat="server">
    >> <ItemTemplate>
    >> <asp:HyperLink ID="JobTitle"
    >> NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    >> Container.DataItem("PositionID") %>'
    >> Text='<%# Container.DataItem("JobTitle")%>'
    >> OnPreRender="FixHyperLink"
    >> runat="server"/>
    >> </ItemTemplate>
    >> </asp:TemplateColumn>
    >> <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    >> HeaderText="Company"
    >> ReadOnly="true"
    >> Visible="True"
    >> ItemStyle-VerticalAlign="Top"
    >> SortExpression="Company"/>
    >> <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    >> HeaderText="Location"
    >> ReadOnly="true"
    >> Visible="True"
    >> ItemStyle-VerticalAlign="Top"
    >> SortExpression="Location"/>
    >> </columns>
    >> </asp:DataGrid>
    >>
    >> I have 3 columns: JobTitle, Company and Location - in that order.
    >>
    >> But one client may want the client to be Company, Location, JobTitle and
    >> another may want it to be Location, JobTitle,Company.
    >>
    >> Is there a way to change the order of the columns as they are displayed?
    >>
    >> I mentioned in another post that I change the size of the DataGrid and
    >> hide columns in my PreRender event, like so:
    >>
    >> DataGrid1.Width = new Unit("551px")
    >> for each col as DataGridColumn in DataGrid1.Columns
    >> if col.HeaderText = "Company" orElse col.HeaderText = "Posted" then
    >> col.Visible = false
    >> end if
    >> next
    >>
    >> Can I do something like this to also change the order in the table?
    >>
    >> Thanks,
    >>
    >> Tom
    >>

    >
    >
    tshad, Aug 23, 2007
    #3
  4. > What I was hoping to do was to change the order of the columns already set
    > up in the DataGrid object below, either in the Page_Load or Page_PreRender
    > event. Since you can add a new column at the beginning of the column list
    > using AddAt or Insert (which would in effect change the order of the
    > columns) - is there a way to say move columns(2) to columns(1) some way?


    Make a copy of columns(2), remove columns(2) from the grid and add it again
    with AddAt(0). This will move columns(2) to columns(1).

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "tshad" <> wrote in message
    news:...
    > "Eliyahu Goldin" <> wrote in
    > message news:...
    >> You just need to operate on DataGrid.Columns collection in code-behind.
    >> Look at the DataGridColumnCollection class, it has a few methods like
    >> AddAt that can help you.

    >
    > I will look at that but I am not using code behind. I build all my pages
    > in DW and am using .net 1.1.
    >
    > Also, AddAt or Insert is for creating new columns than adding them to the
    > DataGrid.
    >
    > What I was hoping to do was to change the order of the columns already set
    > up in the DataGrid object below, either in the Page_Load or Page_PreRender
    > event. Since you can add a new column at the beginning of the column list
    > using AddAt or Insert (which would in effect change the order of the
    > columns) - is there a way to say move columns(2) to columns(1) some way?
    >
    > Thanks,
    >
    > Tom
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer
    >> Microsoft MVP [ASP.NET]
    >> http://msmvps.com/blogs/egoldin
    >> http://usableasp.net
    >>
    >>
    >> "tshad" <> wrote in message
    >> news:...
    >>>I am trying to allow my clients to specify the order that columns show in
    >>>a datagrid.
    >>>
    >>> If I have a Datagrid like so:
    >>>
    >>> <asp:DataGrid
    >>> Visible=true
    >>> AllowSorting="false"
    >>> AutoGenerateColumns="false"
    >>> CellPadding="0"
    >>> CellSpacing="0"
    >>> ID="DataGrid2"
    >>> runat="server"
    >>> ShowFooter="false"
    >>> ShowHeader="true"
    >>> OnSortCommand="SortDataGrid"
    >>> BorderWidth="0"
    >>> BorderColor="#999999"
    >>> Width="701px"
    >>> style="padding-right:5px">
    >>> <headerstyle Font-Bold="true" />
    >>> <alternatingitemstyle CssClass="alternateRows" />
    >>> <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    >>> Font-Bold="true" />
    >>> <pagerstyle BackColor="white" />
    >>> <columns>
    >>> <asp:TemplateColumn sortexpression="JobTitle"
    >>> ItemStyle-Width="190px" HeaderStyle-Width="190px"
    >>> headertext="Job Title" ItemStyle-VerticalAlign="Top"
    >>> runat="server">
    >>> <ItemTemplate>
    >>> <asp:HyperLink ID="JobTitle"
    >>> NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    >>> Container.DataItem("PositionID") %>'
    >>> Text='<%# Container.DataItem("JobTitle")%>'
    >>> OnPreRender="FixHyperLink"
    >>> runat="server"/>
    >>> </ItemTemplate>
    >>> </asp:TemplateColumn>
    >>> <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    >>> HeaderText="Company"
    >>> ReadOnly="true"
    >>> Visible="True"
    >>> ItemStyle-VerticalAlign="Top"
    >>> SortExpression="Company"/>
    >>> <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    >>> HeaderText="Location"
    >>> ReadOnly="true"
    >>> Visible="True"
    >>> ItemStyle-VerticalAlign="Top"
    >>> SortExpression="Location"/>
    >>> </columns>
    >>> </asp:DataGrid>
    >>>
    >>> I have 3 columns: JobTitle, Company and Location - in that order.
    >>>
    >>> But one client may want the client to be Company, Location, JobTitle and
    >>> another may want it to be Location, JobTitle,Company.
    >>>
    >>> Is there a way to change the order of the columns as they are displayed?
    >>>
    >>> I mentioned in another post that I change the size of the DataGrid and
    >>> hide columns in my PreRender event, like so:
    >>>
    >>> DataGrid1.Width = new Unit("551px")
    >>> for each col as DataGridColumn in DataGrid1.Columns
    >>> if col.HeaderText = "Company" orElse col.HeaderText = "Posted" then
    >>> col.Visible = false
    >>> end if
    >>> next
    >>>
    >>> Can I do something like this to also change the order in the table?
    >>>
    >>> Thanks,
    >>>
    >>> Tom
    >>>

    >>
    >>

    >
    >
    Eliyahu Goldin, Aug 26, 2007
    #4
  5. tshad

    tshad Guest

    "Eliyahu Goldin" <> wrote in
    message news:...
    >> What I was hoping to do was to change the order of the columns already
    >> set up in the DataGrid object below, either in the Page_Load or
    >> Page_PreRender event. Since you can add a new column at the beginning of
    >> the column list using AddAt or Insert (which would in effect change the
    >> order of the columns) - is there a way to say move columns(2) to
    >> columns(1) some way?

    >
    > Make a copy of columns(2), remove columns(2) from the grid and add it
    > again with AddAt(0). This will move columns(2) to columns(1).


    That's sounds like a great idea.

    I understand how AddAt works , but how do you copy one column and remove
    another?

    CopyAt will copy all the columns into an array (I think) - but I am not sure
    how I delete the columns that add the array back into the array.

    Also, I plan to do this in my Page_Load/"not IsPostback" event. Will this
    change carry over to my next page or do I need to do it again at each
    PostBack?

    Thanks,

    Tom
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "tshad" <> wrote in message
    > news:...
    >> "Eliyahu Goldin" <> wrote in
    >> message news:...
    >>> You just need to operate on DataGrid.Columns collection in code-behind.
    >>> Look at the DataGridColumnCollection class, it has a few methods like
    >>> AddAt that can help you.

    >>
    >> I will look at that but I am not using code behind. I build all my pages
    >> in DW and am using .net 1.1.
    >>
    >> Also, AddAt or Insert is for creating new columns than adding them to the
    >> DataGrid.
    >>
    >> What I was hoping to do was to change the order of the columns already
    >> set up in the DataGrid object below, either in the Page_Load or
    >> Page_PreRender event. Since you can add a new column at the beginning of
    >> the column list using AddAt or Insert (which would in effect change the
    >> order of the columns) - is there a way to say move columns(2) to
    >> columns(1) some way?
    >>
    >> Thanks,
    >>
    >> Tom
    >>>
    >>> --
    >>> Eliyahu Goldin,
    >>> Software Developer
    >>> Microsoft MVP [ASP.NET]
    >>> http://msmvps.com/blogs/egoldin
    >>> http://usableasp.net
    >>>
    >>>
    >>> "tshad" <> wrote in message
    >>> news:...
    >>>>I am trying to allow my clients to specify the order that columns show
    >>>>in a datagrid.
    >>>>
    >>>> If I have a Datagrid like so:
    >>>>
    >>>> <asp:DataGrid
    >>>> Visible=true
    >>>> AllowSorting="false"
    >>>> AutoGenerateColumns="false"
    >>>> CellPadding="0"
    >>>> CellSpacing="0"
    >>>> ID="DataGrid2"
    >>>> runat="server"
    >>>> ShowFooter="false"
    >>>> ShowHeader="true"
    >>>> OnSortCommand="SortDataGrid"
    >>>> BorderWidth="0"
    >>>> BorderColor="#999999"
    >>>> Width="701px"
    >>>> style="padding-right:5px">
    >>>> <headerstyle Font-Bold="true" />
    >>>> <alternatingitemstyle CssClass="alternateRows" />
    >>>> <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    >>>> Font-Bold="true" />
    >>>> <pagerstyle BackColor="white" />
    >>>> <columns>
    >>>> <asp:TemplateColumn sortexpression="JobTitle"
    >>>> ItemStyle-Width="190px" HeaderStyle-Width="190px"
    >>>> headertext="Job Title" ItemStyle-VerticalAlign="Top"
    >>>> runat="server">
    >>>> <ItemTemplate>
    >>>> <asp:HyperLink ID="JobTitle"
    >>>> NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    >>>> Container.DataItem("PositionID") %>'
    >>>> Text='<%# Container.DataItem("JobTitle")%>'
    >>>> OnPreRender="FixHyperLink"
    >>>> runat="server"/>
    >>>> </ItemTemplate>
    >>>> </asp:TemplateColumn>
    >>>> <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    >>>> HeaderText="Company"
    >>>> ReadOnly="true"
    >>>> Visible="True"
    >>>> ItemStyle-VerticalAlign="Top"
    >>>> SortExpression="Company"/>
    >>>> <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    >>>> HeaderText="Location"
    >>>> ReadOnly="true"
    >>>> Visible="True"
    >>>> ItemStyle-VerticalAlign="Top"
    >>>> SortExpression="Location"/>
    >>>> </columns>
    >>>> </asp:DataGrid>
    >>>>
    >>>> I have 3 columns: JobTitle, Company and Location - in that order.
    >>>>
    >>>> But one client may want the client to be Company, Location, JobTitle
    >>>> and another may want it to be Location, JobTitle,Company.
    >>>>
    >>>> Is there a way to change the order of the columns as they are
    >>>> displayed?
    >>>>
    >>>> I mentioned in another post that I change the size of the DataGrid and
    >>>> hide columns in my PreRender event, like so:
    >>>>
    >>>> DataGrid1.Width = new Unit("551px")
    >>>> for each col as DataGridColumn in DataGrid1.Columns
    >>>> if col.HeaderText = "Company" orElse col.HeaderText = "Posted" then
    >>>> col.Visible = false
    >>>> end if
    >>>> next
    >>>>
    >>>> Can I do something like this to also change the order in the table?
    >>>>
    >>>> Thanks,
    >>>>
    >>>> Tom
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    tshad, Aug 28, 2007
    #5
  6. That's right. CopyAt is good for copying and RemoveAt for deleting. Than you
    can AddAt an individual item from the array.

    --
    Eliyahu Goldin,
    Software Developer
    Microsoft MVP [ASP.NET]
    http://msmvps.com/blogs/egoldin
    http://usableasp.net


    "tshad" <> wrote in message
    news:OUzBa%...
    > "Eliyahu Goldin" <> wrote in
    > message news:...
    >>> What I was hoping to do was to change the order of the columns already
    >>> set up in the DataGrid object below, either in the Page_Load or
    >>> Page_PreRender event. Since you can add a new column at the beginning
    >>> of the column list using AddAt or Insert (which would in effect change
    >>> the order of the columns) - is there a way to say move columns(2) to
    >>> columns(1) some way?

    >>
    >> Make a copy of columns(2), remove columns(2) from the grid and add it
    >> again with AddAt(0). This will move columns(2) to columns(1).

    >
    > That's sounds like a great idea.
    >
    > I understand how AddAt works , but how do you copy one column and remove
    > another?
    >
    > CopyAt will copy all the columns into an array (I think) - but I am not
    > sure how I delete the columns that add the array back into the array.
    >
    > Also, I plan to do this in my Page_Load/"not IsPostback" event. Will this
    > change carry over to my next page or do I need to do it again at each
    > PostBack?
    >
    > Thanks,
    >
    > Tom
    >>
    >> --
    >> Eliyahu Goldin,
    >> Software Developer
    >> Microsoft MVP [ASP.NET]
    >> http://msmvps.com/blogs/egoldin
    >> http://usableasp.net
    >>
    >>
    >> "tshad" <> wrote in message
    >> news:...
    >>> "Eliyahu Goldin" <> wrote in
    >>> message news:...
    >>>> You just need to operate on DataGrid.Columns collection in code-behind.
    >>>> Look at the DataGridColumnCollection class, it has a few methods like
    >>>> AddAt that can help you.
    >>>
    >>> I will look at that but I am not using code behind. I build all my
    >>> pages in DW and am using .net 1.1.
    >>>
    >>> Also, AddAt or Insert is for creating new columns than adding them to
    >>> the DataGrid.
    >>>
    >>> What I was hoping to do was to change the order of the columns already
    >>> set up in the DataGrid object below, either in the Page_Load or
    >>> Page_PreRender event. Since you can add a new column at the beginning
    >>> of the column list using AddAt or Insert (which would in effect change
    >>> the order of the columns) - is there a way to say move columns(2) to
    >>> columns(1) some way?
    >>>
    >>> Thanks,
    >>>
    >>> Tom
    >>>>
    >>>> --
    >>>> Eliyahu Goldin,
    >>>> Software Developer
    >>>> Microsoft MVP [ASP.NET]
    >>>> http://msmvps.com/blogs/egoldin
    >>>> http://usableasp.net
    >>>>
    >>>>
    >>>> "tshad" <> wrote in message
    >>>> news:...
    >>>>>I am trying to allow my clients to specify the order that columns show
    >>>>>in a datagrid.
    >>>>>
    >>>>> If I have a Datagrid like so:
    >>>>>
    >>>>> <asp:DataGrid
    >>>>> Visible=true
    >>>>> AllowSorting="false"
    >>>>> AutoGenerateColumns="false"
    >>>>> CellPadding="0"
    >>>>> CellSpacing="0"
    >>>>> ID="DataGrid2"
    >>>>> runat="server"
    >>>>> ShowFooter="false"
    >>>>> ShowHeader="true"
    >>>>> OnSortCommand="SortDataGrid"
    >>>>> BorderWidth="0"
    >>>>> BorderColor="#999999"
    >>>>> Width="701px"
    >>>>> style="padding-right:5px">
    >>>>> <headerstyle Font-Bold="true" />
    >>>>> <alternatingitemstyle CssClass="alternateRows" />
    >>>>> <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    >>>>> Font-Bold="true" />
    >>>>> <pagerstyle BackColor="white" />
    >>>>> <columns>
    >>>>> <asp:TemplateColumn sortexpression="JobTitle"
    >>>>> ItemStyle-Width="190px" HeaderStyle-Width="190px"
    >>>>> headertext="Job Title" ItemStyle-VerticalAlign="Top"
    >>>>> runat="server">
    >>>>> <ItemTemplate>
    >>>>> <asp:HyperLink ID="JobTitle"
    >>>>> NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    >>>>> Container.DataItem("PositionID") %>'
    >>>>> Text='<%# Container.DataItem("JobTitle")%>'
    >>>>> OnPreRender="FixHyperLink"
    >>>>> runat="server"/>
    >>>>> </ItemTemplate>
    >>>>> </asp:TemplateColumn>
    >>>>> <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    >>>>> HeaderText="Company"
    >>>>> ReadOnly="true"
    >>>>> Visible="True"
    >>>>> ItemStyle-VerticalAlign="Top"
    >>>>> SortExpression="Company"/>
    >>>>> <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    >>>>> HeaderText="Location"
    >>>>> ReadOnly="true"
    >>>>> Visible="True"
    >>>>> ItemStyle-VerticalAlign="Top"
    >>>>> SortExpression="Location"/>
    >>>>> </columns>
    >>>>> </asp:DataGrid>
    >>>>>
    >>>>> I have 3 columns: JobTitle, Company and Location - in that order.
    >>>>>
    >>>>> But one client may want the client to be Company, Location, JobTitle
    >>>>> and another may want it to be Location, JobTitle,Company.
    >>>>>
    >>>>> Is there a way to change the order of the columns as they are
    >>>>> displayed?
    >>>>>
    >>>>> I mentioned in another post that I change the size of the DataGrid and
    >>>>> hide columns in my PreRender event, like so:
    >>>>>
    >>>>> DataGrid1.Width = new Unit("551px")
    >>>>> for each col as DataGridColumn in DataGrid1.Columns
    >>>>> if col.HeaderText = "Company" orElse col.HeaderText = "Posted"
    >>>>> then
    >>>>> col.Visible = false
    >>>>> end if
    >>>>> next
    >>>>>
    >>>>> Can I do something like this to also change the order in the table?
    >>>>>
    >>>>> Thanks,
    >>>>>
    >>>>> Tom
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    Eliyahu Goldin, Aug 28, 2007
    #6
  7. tshad

    tshad Guest

    "Eliyahu Goldin" <> wrote in
    message news:%...
    > That's right. CopyAt is good for copying and RemoveAt for deleting. Than
    > you can AddAt an individual item from the array.


    I assume you mean CopyTo?

    I am having a problem doing the CopyTo. Do you have to do it after you
    bind? I am just trying to change the order of the columns in the Page_Load
    event before any binding is done.

    I tried:

    Dim theArray() as DataGridColumn
    DataGrid2.Columns.CopyTo(theArray,0) -- error Object reference not set to
    an instance of an object

    I also tried:

    Dim theArray(3) as DataGridColumn
    DataGrid2.Columns.CopyTo(theArray,0) -- error
    System.IndexOutOfRangeException: Index was outside the bounds of the array

    Not sure what the problem is here.

    Thanks,

    Tom
    >
    > --
    > Eliyahu Goldin,
    > Software Developer
    > Microsoft MVP [ASP.NET]
    > http://msmvps.com/blogs/egoldin
    > http://usableasp.net
    >
    >
    > "tshad" <> wrote in message
    > news:OUzBa%...
    >> "Eliyahu Goldin" <> wrote in
    >> message news:...
    >>>> What I was hoping to do was to change the order of the columns already
    >>>> set up in the DataGrid object below, either in the Page_Load or
    >>>> Page_PreRender event. Since you can add a new column at the beginning
    >>>> of the column list using AddAt or Insert (which would in effect change
    >>>> the order of the columns) - is there a way to say move columns(2) to
    >>>> columns(1) some way?
    >>>
    >>> Make a copy of columns(2), remove columns(2) from the grid and add it
    >>> again with AddAt(0). This will move columns(2) to columns(1).

    >>
    >> That's sounds like a great idea.
    >>
    >> I understand how AddAt works , but how do you copy one column and remove
    >> another?
    >>
    >> CopyAt will copy all the columns into an array (I think) - but I am not
    >> sure how I delete the columns that add the array back into the array.
    >>
    >> Also, I plan to do this in my Page_Load/"not IsPostback" event. Will
    >> this change carry over to my next page or do I need to do it again at
    >> each PostBack?
    >>
    >> Thanks,
    >>
    >> Tom
    >>>
    >>> --
    >>> Eliyahu Goldin,
    >>> Software Developer
    >>> Microsoft MVP [ASP.NET]
    >>> http://msmvps.com/blogs/egoldin
    >>> http://usableasp.net
    >>>
    >>>
    >>> "tshad" <> wrote in message
    >>> news:...
    >>>> "Eliyahu Goldin" <> wrote in
    >>>> message news:...
    >>>>> You just need to operate on DataGrid.Columns collection in
    >>>>> code-behind. Look at the DataGridColumnCollection class, it has a few
    >>>>> methods like AddAt that can help you.
    >>>>
    >>>> I will look at that but I am not using code behind. I build all my
    >>>> pages in DW and am using .net 1.1.
    >>>>
    >>>> Also, AddAt or Insert is for creating new columns than adding them to
    >>>> the DataGrid.
    >>>>
    >>>> What I was hoping to do was to change the order of the columns already
    >>>> set up in the DataGrid object below, either in the Page_Load or
    >>>> Page_PreRender event. Since you can add a new column at the beginning
    >>>> of the column list using AddAt or Insert (which would in effect change
    >>>> the order of the columns) - is there a way to say move columns(2) to
    >>>> columns(1) some way?
    >>>>
    >>>> Thanks,
    >>>>
    >>>> Tom
    >>>>>
    >>>>> --
    >>>>> Eliyahu Goldin,
    >>>>> Software Developer
    >>>>> Microsoft MVP [ASP.NET]
    >>>>> http://msmvps.com/blogs/egoldin
    >>>>> http://usableasp.net
    >>>>>
    >>>>>
    >>>>> "tshad" <> wrote in message
    >>>>> news:...
    >>>>>>I am trying to allow my clients to specify the order that columns show
    >>>>>>in a datagrid.
    >>>>>>
    >>>>>> If I have a Datagrid like so:
    >>>>>>
    >>>>>> <asp:DataGrid
    >>>>>> Visible=true
    >>>>>> AllowSorting="false"
    >>>>>> AutoGenerateColumns="false"
    >>>>>> CellPadding="0"
    >>>>>> CellSpacing="0"
    >>>>>> ID="DataGrid2"
    >>>>>> runat="server"
    >>>>>> ShowFooter="false"
    >>>>>> ShowHeader="true"
    >>>>>> OnSortCommand="SortDataGrid"
    >>>>>> BorderWidth="0"
    >>>>>> BorderColor="#999999"
    >>>>>> Width="701px"
    >>>>>> style="padding-right:5px">
    >>>>>> <headerstyle Font-Bold="true" />
    >>>>>> <alternatingitemstyle CssClass="alternateRows" />
    >>>>>> <footerstyle BackColor="#E8EBFD" ForeColor="#3D3DB6"
    >>>>>> Font-Bold="true" />
    >>>>>> <pagerstyle BackColor="white" />
    >>>>>> <columns>
    >>>>>> <asp:TemplateColumn sortexpression="JobTitle"
    >>>>>> ItemStyle-Width="190px" HeaderStyle-Width="190px"
    >>>>>> headertext="Job Title" ItemStyle-VerticalAlign="Top"
    >>>>>> runat="server">
    >>>>>> <ItemTemplate>
    >>>>>> <asp:HyperLink ID="JobTitle"
    >>>>>> NavigateURL='<%# "displayPositionNew.aspx?PositionID=" &
    >>>>>> Container.DataItem("PositionID") %>'
    >>>>>> Text='<%# Container.DataItem("JobTitle")%>'
    >>>>>> OnPreRender="FixHyperLink"
    >>>>>> runat="server"/>
    >>>>>> </ItemTemplate>
    >>>>>> </asp:TemplateColumn>
    >>>>>> <asp:BoundColumn ItemStyle-Width="150" DataField="Company"
    >>>>>> HeaderText="Company"
    >>>>>> ReadOnly="true"
    >>>>>> Visible="True"
    >>>>>> ItemStyle-VerticalAlign="Top"
    >>>>>> SortExpression="Company"/>
    >>>>>> <asp:BoundColumn ItemStyle-Width="110" DataField="Location"
    >>>>>> HeaderText="Location"
    >>>>>> ReadOnly="true"
    >>>>>> Visible="True"
    >>>>>> ItemStyle-VerticalAlign="Top"
    >>>>>> SortExpression="Location"/>
    >>>>>> </columns>
    >>>>>> </asp:DataGrid>
    >>>>>>
    >>>>>> I have 3 columns: JobTitle, Company and Location - in that order.
    >>>>>>
    >>>>>> But one client may want the client to be Company, Location, JobTitle
    >>>>>> and another may want it to be Location, JobTitle,Company.
    >>>>>>
    >>>>>> Is there a way to change the order of the columns as they are
    >>>>>> displayed?
    >>>>>>
    >>>>>> I mentioned in another post that I change the size of the DataGrid
    >>>>>> and hide columns in my PreRender event, like so:
    >>>>>>
    >>>>>> DataGrid1.Width = new Unit("551px")
    >>>>>> for each col as DataGridColumn in DataGrid1.Columns
    >>>>>> if col.HeaderText = "Company" orElse col.HeaderText = "Posted"
    >>>>>> then
    >>>>>> col.Visible = false
    >>>>>> end if
    >>>>>> next
    >>>>>>
    >>>>>> Can I do something like this to also change the order in the table?
    >>>>>>
    >>>>>> Thanks,
    >>>>>>
    >>>>>> Tom
    >>>>>>
    >>>>>
    >>>>>
    >>>>
    >>>>
    >>>
    >>>

    >>
    >>

    >
    >
    tshad, Aug 28, 2007
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Laub
    Replies:
    0
    Views:
    504
    David Laub
    Aug 3, 2004
  2. Craig G
    Replies:
    0
    Views:
    417
    Craig G
    Feb 22, 2005
  3. rob thomson
    Replies:
    0
    Views:
    253
    rob thomson
    Sep 4, 2003
  4. MattB

    Column order and dynamically created columns

    MattB, Dec 1, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    130
    MattB
    Dec 1, 2004
  5. ton
    Replies:
    2
    Views:
    200
    Alvin Bruney [MVP]
    Feb 11, 2004
Loading...

Share This Page