我有一个动态的GridView:
public class DynamicGridView
{
public DynamicGridView(SectiuneFormular sectiuneFormular , int idFormular)
{
this.idFormular = idFormular;
gridView = new GridView();
sectiune = sectiuneFormular;
dataTable = new DataTable();
gridView.DataSource = dataTable;
SeteazaDataTable();
SeteazaAspectGridView();
gridView.DataBind();
}
private int idFormular = 0;
public int IdFormular
{
get
{
return idFormular;
}
set
{
idFormular = value;
}
}
private GridView gridView;
public GridView GridView
{
get
{
return gridView;
}
set
{
gridView = value;
}
}
private SectiuneFormular sectiune;
public SectiuneFormular Sectiune
{
get
{
return sectiune;
}
set
{
sectiune = value;
}
}
private DataTable dataTable;
public DataTable DataTable
{
get
{
return dataTable;
}
set
{
dataTable = value;
}
}
public void SeteazaDataTable()
{
//Adaug Header si denumirea lui
for (int numarColoana = 2; numarColoana <= 15; numarColoana++)
{
if (sectiune.Sablon.Coloane[numarColoana] != "")
{
dataTable.Columns.Add(sectiune.Sablon.Coloane[numarColoana]);
AdaugaColoanaGridView(sectiune.Sablon.Coloane[numarColoana] , numarColoana);
}
}
dataTable.Columns.Add("Id");
dataTable.Columns.Add("IdFormular");
//Incarc datele in tabel
int numarColoane = dataTable.Columns.Count;
foreach (Inregistrare continut in sectiune.Continut)
{
DataRow dataRow = dataTable.NewRow();
for (int coloana = 0; coloana < numarColoane-2; coloana++)
{
dataRow[coloana] = continut.Coloane[coloana+2];
}
dataRow[numarColoane-1] = continut.Id;
dataRow[numarColoane - 2] = idFormular;
dataTable.Rows.Add(dataRow);
}
}
public void SeteazaAspectGridView()
{
gridView.AutoGenerateDeleteButton = true;
gridView.AutoGenerateEditButton = true;
gridView.DataKeyNames = new string[] { "Id" , "IdFormular" };
gridView.AllowPaging = true;
gridView.AutoGenerateColumns = false;
gridView.Width = new Unit("75%");
gridView.EmptyDataText = "Nu s-au gasit inregistrari";
gridView.CellPadding = 4;
gridView.CssClass = "GridView";
gridView.ForeColor = System.Drawing.ColorTranslator.FromHtml("#33333");
gridView.GridLines = GridLines.None;
gridView.EnableModelValidation = true;
gridView.RowStyle.CssClass = "GridViewItem";
gridView.FooterStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#0099FF");
gridView.PagerStyle.CssClass = "GridViewPager";
gridView.PagerStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#0099FF");
gridView.PagerStyle.HorizontalAlign = HorizontalAlign.Center;
gridView.SelectedRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#D1DDF1");
gridView.SelectedRowStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#333333");
gridView.HeaderStyle.CssClass = "GridViewHeader";
gridView.HeaderStyle.ForeColor = System.Drawing.ColorTranslator.FromHtml("#2084C5");
gridView.HeaderStyle.BorderStyle = BorderStyle.None;
gridView.EditRowStyle.BackColor = System.Drawing.ColorTranslator.FromHtml("#2461BF");
gridView.AlternatingRowStyle.CssClass = "GridViewItem";
}
public void AdaugaColoanaGridView(string dataField , int numarColoana)
{
TemplateField tf2 = new TemplateField();
tf2.HeaderTemplate = new GridViewTemplate(DataControlRowType.Header, dataField, dataField, new Label() , numarColoana , "header");
tf2.ItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, dataField, dataField, new Label() , numarColoana , "item");
tf2.EditItemTemplate = new GridViewTemplate(DataControlRowType.DataRow, dataField, dataField, new TextBox() , numarColoana ,"edit");
//BoundField numeColoana = new BoundField();
//numeColoana.DataField = dataField;
//numeColoana.HeaderText = dataField;
//gridView.Columns.Add(numeColoana);
gridView.Columns.Add(tf2);
}
}
public class GridViewTemplate : ITemplate
{
private DataControlRowType templateType;
private string columnNameFriendly;
private string columnNameData;
private Control control;
private int numarColoana;
private string type;
public GridViewTemplate(DataControlRowType type, string colNameFr, string colNameDt, Control con , int numarColoana , string tip)
{
templateType = type;
columnNameFriendly = colNameFr;
columnNameData = colNameDt;
control = con;
this.numarColoana = numarColoana;
this.type = tip;
}
public void InstantiateIn(System.Web.UI.Control container)
{
switch (templateType)
{
case DataControlRowType.Header:
{
Literal lc = new Literal();
lc.Text = columnNameFriendly;
container.Controls.Add(lc);
break;
}
case DataControlRowType.DataRow:
{
Control field = control;
if (field.GetType() == typeof(Label))
{
Label lbl = new Label();
lbl.ID = "label"+type+ numarColoana;
lbl.DataBinding += new EventHandler(this.lbl_DataBind);
container.Controls.Add(lbl);
}
else if (field.GetType() == typeof(TextBox))
{
TextBox txt = new TextBox();
txt.ID = "textbox"+type+ numarColoana;
txt.DataBinding += new EventHandler(this.txt_DataBind);
container.Controls.Add(txt);
}
else if (field.GetType() == typeof(DropDownList))
{
DropDownList ddl = (DropDownList)field;
ddl.DataBinding += new EventHandler(this.ddl_DataBind);
container.Controls.Add(ddl);
}
else if (field.GetType() == typeof(CheckBox))
{
CheckBox cbx = new CheckBox();
cbx.DataBinding += new EventHandler(this.cbx_DataBind);
container.Controls.Add(cbx);
}
break;
}
}
}
private void txt_DataBind(Object sender, EventArgs e)
{
TextBox txt = (TextBox)sender;
GridViewRow row = (GridViewRow)txt.NamingContainer;
txt.Text = DataBinder.Eval(row.DataItem, columnNameData).ToString();
txt.Font.Name = "Arial";
}
private void lbl_DataBind(Object sender, EventArgs e)
{
Label lbl = (Label)sender;
GridViewRow row = (GridViewRow)lbl.NamingContainer;
lbl.Text = DataBinder.Eval(row.DataItem, columnNameData).ToString();
lbl.Font.Name = "Arial";
}
private void ddl_DataBind(Object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
ddl.SelectedValue = DataBinder.Eval(row.DataItem, columnNameData).ToString();
ddl.Font.Name = "Arial";
}
private void cbx_DataBind(Object sender, EventArgs e)
{
CheckBox cbx = (CheckBox)sender;
GridViewRow row = (GridViewRow)cbx.NamingContainer;
cbx.Checked = false;
}
}
1.第一期: 我的RowUpdating事件由 GridViewEditEventHandler 处理,但不由GridViewUpdateEventHandler处理(测试断点)。
如果我按更新单元格
string cell= grid.Rows[e.NewEditIndex].Cells[cellNumber].Controls[0].ToString(); //cell== OldItemValue(from my ViewItemTemplate)
cell ==&#34; OldItemValue&#34; ,来自我的ViewItemTemplate,但它必须从EditItemTemplate
中检索它的值答案 0 :(得分:0)
按下任何按钮时,按下&#34;编辑&#34;命令,RowEditing事件应该发生。之后,按&#34;更新&#34;触发RowUpdating命令。在RowEditing处理程序中,您可以在其中找到控件并获取其值。你在做什么
string cell=grid.Rows[e.NewEditIndex].Cells[cellNumber].Controls[0].ToString();
...我通常grid.rows[e.NewEditIndex].FindControl("ID OF CONTROL")
来获得确切的控制权。
问题有点令人困惑所以我可能没有给你答案。你能提供更多信息吗?