Adding dynamic columns to an ASP.NET Gridview

前端 未结 9 1351
不思量自难忘°
不思量自难忘° 2020-12-28 18:54

I\'m having a problem dynamically adding columns to a GridView. I need to change the layout -- i.e. the included columns -- based on the value in a DropDownList. When the

相关标签:
9条回答
  • 2020-12-28 19:03

    I found this earlier today: TemplateField in a GridView doesn't have its ViewState restored when BoundFields are inserted.

    Looks like a bug that Microsoft doesn't plan on fixing, so you'll have to try one of the solutions above. I'm having the same problem -- I have some DataBoundFields and some TemplateFields, and after a postback, the TemplateField based columns lose their controls and data.

    0 讨论(0)
  • 2020-12-28 19:04
        void Page_PreRenderComplete(object sender, EventArgs e)
        {
            // TemplateField reorder bug: if there is a TemplateField based column (or derived therefrom), GridView may blank out
            // the column (plus possibly others) during any postback, if the user has moved it from its original markup position.
            // This is probably a viewstate bug, as it happens only if a TemplateField based column has been moved.  The workaround is
            // to force a databind before each response. See https://connect.microsoft.com/VisualStudio/feedback/details/104994/templatefield-in-a-gridview-doesnt-have-its-viewstate-restored-when-boundfields-are-inserted
            //
            // This problem is also happening for grid views inside a TabPanel, even if the TemplateField based columns have not
            // been moved.  Also do a databind in that case.
            //
            // We also force a databind right after the user has submitted the column chooser dialog.
            // (This is because the user could have moved TemplateField based column(s) but ColChooserHasMovedTemplateFields()
            // returns false -- ie when the user has moved all TemplateField based columns back to their original positions.
            if ((!_DataBindingDone && (ColChooserHasMovedTemplateFields() || _InTabPanel)) || _ColChooserPanelSubmitted || _ColChooserPanelCancelled)
                DataBind();
    
            // There is a problem with the GridView in case of custom paging (which is true here) that if we are on the last page,
            // and we delete all row(s) of that page, GridView is not aware of the deletion during the subsequent data binding,
            // will ask the ODS for the last page of data, and will display a blank.  By PreRenderComplete, it will somehow have
            // realized that its PageIndex, PageCount, etc. are too big and updated them properly, but this is too late
            // as the data binding has already occurred with oudated page variables.  So, if we were on the last page just before
            // the last data binding (_LastPageIndex == _LastPageCount - 1) and PageIndex was decremented after the data binding,
            // we know this scenario has happened and we redo the data binding.  See http://scottonwriting.net/sowblog/archive/2006/05/30/163173.aspx
            // for a discussion of the problem when the GridView uses the ODS to delete data.  The discussion also applies when we
            // delete data directly through ClassBuilder objects.
            if (_LastPageIndex == _LastPageCount - 1 && PageIndex < _LastPageIndex)
                DataBind();
    
            if (EnableColChooser)
            {
                if (!_IsColChooserApplied)
                    ApplyColChooser(null, false, false);
                else
                {
                    // The purpose of calling ApplyColChooser() here is to order the column headers properly.  The GridView
                    // at this point will have reverted the column headers to their original order regardless of ViewState,
                    // so we need to apply our own ordering.  (This is not true of data cells, so we don't have to apply
                    // ordering to them, as reflected by the parameters of the call.)
    
                    // If we have already processed column reordering upon the column chooser panel being submitted,
                    // don't repeat the operation.
                    if (!_ColChooserPanelSubmitted)
                        ApplyColChooser(null, false, true);
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-28 19:06

    best solution for add dynamic column to grid view (ASP) placed on code project by below address : please check it out : http://www.codeproject.com/Articles/13461/how-to-create-columns-dynamically-in-a-grid-view

    0 讨论(0)
  • 2020-12-28 19:07

    Sorry, Decker. I missed a few key points obviously.. :)

    If this is still a problem for you, I wonder if it makes a difference what you have in your item template? If you just put some text in there, then refresh the page a few times, does the text appear on the first load, then not on the second?

    Also, when the problem arises is there any html markup at all in the cells or are they completely empty?

    0 讨论(0)
  • 2020-12-28 19:08

    I have written a short article on the similar topic that deal with dynamically populating GridView column based on the columns selected by the user in the CheckBoxList control. Hope this will help to those looking for simple demonstration How to generate GridView columns dynamically based on user selection?.

    0 讨论(0)
  • 2020-12-28 19:13

    I found this little nugget in the documentation, under the DataControlFieldCollection Class.

    If you are using the GridView or DetailsView control, the DataControlField objects that are automatically created (for example, when the AutoGenerateColumns property is true) are not stored in the publicly accessible fields collection. You can only access and manipulate DataControlField objects that are not automatically generated.

    I guess the answer is to do all of your column manipulation in code, and then your approach should work fine.

    0 讨论(0)
提交回复
热议问题