edit , update dynamically datagrid

D

doronb

hello

my question is :
i define dynamic columns to datagrid , 4 columns are databound and 1
columne is "edit template column" and only this col editable ,
i define function for update when pressing on button the datagrid not
define
and body of function recognize the object i think is becuase view
state
but i don't know how to solv it
(when i press on edit bottun it work perfectly )

thanks

private bool DynamicColumnAdded
{
get
{
object b = ViewState["DynamicColumnAdded"];
return (b == null) ? false : true;
}
set
{
ViewState["DynamicColumnAdded"] = value;
}
}

protected override void LoadViewState(object savedState)
{

base.LoadViewState(savedState);
if (DynamicColumnAdded)
{
this.GenerateDataGrid();
}
}
private void GenerateDataGrid()
{
//the edit col i define on html page
//bound col shift name
BoundColumn sTName = new BoundColumn();
sTName.DataField = "ShiftTeamName";
sTName.HeaderText = "Shift Team Name";
sTName.ItemStyle.Width = new Unit(150);
sTName.ReadOnly=true;
dgCounter.Columns.Add(sTName);

//Part Name col
BoundColumn pName = new BoundColumn();
pName.DataField = "PartName";
pName.HeaderText = "Part Name";
pName.ItemStyle.Width = new Unit(150);
pName.ReadOnly=true;
dgCounter.Columns.Add(pName);

//Start_Date col
BoundColumn sDate = new BoundColumn();
sDate.DataField = "Start_Date";
sDate.HeaderText = "From";
sDate.ItemStyle.Width = new Unit(150);
sDate.ReadOnly=true;
dgCounter.Columns.Add(sDate);

//End_Date col
BoundColumn eDate = new BoundColumn();
eDate.DataField = "End_Date";
eDate.HeaderText = "To";
eDate.ItemStyle.Width = new Unit(100);
eDate.ReadOnly=true;
dgCounter.Columns.Add(eDate);

//editable col
TemplateColumn tc = new TemplateColumn();
tc.HeaderText= "Quantity";
tc.ItemTemplate = new
DataGridTemplate(ListItemType.Item,"Quantity");
tc.EditItemTemplate = new DataGridTemplate(ListItemType.EditItem,
"Quantity");

dgCounter.Columns.Add(tc);

}


public void dgCounter_UpdateCommand(Object s,DataGridCommandEventArgs
e)
{

TextBox qauntity =
(TextBox)e.Item.Cells[5].FindControl("txtQuantity") ;

int qnt =Convert.ToInt32(qauntity.Text);
string sTime = e.Item.Cells[3].Text;
string eTime = e.Item.Cells[4].Text;
int ioCounterId = Convert.ToInt32(cboCntId.SelectedValue);

DateTime startTime = DateTime.Parse(sTime);
DateTime endTime = DateTime.Parse(eTime);

MCItems upItem = new MCItems(startTime,endTime,qnt,ioCounterId);
ManualCountLogicLayer updateLogic = new
ManualCountLogicLayer();

updateLogic.UpdateAndInsertCounterDetails(upItem);

dgCounter.EditItemIndex = -1;
GetTheDetails();
FillDataGrid();
}


And the template class

public class DataGridTemplate : ITemplate
{
ListItemType templateType;
string columnName;

public DataGridTemplate(ListItemType type, string colname)
{
templateType = type;
columnName = colname;
}

public void InstantiateIn(System.Web.UI.Control container)
{
Literal lc = new Literal();
switch(templateType)
{
case ListItemType.Header:
lc.Text = "<B>" + columnName + "</B>";
container.Controls.Add(lc);
break;
case ListItemType.Item:
Label lbl = new Label();
lbl.DataBinding += new EventHandler(DataGrid_DataBinding);
container.Controls.Add(lbl);
break;
case ListItemType.EditItem:
TextBox tb = new TextBox();
tb.ID = "txtQuantity";
tb.DataBinding += new EventHandler(DataGridText_DataBinding);
container.Controls.Add(tb);
break;
case ListItemType.Footer:
lc.Text = "<I>" + columnName + "</I>";
container.Controls.Add(lc);
break;
}

}
private void DataGrid_DataBinding(object sender,System.EventArgs e)
{
Label lbl;
lbl = (Label) sender;
DataGridItem container = (DataGridItem) lbl.NamingContainer;
lbl.Text += DataBinder.Eval(container.DataItem, "Quantity");

}
private void DataGridText_DataBinding(object sender,System.EventArgs
e)
{
TextBox tb ;
tb = (TextBox) sender;
DataGridItem container = (DataGridItem) tb.NamingContainer;
tb.Text += DataBinder.Eval(container.DataItem , "Quantity");

}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,983
Messages
2,570,187
Members
46,747
Latest member
jojoBizaroo

Latest Threads

Top