Asp – GridView Create Fields Dynamically

asp.net

I would like somebody to help me to solve the following issue.

I have a gridview, where the fields are created dynamically from codebehind.

I would like to create with the same way(dynamically) a templateField which, if it is possible, to hold in it two button controls, having also and commandName.

To be more clear:

Headers–>> First Name, LastName, Print(Daily/Invoices)

Results –>> FooFName, FooLName, Daily | Invoice

The bold one text is what I am looking for.

I am posting also the methods and the way I use them to create the fields.

CreateBoundField(BillingSummaryGV, "Book_Date", "Date", "{0:dd/MM/yyyy}");

CreateButtonField(BillingSummaryGV, "Print", "Print(Daily)", ButtonType.Link,"");

BillingSummaryGV.DataSource = billingSummaries;
BillingSummaryGV.DataBind();



protected void CreateBoundField(GridView gv, string dataField, string headerText, string arrFormatingString) {
            var bf = new BoundField {
                DataField = dataField,
                DataFormatString = arrFormatingString,
                HeaderText = headerText
            };
            gv.Columns.Add(bf);
        }

private void CreateButtonField(GridView gv, string text, string headerText, ButtonType buttonType, string commandName) {
            var bfPrint = new ButtonField() {
                ButtonType = buttonType,
                Text = text,
                HeaderText = headerText,
                Visible = true,
                CommandName = commandName
            };
            gv.Columns.Add(bfPrint);
        }

I would appreciate any reply

thanks

Best Answer

You have to create a template class that implements ITemplate. See this example from MSDN. The code below extends my answer to this question to add a template column that displays Title|Author.

Markup:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" 
    OnRowCommand="GridView1_RowCommand" 
    OnRowEditing="GridView1_RowEditing"/>

Code:

protected void Page_Load(object sender, EventArgs e)
{
    BindGridView();
}

private DataTable GetBooksDataTable()
{
    var dt = new DataTable();
    dt.Columns.Add("ID", typeof(int));
    dt.Columns.Add("Title", typeof(string));
    dt.Columns.Add("Author", typeof(string));

    for (int index = 0; index < 10; index++)
    {
        dt.Rows.Add(index, "Title" + index, "Author" + index);
    }
    return dt;
}

private void BindGridView()
{
    var dt = GetBooksDataTable();

    GridView1.Columns.Clear();
    GridView1.ShowFooter = true;

    var cf = new CommandField();
    cf.HeaderText = "Action";
    cf.ShowEditButton = true;
    GridView1.Columns.Add(cf);

    for (int index = 0; index < dt.Columns.Count; index++)
    {
        var boundField = new BoundField();
        boundField.DataField = dt.Columns[index].ColumnName;
        boundField.HeaderText = dt.Columns[index].ColumnName;
        GridView1.Columns.Add(boundField);
    }

    CreateCustomTemplateField(GridView1, "Title|Author");

    GridView1.DataSource = dt;
    GridView1.DataBind();

    var footer = GridView1.FooterRow;
    var b = new LinkButton();
    b.Text = "Add New";
    b.CommandName = "Add New";
    footer.Cells[0].Controls.Add(b);
    for (int index = 1; index < dt.Columns.Count + 1; index++)
    {
        var tb = new TextBox();
        footer.Cells[index].Controls.Add(tb);
    }

}

private void CreateCustomTemplateField(GridView gv, string headerText)
{
    var customField = new TemplateField();
    customField.HeaderTemplate = new CustomTemplate(DataControlRowType.Header, headerText);
    customField.ItemTemplate = new CustomTemplate(DataControlRowType.DataRow, headerText);
    gv.Columns.Add(customField);
}

public class CustomTemplate : ITemplate
{
    private DataControlRowType _rowType;
    private string _headerText;

    public CustomTemplate(DataControlRowType rowType, string headerText)
    {
        _rowType = rowType;
        _headerText = headerText;
    }

    public void InstantiateIn(Control container)
    {
        switch (_rowType)
        {
            case DataControlRowType.Header:
                var header = new Literal();
                header.Text = _headerText;
                container.Controls.Add(header);
                break;
            case DataControlRowType.DataRow:
                var data = new Literal();
                data.DataBinding += DataRowLiteral_DataBinding;
                container.Controls.Add(data);
                break;
        }
    }

    private void DataRowLiteral_DataBinding(object sender, EventArgs e)
    {
        var data = (Literal)sender;
        var row = (GridViewRow)data.NamingContainer;
        data.Text = DataBinder.Eval(row.DataItem, "Title") + "|" + DataBinder.Eval(row.DataItem, "Author");
    }
}

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
    Literal1.Text = e.CommandName;
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
    Literal1.Text = "Editing row index " + e.NewEditIndex.ToString();
}
Related Topic