0

此链接是我的参考(代码项目): 在网格视图中动态创建模板列

我的需求:构建一个有时有 2 个单选按钮模板列,有时超过 2 个关于数据库问题选项的网格。

每次我访问该页面时,此网格都会包含以下示例:

  • 2-4 个问题,带有 2 个选项单选按钮。
  • 2-7 个问题,带有 5 个选项单选按钮。

构建错误

Cannot implicitly convert type GridViewTemplate' to 'System.Web.UI.WebControls.TemplateField'

我的课:

public class GridViewTemplate : ITemplate
{
    #region Members
    //A variable to hold the type of ListItemType.
    ListItemType litTemplateType;

    //A variable to hold the column name.
    string sColumnName;

    //A variable to hold the Template Field Control type.
    string sItemControlType;
    #endregion

    #region Constructor
    /// <summary>
    /// Constructor where we define the template type, column name and control type.
    /// </summary>
    /// <param name="litType">ListItemType template type</param>
    /// <param name="sColName">string column name</param>
    public GridViewTemplate(ListItemType litType, string sColName)
    {
        //Stores the template type.
        litTemplateType = litType;

        //Stores the column name.
        sColumnName = sColName;
    }
    /// <summary>
    /// Constructor where we define the template type, column name and control type.
    /// </summary>
    /// <param name="litType">ListItemType template type</param>
    /// <param name="sColName">string column name</param>
    /// <param name="sControlType">string control type</param>
    public GridViewTemplate(ListItemType litType, string sColName, string sControlType)
    {
        //Stores the template type.
        litTemplateType = litType;

        //Stores the column name.
        sColumnName = sColName;

        //Stores the template field control type.
        sItemControlType = sControlType;
    }
    #endregion

    void ITemplate.InstantiateIn(System.Web.UI.Control oContainer)
    {
        switch (litTemplateType)
        {
            case ListItemType.Header:
                //Creates a new label control and add it to the container.
                Label lblHeaderText = new Label();                          //Allocates the new label object.
                lblHeaderText.Text = sColumnName;                           //Assigns the name of the column in the lable.
                oContainer.Controls.Add(lblHeaderText);                     //Adds the newly created label control to the container.
                break;

            case ListItemType.Item:
                //Creates a new text box control and add it to the container.
                switch (sItemControlType.ToUpper())
                {
                    case "TEXTBOX":
                        TextBox tb1 = new TextBox();                                //Allocates the new text box object.
                        tb1.DataBinding += new EventHandler(tb1_DataBinding);       //Attaches the data binding event.
                        tb1.Columns = 4;                                            //Creates a column with size 4.
                        oContainer.Controls.Add(tb1);                               //Adds the newly created textbox to the container.
                        break;
                    case "RADIOBUTTON":
                        RadioButton rbtnItem = new RadioButton();                           //Allocates the new text box object.
                        rbtnItem.DataBinding += new EventHandler(rbtnItem_DataBinding);     //Attaches the data binding event.
                        oContainer.Controls.Add(rbtnItem);                                  //Adds the newly created textbox to the container.
                        break;
                }
                break;

            case ListItemType.EditItem:
                //As, I am not using any EditItem, I didn't added any code here.
                break;

            case ListItemType.Footer:
                //As, I am not using any EditItem, I didn't added any code here.
                break;
        }
    }

    /// <summary>
    /// This is the event, which will be raised when the binding happens.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void tb1_DataBinding(object sender, EventArgs e)
    {
        TextBox txtdata = (TextBox)sender;
        GridViewRow container = (GridViewRow)txtdata.NamingContainer;
        object dataValue = DataBinder.Eval(container.DataItem, sColumnName);
        if (dataValue != DBNull.Value)
        {
            txtdata.Text = dataValue.ToString();
        }
    }
    #region Event :: Radio Button Data Binder
    /// <summary>
    /// This is the event, which will be raised when the binding happens.
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    void rbtnItem_DataBinding(object sender, EventArgs e)
    {
        RadioButton rbtnItem = (RadioButton)sender;
        GridViewRow oRowContainer = (GridViewRow)rbtnItem.NamingContainer;
        object oDataValue = DataBinder.Eval(oRowContainer.DataItem, sColumnName);
        if (oDataValue != DBNull.Value)
        {
            bool bValue;
            bool bIsValid;
            bIsValid = bool.TryParse(oDataValue.ToString(), out bValue);
            if (bIsValid)
                rbtnItem.Checked = (bool)oDataValue;
            else
                rbtnItem.Checked = false;
        }
    }
    #endregion
}

和 Build Grid cols 方法:

private void vBuildGridColumns()
    {
        BoundField bfldQuestionDesc = new BoundField();
        bfldQuestionDesc.DataField = "Question_Desc";
        bfldQuestionDesc.HeaderText = "Question";// GetLocalResourceObject("Col1_Text").ToString();
        bfldQuestionDesc.SortExpression = "Question_DescArabic";

        gvEvaluation.Columns.Add(bfldQuestionDesc);

        DataTable dtData = dtGetEvaluationGridColumns();
        if (dtData != null && dtData.Rows.Count > 0)
        {
            TemplateField tfldRadio;
            for (int i = 0; i < dtData.Rows.Count; i++)
            {
                tfldRadio = new TemplateField();
                tfldRadio = new GridViewTemplate(ListItemType.Header, dtData.Rows[i]["Answer_Desc"].ToString());
                tfldRadio = new GridViewTemplate(ListItemType.Item, dtData.Rows[i]["Answer_Desc"].ToString(),"RadioButton");
                gvEvaluation.Controls.Add(tfldRadio);
            }
        }
    }
4

1 回答 1

1

我一直在搜索,所有自定义动态模板都以相同的方式构建。

我通过以下方式计算出我:

前:

tfldRadio = new TemplateField();
            tfldRadio = new GridViewTemplate(ListItemType.Header, dtData.Rows[i]["Answer_Desc"].ToString());
            tfldRadio = new GridViewTemplate(ListItemType.Item, dtData.Rows[i]["Answer_Desc"].ToString(),"RadioButton");
            gvEvaluation.Controls.Add(tfldRadio);

固定的:

for (int i = 0; i < dtData.Rows.Count; i++)
            {
                tfldRadio = new TemplateField();
                tfldRadio.HeaderTemplate = new GridViewTemplate(ListItemType.Header, dtData.Rows[i]["Answer_Desc_Arabic"].ToString());
                tfldRadio.ItemTemplate = new GridViewTemplate(ListItemType.Item, "Answer_ID", "RadioButton");
                gvEvaluation.Columns.Insert(i + 1, tfldRadio);
            }

很简单,我没有注意到我需要定义 HeaderTemplate 和 ItemTemplates 我不好:)

于 2012-02-06T10:44:22.183 回答