Dynamically adding/removing columns in datagrid

ninel

Active member
Joined
Mar 23, 2005
Messages
32
Location
Land O Lakes, Florida
Programming Experience
3-5
create a report by allowing users to select certain criteria from dropdownlistbox.

Dropdowns: site, project, startDate, endDate, reporting level (summary or agent detail)

I have the following datagrid with 7 columns:
VB.NET:
<asp:datagrid id="grdResults" runat="server" Height="50px" Font-Names="Tahoma" Width="900px" Font-Size="XX-Small" BorderColor="Silver" ForeColor="Black" PageSize="1" adding="1" AutoGenerateColumns="False" BorderStyle="Solid"
CellSpacing="1" HorizontalAlign="Center">
<Columns>
<asp:BoundColumn DataField="iMsSiteId" HeaderText="Site"></asp:BoundColumn>
<asp:BoundColumn DataField="sProject" HeaderText="Project"></asp:BoundColumn>
<asp:BoundColumn DataField="sCalldate" HeaderText="Calldate"></asp:BoundColumn>
<asp:BoundColumn DataField="sAgentId" HeaderText="Agentid"></asp:BoundColumn>
<asp:BoundColumn DataField="sFirstName" HeaderText="First Name"></asp:BoundColumn>
<asp:BoundColumn DataField="sLastName" HeaderText="Last Name"></asp:BoundColumn>
<asp:BoundColumn DataField="sRevenue" HeaderText="Revenue" DataFormatString="{0:$###,###.##}"></asp:BoundColumn>
</Columns>
</asp:datagrid>

When agent detail is selected from the dropdown the datagrid should contain agentid, firstname, and lastname columns.
When summary is selected those 3 columns shouldn't be visible.

How can I do this dynamically?

Thanks,
Ninel
 
The datagrid is bounded with the data source given like this

VB.NET:
grdresults.datasource=dt 'where dt is a datatable 
grdresults.databind
So u can add the rows to the datatable and call the above two lines to bind the new datasource like this .

Add new row to datatable dt
VB.NET:
dim dr as datarow
dt.acceptchanges
dr=dt.newrow
dr(0)="haello"
dt.acceptchanges
 
grdresults.datasource=dt
grdresults.databind
Note :U should add columns to the datatable before use that.So u need to add 7 columns before use as datasource.
 
Last edited by a moderator:
Back
Top