GridView动态创建字段

时间:2009-12-30 13:43:29

标签: asp.net

我希望有人帮我解决以下问题。

我有一个gridview,其中的字段是从代码隐藏中动态创建的。

我想以相同的方式(动态地)创建一个templateField,如果可能的话,在其中保存两个按钮控件,同时具有和commandName。

更清楚:

标题 - >>名字,姓氏,打印(每日/发票)

结果 - >> FooFName,FooLName,每日|发票

我正在寻找粗体文字。

我也发布了使用它们创建字段的方法和方法。

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);
        }

我很感激任何回复

感谢

1 个答案:

答案 0 :(得分:0)

您必须创建一个实现ITemplate的模板类。见this example from MSDN。下面的代码将我的答案扩展到this question,以添加显示标题|作者的模板列。

标记:

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

代码:

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();
}