Change GridView column properties from code

I create a GridView in a method like this:

GridView gridView = new GridView();
gridView.DataSource = reportData.Tables[0];
gridView.DataBind();

I later export it to Excel and it works great. Columns are automatically generated from my source data. I would like to change the DataFormatString property for some columns, but after I bind to the data before exporting to Excel. It seems I cannot find the correct property to change. Can someone point me in the right direction?

+5
source share
4 answers

According to AutoGenerateColumns Documentation :

; , .

: .

AutoGeneratedField .
( ):

  • (, RowDataBound), , .
  • AutoGeneratedField , :

    BoundField dateField = new BoundField();
    dateField.HeaderText = "Date";
    dateField.DataField = "date";
    dateField.DataFormatString = "{0:MMMM, yyyy}";
    gridView.Columns.Add(dateField); 
    

    .

  • . , . , DataTables, GridView (, List<Employee)), AutoGeneratedField .
    , . , , ? , .

, , Excel API. , HTML XLS Excel 2007 - , , , , , ( Save As ), .

+7

:

String newDataFormatString = "{0:d}";
BoundField bf = gridView.Columns[Index] as BoundField;
if (bf != null) {
    bf.DataFormatString = "{0}"; // workaround to sync with ViewState (it documented)
    bf.DataFormatString = newDataFormatString;
}
0

asp.net. () , , , , ... . , , ().
- Page_Init...

-1

GridView, , GridView , . :

        /// <summary>
    /// Parses and cleans up data from the GridView controls collection
    /// to make the data more suitable for Exported
    /// </summary>
    /// <param name="gv">The GridView to parse</param>
    private void CleanUpControls(Control gv)
    {
        Literal l = new Literal();

        for (int i = 0; i < gv.Controls.Count; i++)
        {

            if (gv.Controls[i].GetType() == typeof (LinkButton))
            {
                l.Text = (gv.Controls[i] as LinkButton).Text;
                ReplaceWithLiteral(gv, l, i);
            }
            else if (gv.Controls[i].GetType() == typeof (ListControl))
            {
                l.Text = (gv.Controls[i] as ListControl).SelectedItem.Text;
                ReplaceWithLiteral(gv, l, i);
            }
            else if (gv.Controls[i].GetType() == typeof (CheckBox))
            {
                l.Text = (gv.Controls[i] as CheckBox).Checked ? "True" : "False";
                ReplaceWithLiteral(gv, l, i);
            }
            else if (gv.Controls[i].GetType() == typeof (BooleanImage))
            {
                l.Text = (gv.Controls[i] as BooleanImage).Value ? "True" : "False";
                ReplaceWithLiteral(gv, l, i);
            }
            else if (gv.Controls[i].GetType().ToString() == "System.Web.UI.WebControls.PagerTable")
                ReplaceWithLiteral(gv, l, i);

            else if (gv.Controls[i].GetType() == typeof (HyperLink))
            {
                HyperLink hl = gv.Controls[i] as HyperLink;
                if (MakeHyperLinksAbsolute)
                {
                    if (hl != null)
                        hl.NavigateUrl = UrlHelper.MakeAbsoluteUrl(hl.NavigateUrl);
                }

                switch (TreatHyperLinksAs)
                {
                    case HyperLinkMode.Text:
                        l.Text = hl.Text;
                        ReplaceWithLiteral(gv, l, i);
                        break;

                    case HyperLinkMode.NavigateUrl:
                        if (hl != null) l.Text = hl.NavigateUrl;
                        ReplaceWithLiteral(gv, l, i);
                        break;

                    case HyperLinkMode.ToolTip:
                        l.Text = hl.ToolTip;
                        ReplaceWithLiteral(gv, l, i);
                        break;

                    case HyperLinkMode.TextAndLink:
                        l.Text = String.Format("{0} ({1})", hl.Text, hl.NavigateUrl);
                        ReplaceWithLiteral(gv, l, i);
                        break;

                    case HyperLinkMode.HyperLink:
                        break;
                }
            }

            if (gv.Controls[i].HasControls())
                CleanUpControls(gv.Controls[i]);
        }
    }
-1

All Articles