Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Dynamically adding/removing columns in datagrid

Author  Topic 

ninel
Posting Yak Master

141 Posts

Posted - 2006-05-02 : 15:33:29
I 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:

[Code]

<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>

[/code]

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

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-05-02 : 17:08:31
I'd probably have two datagrids on the page, both in a panel. Depending on the value of your drop down box, hide / unhide the correct panels and databind to the correct datagrid.

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-05-02 : 17:13:35
That's a good idea, but what happens if I have a lot of different criteria? Am I going to end up with 4 or more datagrids on the page?
Is there any other way of doing this?

Thanks,
Ninel
Go to Top of Page

MichaelP
Jedi Yak

2489 Posts

Posted - 2006-05-02 : 20:19:05
Here's a way to do it in code. I searched for dynamic datagrid in google and this came up as the first result. There may be other solutions though.

http://www.odetocode.com/Articles/218.aspx

Michael

<Yoda>Use the Search page you must. Find the answer you will. Cursors, path to the Dark Side they are. Avoid them, you must. Use Order By NewID() to get a random record you will.</Yoda>

Opinions expressed in this post are not necessarily those of TeleVox Software, inc. All information is provided "AS IS" with no warranties and confers no rights.
Go to Top of Page

jsmith8858
Dr. Cross Join

7423 Posts

Posted - 2006-05-03 : 14:45:42
I did not read the link Michael provided, but a datagrid has a Columns() property that lets you access the columns and set the visible property as needed.

i.e., to hide the 3rd column using code:

DataGrid1.Columns(2).Visible = False
Go to Top of Page

ninel
Posting Yak Master

141 Posts

Posted - 2006-05-22 : 15:29:29
Along the same lines...

I have a grid on an aspx page with an "Add New Row" submit button.
When user clicks on "Add New Row" button I need to create a new row in the grid.

Is there any way I can do this?

Thanks,
Ninel
Go to Top of Page

elwoos
Master Smack Fu Yak Hacker

2052 Posts

Posted - 2006-05-23 : 03:01:58
The article Michael linked to explains how to do this in C#. If you want a similar article in VB then I used this to create my dynamic grid [url]http://www.dotnet247.com/247reference/msgs/5/27405.aspx[/url]

steve

-----------

Oh, so they have internet on computers now!
Go to Top of Page
   

- Advertisement -