GridViewAdapter and Visible flag

Last post 09-19-2006 8:50 AM by carpenocturnum. 4 replies.

Sort Posts:

  • GridViewAdapter and Visible flag

    09-14-2006, 1:44 AM

    Hi all, I am trying to use the GridViewAdapter, but it appears as though there is a similar problem to the DetailViewAdapter as explained by this post http://forums.asp.net/thread/1336007.aspx

    My issues is that I have set the visible attribute on one of my Databound columns to be false. When I apply the adapter, the column is displayed, the header is written, but the cells just contain no data. Can someone please confirm that this is in fact a bug, and if/when it might be fixed.

     

    Thanks.

  • Re: GridViewAdapter and Visible flag

    09-14-2006, 2:49 AM
    • Contributor
      3,298 point Contributor
    • Russ Helfand
    • Member since 09-14-2005, 6:22 PM
    • Groovybits.com
    • Posts 741

    Interesting. Yes, I think this is a bug, though you are the first to the report it.

    Would you mind working a bit with me to develop a fix?  I'm going to propose some code.  It would be terrific if you could try it out locally and let me know if it fixes your situation the way you'd like.  OK?

    BTW, I'm going to give this to you in VB but if you prefer it in C# left me know.  I never know which language to publish these work arounds in!

    In App_Code\Adapters\GridViewAdapter.vb (or cs) you need to dig down until you find the WriteRows method.  In it, you'll find a loop that goes over each cell in the row.  We need to add some logic inside that loop so we skip cells that are in columns that aren't visible.  The logic I propose is this:

    For Each cell In row.Cells
        If gridView.Columns(row.Cells.GetCellIndex(cell)).Visible Then
            writer.WriteLine()
            cell.RenderControl(writer)
        End If
    Next

    Can you give that a shot? If I've not been clear about what's needed let me know and I'll try to do better.  It's nearly midnight and I'm a little blurry.

    Russ Helfand
    Groovybits.com
  • Re: GridViewAdapter and Visible flag

    09-18-2006, 1:54 AM

    Thanks Russ, that works fine..., I was soooo close, I just couldn't figure out how to get the columns visibility.

     

    Incidently I prefer C#, but I'm bi-lingual :)

     

    Cheers.

  • Re: GridViewAdapter and Visible flag

    09-18-2006, 4:55 AM
    • Member
      32 point Member
    • Bolik
    • Member since 09-06-2006, 7:24 AM
    • china
    • Posts 5

    change GridViewAdapter.cs WriteRows Method

    private void WriteRows(HtmlTextWriter writer, GridView gridView, GridViewRowCollection rows, string tableSection)
        {
         if (rows.Count > 0)
         {
            writer.WriteLine();
            writer.WriteBeginTag(tableSection);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Indent++;
            foreach (GridViewRow row in rows)
            {
             writer.WriteLine();
             writer.WriteBeginTag("tr");
             string className = GetRowClass(gridView, row);
             if (className.Length > 0)
             {
                writer.WriteAttribute("class", className);
             }
             writer.Write(HtmlTextWriter.TagRightChar);
             writer.Indent++;
             //---------- Bolik Fixed for not Visible Columns
             //foreach (TableCell cell in row.Cells)
             //{
             // writer.WriteLine();
             // cell.RenderControl(writer);
             //}            
             for (int i = 0; i < row.Cells.Count; i++)
             {
                if (gridView.Columns[i].Visible)
                {
                 writer.WriteLine();
                 row.Cells[i].RenderControl(writer);
                }
             }
             //---------- Bolik Fixed for not Visible Columns
             writer.Indent--;
             writer.WriteLine();
             writer.WriteEndTag("tr");
            }
            writer.Indent--;
            writer.WriteLine();
            writer.WriteEndTag(tableSection);
         }
        }

     

    Change DetailsViewAdapter.cs BuildItem Method 

    protected override void BuildItem(HtmlTextWriter writer)
        {
         if (IsDetailsView && (ControlAsDetailsView.Rows.Count > 0))
         {
            writer.WriteLine();
            writer.WriteBeginTag("div");
            writer.WriteAttribute("class", _classData);
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Indent++;
            writer.WriteLine();
            writer.WriteBeginTag("ul");
            writer.Write(HtmlTextWriter.TagRightChar);
            writer.Indent++;
            //---------- Bolik Fixed for not Visible Columns
            for (int i = 0; i < ControlAsDetailsView.Rows.Count; i++)
            //foreach (DetailsViewRow row in ControlAsDetailsView.Rows)
            {
             DetailsViewRow row = ControlAsDetailsView.Rows[i];
             if ((!ControlAsDetailsView.AutoGenerateRows) &&
                 ((row.RowState & DataControlRowState.Insert) == DataControlRowState.Insert) &&
                 (!ControlAsDetailsView.Fields[row.RowIndex].InsertVisible))
             {
                continue;
             }            
             if (ControlAsDetailsView.Fields[i].Visible)
             {
                writer.WriteLine();
                writer.WriteBeginTag("li");
                if ((row.RowState & DataControlRowState.Alternate) == DataControlRowState.Alternate)
                {
                 writer.WriteAttribute("class", "AspNet-DetailsView-Alternate");
                }
                writer.Write(HtmlTextWriter.TagRightChar);
                writer.Indent++;
                writer.WriteLine();
                for (int iCell = 0; iCell < row.Cells.Count; iCell++)
                {
                 TableCell cell = row.Cells[iCell];
                 writer.WriteBeginTag("span");
                 if (iCell == 0)
                 {
                    writer.WriteAttribute("class", "AspNet-DetailsView-Name");
                 }
                 else if (iCell == 1)
                 {
                    writer.WriteAttribute("class", "AspNet-DetailsView-Value");
                 }
                 else
                 {
                    writer.WriteAttribute("class", "AspNet-DetailsView-Misc");
                 }
                 writer.Write(HtmlTextWriter.TagRightChar);
                 if (cell.Text != null)
                 {
                    writer.Write(cell.Text);
                 }
                 foreach (Control cellChildControl in cell.Controls)
                 {
                    cellChildControl.RenderControl(writer);
                 }
                 writer.WriteEndTag("span");
                }
                writer.Indent--;
                writer.WriteLine();
                writer.WriteEndTag("li");
             }
             //---------- Bolik Fixed for not Visible Columns
            }
            writer.Indent--;
            writer.WriteLine();
            writer.WriteEndTag("ul");
            writer.Indent--;
            writer.WriteLine();
            writer.WriteEndTag("div");
         }
        }

    for more CSS Control Adapter Toolkit GridView DetailsView NonVisible Column Field Fixed

  • Re: GridViewAdapter and Visible flag

    09-19-2006, 8:50 AM

    Yep, that works too.

     

    Thanks.

Page 1 of 1 (5 items)