Half_Baked
Half_Baked

Reputation: 340

asp GridView add data to a certain column (after TemplateField) with Boundfield?

I have a asp:GridView wich in code behind is asigned to a List with Objects.

gwMyGridview.DataSource = test; // test is a list with some objects (two fields)
gwTract.DataBind();

I can add buttons and stuff to the columns, but my data (two columns) are always the last two columns. I know BoundField can be used, but I still get two columns in the end. How can i do this: column1 = data1, column2 = button, column3 = data. Now it looks like this: button, data, data

Thanks in advance!

Upvotes: 1

Views: 747

Answers (1)

Hanlet Escaño
Hanlet Escaño

Reputation: 17380

Don't let the GridView to auto generate the columns. To do this, follow these steps:

1- Click on the GridView's Tasks (little arrow on top of the GridView when you click on it) and click on Edit Columns, and uncheck the "Auto-Generate fields" field, or from the Markup set the AutoGenerateColumns property to false

2 - Add your columns manually to the GridView, by either adding them in the previous dialog (Fields) or in the markup, in the exact order you want them to a appear in your GridView:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False">
    <Columns>
        <asp:BoundField DataField="data1" HeaderText="Data1" />
        <asp:TemplateField HeaderText="CustomColumn"></asp:TemplateField>
        <asp:BoundField DataField="data2" HeaderText="Data2" />
    </Columns>
</asp:GridView>

After that you can bind normally.

Upvotes: 3

Related Questions