转换构建错误::在网格视图2.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
}
以及构建网格 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);
}
}
}
This Link was my reference (code project):
create template columns dynamically in a grid view
My needs:
To build a grid that has sometimes 2 radio button template columns and some times more than 2 regarding to database questions options.
Each time i access the page this grid will have these examples:
- 2-4 questions with 2 options radio buttons.
- 2-7 questions with 5 options radio buttons.
Build Error
Cannot implicitly convert type GridViewTemplate' to 'System.Web.UI.WebControls.TemplateField'
My Class:
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
}
And the Build Grid cols method :
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);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我继续搜索,所有自定义动态模板都以相同的方式构建。
我认为我是通过以下方式:
之前:
修复:
这很简单,我没有注意到我需要定义 HeaderTemplate 和 ItemTemplates 我的坏:)
I Kept searching and all custom dynamic templates build the same way.
I figured i by the following:
Before:
Fixed:
It was simple that i didn't notice that i need to define HeaderTemplate and ItemTemplates my bad :)