0
Reply

HELP Please!!! Updating DataGrid!!!

Aymen BENTALEB

Aymen BENTALEB

Jun 15 2007 10:29 AM
1.7k
Hi,
I'm trying to construct a datagrid dynamicly by reading properties from an Xml File.
so i've defined a template column so they fit the type of controls defined in the xml.
this is code wish define the template column
public class GenericItem : ITemplate
{
private readonly string column;
//private bool validate;
public GenericItem(string column)
{
this.column = column;
}
public void InstantiateIn(Control container)
{
Literal l = new Literal();
l.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(l);
}
public void BindData(object sender, EventArgs e)
{
Literal l = (Literal)sender;
DataGridItem container = (DataGridItem)l.NamingContainer;
l.Text = ((DataRowView)container.DataItem)[column].ToString();

}
}
public class ValidateEditItem : ITemplate
{
private readonly string column;
public ValidateEditItem(string column)
{
this.column = column;
}
public void InstantiateIn(Control container)
{
TextBox tb = new TextBox();
tb.DataBinding += new EventHandler(this.BindData);
container.Controls.Add(tb);
tb.ID = column;
RequiredFieldValidator rfv = new RequiredFieldValidator();
rfv.Text = "please write!";
rfv.ControlToValidate = tb.ID;
rfv.Display = ValidatorDisplay.Dynamic;
rfv.ID = "validate" + tb.ID;
container.Controls.Add(rfv);
}
public void BindData(object sender, EventArgs e)
{
TextBox tb = (TextBox)sender;
DataGridItem container = (DataGridItem)tb.NamingContainer;
tb.Text = ((DataRowView)container.DataItem)[column].ToString();
}
}
public TemplateColumn DynamicColumns(string column, bool isEditable)
{
TemplateColumn genericcolumn = new TemplateColumn();
genericcolumn.HeaderText = column;
genericcolumn.ItemTemplate = new GenericItem(column);

if (isEditable)
{
genericcolumn.EditItemTemplate = new ValidateEditItem(column);
}

return genericcolumn;
}

and the way i contruct column is
if (Type != null)
{
//case of primary key
BoundColumn ColPk = new BoundColumn();
ColPk.DataField = c.ColumnName;
ColPk.SortExpression = c.ColumnName;
ColPk.ReadOnly = true;
Grid.Columns.Add(ColPk);
}else
{
//case of other column
TemplateColumn col;
col = DynamicColumns(c.ColumnName, true);
col.SortExpression = c.ColumnName;
Grid.Columns.Add(col);
}

so that I'm sure to make the primary Key not editable;
but the problem is how to retreive the primary key from datagrid
while it's in readOnly and I've want the value of this key to make Update
I've tried with two method

public void GridUpdate(Object sender, DataGridCommandEventArgs e)
{
//the first way
e.Item.FindControl("key").Text ;->it returns null ;
//the second way
grid.DataKeys[e.Item.ItemIndex];->the grid.Datakeys isempty
}
So I can't contruct the UpdateQuery
thank in advance for help