ciao a tutti ,ho un problema ecco cosa ho fatto
TemplateColumn tc1 = new TemplateColumn();
tc1.ItemTemplate = new GridViewTextBoxTemplate(ListItemType.Item, "Column1");
tc1.EditItemTemplate = new GridViewTextBoxTemplate(ListItemType.EditItem, "Column1");
gridview1.Columns.Add(tc1);
e la classe
public class GridViewTextBoxTemplate : ITemplate
{
//A variable to hold the type of ListItemType.
ListItemType _templateType;
//A variable to hold the column name.
string _columnName;
//Constructor where we define the template type and column name.
public GridViewTextBoxTemplate(ListItemType type, string colname)
{
//Stores the template type.
_templateType = type;
//Stores the column name.
_columnName = colname;
}
void ITemplate.InstantiateIn(System.Web.UI.Control container)
{
switch (_templateType)
{
case ListItemType.Header:
//Creates a new label control and add it to the container.
Label lbl = new Label(); //Allocates the new label object.
lbl.Text = _columnName; //Assigns the name of the column in the lable.
container.Controls.Add(lbl); //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.
TextBox tb1 = new TextBox(); //Allocates the new text box object.
tb1.Enabled = false;
tb1.DataBinding += new EventHandler(tb1_DataBinding); //Attaches the data binding event.
tb1.Columns = 4; //Creates a column with size 4.
container.Controls.Add(tb1); //Adds the newly created textbox to the container.
break;
case ListItemType.EditItem:
//Creates a new text box control and add it to the container.
TextBox tb2 = new TextBox(); //Allocates the new text box object.
tb2.DataBinding += new EventHandler(tb1_DataBinding); //Attaches the data binding event.
tb2.Columns = 4; //Creates a column with size 4.
container.Controls.Add(tb2); //Adds the newly created textbox to the container.
break;
case ListItemType.Footer:
CheckBox chkColumn = new CheckBox();
chkColumn.ID = "Chk" + _columnName;
container.Controls.Add(chkColumn);
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, _columnName);
if (dataValue != DBNull.Value)
{
txtdata.Text = dataValue.ToString();
}
}
}
allora questo cosa si trova in lungo e in largo su google e io mi sto chiedendo perché a me non va:(
perché mi dice
Errore7Argomento 1: impossibile convertire da 'System.Web.UI.WebControls.TemplateColumn' a 'System.Web.UI.WebControls.DataControlField'
Sto cercando di inserire in una gridview delle righe che quando si va in edit mostra la textbox.
Ora ho creato le mie righe cosi :
BoundField fBoundFieldbf = new BoundField();
fBoundFieldbf.DataField = DataField;
fBoundFieldbf.HeaderText = HeaderText;
fBoundFieldbf.HeaderStyle.HorizontalAlign = HeaderAling;
fBoundFieldbf.ItemStyle.HorizontalAlign = ItemAling;
fBoundFieldbf.Visible = isvisible;
if (isDate)
fBoundFieldbf.DataFormatString = "{0:dd MMMM yyyy}";
gv.Columns.Add(fBoundFieldbf);
quando però vado in edit, mi compare si le textbox peccato che me le mostra per tutte le colonne della riga selezionata e a me serve solo x alcune.
Allora ho fatto cercato su google e mostrava quel esempio (anche da sito MS) peccato che mi da quel errore. Come posso fare?
Grazie a tutti