Skip to content

Data Grid Column Order Problem

George Laing

Asked Jan 8, 2009, 3:18 AM EST1 answer
Share
LinkedInX

Hi
  I am having a problem with the order of the columns in my data grid. I have set the order I want in the data grid property Edit Columns. When I run the project 2 columns are way out of position. I tried to move the out of position column using column order but it is ignored. I then tried coding the position using event private void GridDisplayOrder(). I get the message object reference not set to an instance of an object. Not sure how to get around this error. Here is the event I use

 private void GridDisplayOrder()
        {
            Grid_Property.Columns["pContract_NO"].DisplayIndex = 0;
            Grid_Property.Columns["pProperty_ID"].DisplayIndex = 1;
            Grid_Property.Columns["pVersion"].DisplayIndex = 2;
            etc
            etc

any assistance would be appreciated

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Hi

    In DatatGrid by default AutoGenerateColumn is set to true so when ever you assign dataset as datasource it will display columns in the same order as they were retrieved from database

    So to fix their order you have to go for tamplate or bound fields
    Here what u hv to do :
    1. Set AutoGenerateColumn of Datagrid to False
    2. Generate bound feilds for each column in dataset
    3. If you want to use controls other than label to bind or diaply then use template feild
    4. finally assign dataset to grid in pageload
    here is sample for grid
    <asp:DataGrid ID="Grid1" runat="server" AllowSorting="True" AllowPaging="True" AutoGenerateColumns="False" >
    <Columns>                                                                                                                 <asp:TemplateColumn>
    <ItemTemplate>
        //Code for your control
    </ItemTemplate>
    </asp:TemplateColumn>
    <asp:BoundColumn DataField="ColumNameFromDB" HeaderText="" SortExpression="ColumNameFromDB">
    </Columns>
    </asp:DataGrid>
    0