Bes-m M-bes
Bes-m M-bes

Reputation: 177

Populate a gridview on runtime

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?

Upvotes: 1

Views: 1857

Answers (3)

Heinzi
Heinzi

Reputation: 172200

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.

Upvotes: 1

Niranjan Singh
Niranjan Singh

Reputation: 18290

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..

Upvotes: 2

Ankit
Ankit

Reputation: 6644

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.

Upvotes: 1

Related Questions