esastincy
esastincy

Reputation: 1627

Adding column to a gridview

I currently have a gridview that is set up and working properly but I ran into a little bit of an issue. My grid's datasource is being set from a DB table and one of the columns in the table is the numeric ID of another table. Rather than displaying this numeric ID in the grid, I need to display a description column off of the other table. Can I add an additional column to my current gridview just for display purposes?

Upvotes: 0

Views: 2998

Answers (4)

rkw
rkw

Reputation: 7297

The change should be made in 3 locations:

  1. The datasource should have the desc column brought back with it. Do the join in the database and bring it all back at once so the front end will not need to make an insane amount of calls.

  2. On display, you can set the id column to invisible. This can be done on the code behind or on the aspx.

  3. On edit, if it is a value that is based off of a look-up table, you will need to create a drop down list. You can query for more info if edit is needed, if not, the top two points should cover your needs.

Upvotes: 0

pat8719
pat8719

Reputation: 1700

Yes you can accomplish this by using a template on your ID field rather than creating a new field. This link has some examples. This way you can custom format your existing column.

Upvotes: 1

James Johnson
James Johnson

Reputation: 46047

Set AutoGenerateColumns to false, and define the columns that you want to display:

                <asp:GridView ID="GridView1" runat="server" CellPadding="4" CellSpacing="0" AutoGenerateColumns="false" Width="100%">
                    <RowStyle BackColor="#ffffff" />                            
                    <AlternatingRowStyle BackColor="#f5f5dc" />
                    <HeaderStyle BackColor="Beige" ForeColor="#333333" Font-Bold="true" Height="25px" />                        
                    <Columns>
                        </asp:TemplateField>                                                            
                        <asp:TemplateField HeaderText="Address">
                            <ItemTemplate>
                                <%# Eval("Address") %>
                            </ItemTemplate>
                        </asp:TemplateField>
                   </Columns>
             </asp:GridView>

Upvotes: 0

Vinay
Vinay

Reputation: 1064

You can use a data-set and bind this dataset to the gridview. Using the data-set, you can add rows, columns. The below example is a good one to add rows/columns to the grid-view. You can also follow the same example with a little bit of tweaks. http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx

Upvotes: 0

Related Questions