Removing Columns From Gridview in List.aspx Page

Last post 05-07-2008 10:57 PM by westmich. 35 replies.

Sort Posts:

  • Removing Columns From Gridview in List.aspx Page

    05-05-2008, 3:48 PM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

    I'm a newbie to Dynamic Data so I tried this a number of ways before posting here.  Basically, I'd like to remove certain columns from the gridview and have it apply to all tables (otherwise I'd be doing it in the custom pages area).  Specifically, I only want required columns to be listed here.  I tried:

    • Looping through the columns in the metatable.  Not a problem, but I have no way to match up the metacolumn with the gridview column.  Apparently, the gridview columns are only accessible by their integer index value and they do not match up with the integer index value in the metatable.
    • I tried matching them by the header text property of the gridview column against the name of the metacolumn, but there doesn't seem to by any header text value although the UI clearly shows it.
    •  I tried modifying the .dbml file - setting the access to not be public - but any changes in the .dbml file itself cause a runtime error.
    Thanks
  • Re: Removing Columns From Gridview in List.aspx Page

    05-05-2008, 6:05 PM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 923
    • AspNetTeam

    There is a way to do this though it's somewhat advanced.  Try this:

    • Implement IAutoFieldGenerator on some class.
    • In your GenerateFields(), return some arbitrary collection of DynamicField's
    • In the page code, set the GridView's ColumnsGenerator to your implementation.

    Let us know how this goes!

    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-05-2008, 6:23 PM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

    I'm not following item 2 -  return some arbitrary collection of DynamicField's

     Would this be specific to a table?
     

  • Re: Removing Columns From Gridview in List.aspx Page

    05-05-2008, 6:25 PM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 923
    • AspNetTeam

    Yes, your implemenation of that interface would be passed the MetaTable it should target (it's available in the Page, e.g. through the DataSource).  Then you would enumerate through the MetaColumns, and for those you want to add (based on your own criteria) you'd add a DynamicField to the collection.

    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-05-2008, 7:30 PM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

     I appreciate your help and patience.  I've got some puesdo code for item 2, but didn't get too far.

     

    1    Imports Microsoft.VisualBasic
    2    Imports System.Web.DynamicData
    3    
    4    Public Class ListDataSource
    5        Implements IAutoFieldGenerator 'SAYS TYPE IS NOT DEFINED, I DONT SEE WHERE IT RESIDES
    6    
    7        Protected table As MetaTable
    8    
    9        Function GenerateFields() As ArrayList
    10           ' this will store the return value
    11           Dim oDataField As New ArrayList
    12   
    13           ' loop through the available columns
    14           For Each col As MetaColumn In table.Columns
    15   
    16               ' remove columns that are not required
    17               If Not col.IsRequired Then
    18                   ' NOT SURE WHAT CODE WOULD GO HERE TO REMOVE COLUMN OR BUILD TO ARRAYLIST
    19   
    20               End If
    21           Next
    22       End Function
    23   End Class
    
      
  • Re: Removing Columns From Gridview in List.aspx Page

    05-05-2008, 7:54 PM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

    One other thing in jumping ahead to number 3 - the intellisense does not list a member for the gridview called ColumnsGenerator.  Is that just a bug in the intellisense?

  • Re: Removing Columns From Gridview in List.aspx Page

    05-06-2008, 2:36 AM
    Answer
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 923
    • AspNetTeam

    Here is an example of what you might do:

                public ICollection GenerateFields(Control control) {
                    var fields = new List();
                    foreach (MetaColumn column in table.Columns) {
                        if ([your custom check to decide to skip a column])
                            continue;

                        fields.Add(new DynamicField() { DataField = column.Name });
                    }

                    return fields;
                }

    The intellisense is broken because the simplistic install batch file doesn't copy the updated System.Web to \windows\Microsoft.NET\Framework\v2.0.50727.  Potentially, you could copy it manually, though you should be able to run fine without it.

    David

     

  • Re: Removing Columns From Gridview in List.aspx Page

    05-06-2008, 7:41 AM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

    Again, much thanks :)

    Here is the VB code I came up with, placed it in the App_Code folder called ListDataSource.vb

     

    1    Imports Microsoft.VisualBasic
    2    Imports System.Web
    3    Imports System.Web.UI
    4    Imports System.Web.DynamicData
    5    Imports System.Collections.Generic
    6    
    7    
    8    Public Class ListDataSource
    9        Implements IAutoFieldGenerator
    10   
    11       Protected table As MetaTable
    12   
    13       Public Function GenerateFields(ByVal control As Control) As ICollection
    14           Dim oFields = New List(Of DynamicField)
    15           For Each col As MetaColumn In table.Columns
    16               If col.IsRequired Then
    17                   Dim oDF As New DynamicField
    18                   oDF.DataField = col.Name
    19                   oFields.Add(oDF)
    20               End If
    21           Next
    22           Return oFields
    23       End Function
    24   
    25   End Class
    

     In the actual list page, I am implementing the new class this way.

     

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
            DynamicDataManager1.RegisterControl(GridView1)
            ListDataSource.GenerateFields(GridView1)
        End Sub

     And this is the error message I get which makes no sense to me.  Everything appears to be correct.

    Compiler Error Message: BC30149: Class 'ListDataSource' must implement 'Function GenerateFields(control As Control) As Collections.ICollection' for interface 'System.Web.UI.IAutoFieldGenerator'.
     

  • Re: Removing Columns From Gridview in List.aspx Page

    05-06-2008, 6:59 PM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 923
    • AspNetTeam

    Try:

    GridView1.ColumnsGenerator = [instance of your class that implements the interface]

    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-06-2008, 10:06 PM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67
    Below is the code in C# for those who may be interested.
    Now, is there a similar technique that can be used for DetailsView?
    This does not work --> DetailsView1.ColumnsGenerator = new ListDataSource(table);
    
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Xml.Linq;
    using System.Web.DynamicData;
    
    public partial class List : System.Web.UI.Page
    {
    	protected MetaTable table;
    
    	protected void Page_Init(object sender, EventArgs e)
    	{
    		DynamicDataManager1.RegisterControl(GridView1);
    	}
    
    	protected void Page_Load(object sender, EventArgs e)
    	{
    		table = GridDataSource.GetTable();
    		Title = table.DisplayName;
    
    		GridView1.ColumnsGenerator = new ListDataSource(table);
    
    		InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert);
    
    		// Disable various options if the table is readonly
    		if (table.IsReadOnly)
    		{
    			GridView1.Columns.RemoveAt(0);
    			InsertHyperLink.Visible = false;
    		}
    	}
    
    	protected void OnFilterSelectedIndexChanged(object sender, EventArgs e)
    	{
    		GridView1.PageIndex = 0;
    	}
    
    }
    
    public class ListDataSource : IAutoFieldGenerator
    {
    	protected MetaTable _table;
    
    	public ListDataSource(MetaTable table)
    	{
    		_table = table;
    	}
    
    	public ICollection GenerateFields(Control control)
    	{
    		List oFields = new List();
    
    		foreach (MetaColumn col in _table.Columns)
    		{
    			if (col.Name == "Memo")
    				continue;
    
    			DynamicField f = new DynamicField()
    			{
    				DataField = col.Name
    			};
    
    			oFields.Add(f);
    		}
    
    		return oFields;
    	}
    }
    
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 12:54 AM
    • Loading...
    • davidebb
    • Joined on 06-11-2002, 12:31 PM
    • Redmond, WA
    • Posts 923
    • AspNetTeam

    Yes, the same works with DetailsView, except the property is named RowsGenerator instead of ColumnsGenerator.

    David

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 7:15 AM
    • Loading...
    • tlanier
    • Joined on 04-29-2008, 1:20 PM
    • Posts 67

    As we loop though the fields, is there any way to mark a field as read-only?

     

    foreach (MetaColumn col in _table.Columns)
    {
    	if (col.Name == "Memo")
    		continue;
    
    	DynamicField f = new DynamicField()
    	{
    		DataField = col.Name
    	};
    
    	oFields.Add(f);
    }
    
     
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 7:20 AM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

    I tried the updated line for the gridview, and several variations, and still get the same error.

    -------------

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
        DynamicDataManager1.RegisterControl(GridView1)
        Dim oLDS As New ListDataSource
        GridView1.ColumnsGenerator = oLDS
    End Sub

  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 7:31 AM
    • Loading...
    • westmich
    • Joined on 06-19-2002, 12:14 PM
    • Posts 51

     OK - I tried the updated code, but still get the same error - :( frustrating

     How do I manually add the update so intellisense will recognize it?  I am beginning to wonder if that is part of the problem.  I get build errors related to the dynamic stuff intellisense doesn't recognize.  Updated code below.

     

     

    1    Imports Microsoft.VisualBasic
    2    Imports System.Web
    3    Imports System.Web.UI
    4    Imports System.Web.DynamicData
    5    Imports System.Collections.Generic
    6    
    7    
    8    Public Class ListDataSource
    9        Implements IAutoFieldGenerator
    10   
    11       Protected _table As MetaTable
    12   
    13       Public Sub New(ByVal table As MetaTable)
    14           _table = table
    15       End Sub
    16   
    17       Public Function GenerateFields(ByVal control As Control) As ICollection
    18           Dim oFields = New List(Of DynamicField)
    19           For Each col As MetaColumn In _table.Columns
    20               If col.IsRequired Then
    21                   Dim oDF As New DynamicField
    22                   oDF.DataField = col.Name
    23                   oFields.Add(oDF)
    24               End If
    25           Next
    26           Return oFields
    27       End Function
    28   
    29   End Class
    30   
    31   ------
    32   
    33   Imports System.Web.DynamicData
    34   
    35   Partial Class List
    36       Inherits System.Web.UI.Page
    37   
    38       Protected table As MetaTable
    39       
    40       Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)
    41           DynamicDataManager1.RegisterControl(GridView1)
    42       End Sub
    43       
    44       Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    45           table = GridDataSource.GetTable
    46           Title = table.DisplayName
    47           GridView1.ColumnsGenerator = New ListDataSource(table)
    48           InsertHyperLink.NavigateUrl = table.GetActionPath(PageAction.Insert)
    49           ' Disable various options if the table is readonly
    50           If table.IsReadOnly Then
    51               GridView1.Columns.RemoveAt(0)
    52               InsertHyperLink.Visible = false
    53           End If
    54       End Sub
    55       
    56       Protected Sub OnFilterSelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
    57           GridView1.PageIndex = 0
    58       End Sub
    59   
    60   End Class
    61   
    
       
  • Re: Removing Columns From Gridview in List.aspx Page

    05-07-2008, 7:47 AM