Populate a gridview on runtime

早过忘川 提交于 2019-12-24 07:27:47

问题


I have a gridview that I want to fill with data in some generic lists. I used a "DataTable" as DataSource which has the columns I need (DataColumn).

GridView1.DataSource = CreateDataTable();

My problem is my gridview contains html tags so I need something like

myBoundedField.HtmlEncode = false;

and I need to change the caption of the columns and all this is not possible if I use "DataColumn". I found some code talking about BoundField. why/when should I use BoundField instead? what are the benefit?


回答1:


Create you GridView Column Objects and add them to Gridview's Columns collection. and you can create your own ItemTemplates for columns for your cuustom values just like you want to manipulate HTML here.

    GridView gvDynamicArticle = new GridView();

    gvDynamicArticle.Width = Unit.Pixel(700);

    gvDynamicArticle.BorderWidth = Unit.Pixel(0);
    gvDynamicArticle.Caption = "Report View";
    gvDynamicArticle.AutoGenerateColumns = false;

    gvDynamicArticle.ShowFooter = true;

    TemplateField tf = null;

    tf = new TemplateField();

    tf.HeaderTemplate = new DynamicGridViewTextTemplate("ArticleID", DataControlRowType.Header);

    tf.ItemTemplate = new DynamicGridViewTextTemplate("ArticleID", DataControlRowType.DataRow);

    tf.FooterTemplate = new DynamicGridViewTextTemplate(DataControlRowType.Footer, ds.Tables[i].Rows.Count);              

  gvDynamicArticle.Columns.Add(tf);

follow these link for more information:

http://www.codedigest.com/Articles/ASPNET/168_Create_Dynamic_GridView_Control_in_C_ASPNet.aspx

http://www.codeproject.com/KB/aspnet/dynamic_Columns_in_Grid.aspx

http://www.dotnetfunda.com/articles/article1400-how-to-generate-gridview-columns-dynamically-based-on-user-selection.aspx

Add some bounded column and you can access data at rowcreated event and then use server.htmlDeocde( <html encoded field value>) to show in the grid.. in the same way you can save html code on row command with HtmlEncode( <save html field value> ) using this event.

hope this help you..




回答2:


You can create columns for the grid dynamically using new Column() and assigning values to it.

Let me know if you want me to support it with a piece of code.




回答3:


In HTML, define your GridView as follows:

<asp:GriVview ... AutoGenerateColumns="false" runat="server">
  <Columns>
    <asp:BoundField DataField="CustomerID" HeaderText="Customer ID" />
    <asp:BoundField DataField="CompanyName" HtmlEncode="false" HeaderText="Customer Name" />
    ...
  </Columns>
</asp:GridView>

(adapted from this example)

It should be obvious to see how you can specify the column captions (HeaderText) as well as the HTML Encoding (HtmlEncode) for each column. The DataField specifies the name of the column in your data tables.



来源:https://stackoverflow.com/questions/8110650/populate-a-gridview-on-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!