DataGrid

S

Steve

I have a datagrid that is created at run time

DataGrid dgG = new DataGrid();
BoundColumn bcB;

dgG.CellPadding = 5;
dgG.CellSpacing = 0;
dgG.GridLines = GridLines.Both;
dgG.CssClass = "SectionTableLines";
dgG.DataKeyField = "PlanWorkOrderID";
dgG.ID = DataSetName + "." + Constants.datagrid + "." + "0";

dgG.HeaderStyle.CssClass = "SectionHeader";


bcB = new BoundColumn();

bcB.DataField = "WorkOrder";
bcB.HeaderText = "WorkOrder";
dgG.Columns.Add(bcB);

dgG.AutoGenerateColumns = false;

EditCommandColumn eccE = new EditCommandColumn();
eccE.EditText = "Edit";
eccE.CancelText = "Cancel";
eccE.UpdateText = "Update";
eccE.HeaderText = "Edit";
eccE.ButtonType = ButtonColumnType.LinkButton;
eccE.ItemStyle.CssClass = "TxtBox";

dgG.Columns.Add(eccE);
dgG.EnableViewState =false;


ButtonColumn btncBC = new ButtonColumn();
btncBC.HeaderText = "Delete";
btncBC.ButtonType = ButtonColumnType.LinkButton;
btncBC.Text = "Delete";
btncBC.CommandName = "Delete";

dgG.Columns.Add(btncBC);


dgG.DataSource = dtT;
dgG.DataBind();

dgG = qabQA.makeWorkOrder(dstForm1370.Tables[Constants.WorkOrder]);
dgG.EditCommand += new DataGridCommandEventHandler(dgG_EditCommand);
dgG.DeleteCommand += new DataGridCommandEventHandler(dgG_DeleteCommand);
dgG.UpdateCommand += new DataGridCommandEventHandler(dgG_UpdateCommand);
dgG.ItemCommand += new DataGridCommandEventHandler(dgG_ItemCommand);
dgG.CancelCommand += new DataGridCommandEventHandler(dgG_CancelCommand);
tcC.Controls.Add(dgG); // tcC is a table cell


The event handlers are the standard stuff.

private void dgG_EditCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = e.Item.ItemIndex;

BindGrid();

}

private void dgG_DeleteCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}

private void dgG_ItemCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

if (((LinkButton)e.CommandSource).CommandName == "Delete")
{
dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}
else
{
}
}

private void dgG_UpdateCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
string sDGID;
int iPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

iPK = (int)e.Item.ItemIndex;

dtT.Rows[iPK]["WorkOrder"] = ((TextBox)(e.Item.FindControl(sDGID))).Text;

BindGrid();
}

private void BindGrid()
{
DataTable dtT;
Control ctlC;
string sDGID;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).DataSource = dtT;
((DataGrid)ctlC).DataBind();
Session["Form1370DataSet"] = dstForm1370.Copy();
}

private void dgG_CancelCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = -1;

BindGrid();

}



The Editcommand column renders properly on screen. When I click the Edit Link button, it correctly fires the Edit event which calls my EditGrid method and puts me in edit mode. So far, so good. But, when I click the update button or the cancel button, it fires the Edit event again instead of firing Update or Cancel events. In other words, no matter what button I click, it always puts me in my EditGrid method. I don't know why.


I found one other person who had the exact same problem. The advise was:

I think your problem may be in the page_load subroutine. Make sure you don't bind any data to the at a grid there unless IsPostBack is false (the first time you go there should be the only time data is bound to the grid). I think when you hit the Update link, it goes to the page_load sub before it does anything else. Thus, if you bind data there to the datagrid, you just bind what you already have--not the changes you want to make.

I do not think that applies to me. Here is my page_load event handler

private void Page_Load(object sender, System.EventArgs e)
{
DataAccess objDA;

if (Page.IsPostBack)
{
dstForm1370 = ((DataSet)Session["Form1370DataSet"]).Copy();
BuildSections(); // data bind when grid created
}
else
{
objDA = new DataAccess();
objDA.PlanID = Convert.ToInt32(Session["PlanID"]);
dstForm1370 = objDA.GetDataSet("Form1370Data");

BuildSections(); // data bind when grid created

Session["Form1370DataSet"] = dstForm1370.Copy();
}
}


I am at a loss. Anyone have any suggestions?

Thanks.

Steve
 
A

Alvin Bruney [MVP]

Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
I have a datagrid that is created at run time

DataGrid dgG = new DataGrid();
BoundColumn bcB;

dgG.CellPadding = 5;
dgG.CellSpacing = 0;
dgG.GridLines = GridLines.Both;
dgG.CssClass = "SectionTableLines";
dgG.DataKeyField = "PlanWorkOrderID";
dgG.ID = DataSetName + "." + Constants.datagrid + "." + "0";

dgG.HeaderStyle.CssClass = "SectionHeader";


bcB = new BoundColumn();

bcB.DataField = "WorkOrder";
bcB.HeaderText = "WorkOrder";
dgG.Columns.Add(bcB);

dgG.AutoGenerateColumns = false;

EditCommandColumn eccE = new EditCommandColumn();
eccE.EditText = "Edit";
eccE.CancelText = "Cancel";
eccE.UpdateText = "Update";
eccE.HeaderText = "Edit";
eccE.ButtonType = ButtonColumnType.LinkButton;
eccE.ItemStyle.CssClass = "TxtBox";

dgG.Columns.Add(eccE);
dgG.EnableViewState =false;


ButtonColumn btncBC = new ButtonColumn();
btncBC.HeaderText = "Delete";
btncBC.ButtonType = ButtonColumnType.LinkButton;
btncBC.Text = "Delete";
btncBC.CommandName = "Delete";

dgG.Columns.Add(btncBC);


dgG.DataSource = dtT;
dgG.DataBind();

dgG = qabQA.makeWorkOrder(dstForm1370.Tables[Constants.WorkOrder]);
dgG.EditCommand += new DataGridCommandEventHandler(dgG_EditCommand);
dgG.DeleteCommand += new DataGridCommandEventHandler(dgG_DeleteCommand);
dgG.UpdateCommand += new DataGridCommandEventHandler(dgG_UpdateCommand);
dgG.ItemCommand += new DataGridCommandEventHandler(dgG_ItemCommand);
dgG.CancelCommand += new DataGridCommandEventHandler(dgG_CancelCommand);
tcC.Controls.Add(dgG); // tcC is a table cell


The event handlers are the standard stuff.

private void dgG_EditCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = e.Item.ItemIndex;

BindGrid();

}

private void dgG_DeleteCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}

private void dgG_ItemCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

if (((LinkButton)e.CommandSource).CommandName == "Delete")
{
dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}
else
{
}
}

private void dgG_UpdateCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
string sDGID;
int iPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

iPK = (int)e.Item.ItemIndex;

dtT.Rows[iPK]["WorkOrder"] = ((TextBox)(e.Item.FindControl(sDGID))).Text;

BindGrid();
}

private void BindGrid()
{
DataTable dtT;
Control ctlC;
string sDGID;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).DataSource = dtT;
((DataGrid)ctlC).DataBind();
Session["Form1370DataSet"] = dstForm1370.Copy();
}

private void dgG_CancelCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = -1;

BindGrid();

}



The Editcommand column renders properly on screen. When I click the Edit Link button, it correctly fires the Edit event which calls my EditGrid method and puts me in edit mode. So far, so good. But, when I click the update button or the cancel button, it fires the Edit event again instead of firing Update or Cancel events. In other words, no matter what button I click, it always puts me in my EditGrid method. I don't know why.


I found one other person who had the exact same problem. The advise was:

I think your problem may be in the page_load subroutine. Make sure you don't bind any data to the at a grid there unless IsPostBack is false (the first time you go there should be the only time data is bound to the grid). I think when you hit the Update link, it goes to the page_load sub before it does anything else. Thus, if you bind data there to the datagrid, you just bind what you already have--not the changes you want to make.

I do not think that applies to me. Here is my page_load event handler

private void Page_Load(object sender, System.EventArgs e)
{
DataAccess objDA;

if (Page.IsPostBack)
{
dstForm1370 = ((DataSet)Session["Form1370DataSet"]).Copy();
BuildSections(); // data bind when grid created
}
else
{
objDA = new DataAccess();
objDA.PlanID = Convert.ToInt32(Session["PlanID"]);
dstForm1370 = objDA.GetDataSet("Form1370Data");

BuildSections(); // data bind when grid created

Session["Form1370DataSet"] = dstForm1370.Copy();
}
}


I am at a loss. Anyone have any suggestions?

Thanks.

Steve
 
S

Steve

No not yet. Do you have a solution?

Steve



"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
I have a datagrid that is created at run time

DataGrid dgG = new DataGrid();
BoundColumn bcB;

dgG.CellPadding = 5;
dgG.CellSpacing = 0;
dgG.GridLines = GridLines.Both;
dgG.CssClass = "SectionTableLines";
dgG.DataKeyField = "PlanWorkOrderID";
dgG.ID = DataSetName + "." + Constants.datagrid + "." + "0";

dgG.HeaderStyle.CssClass = "SectionHeader";


bcB = new BoundColumn();

bcB.DataField = "WorkOrder";
bcB.HeaderText = "WorkOrder";
dgG.Columns.Add(bcB);

dgG.AutoGenerateColumns = false;

EditCommandColumn eccE = new EditCommandColumn();
eccE.EditText = "Edit";
eccE.CancelText = "Cancel";
eccE.UpdateText = "Update";
eccE.HeaderText = "Edit";
eccE.ButtonType = ButtonColumnType.LinkButton;
eccE.ItemStyle.CssClass = "TxtBox";

dgG.Columns.Add(eccE);
dgG.EnableViewState =false;


ButtonColumn btncBC = new ButtonColumn();
btncBC.HeaderText = "Delete";
btncBC.ButtonType = ButtonColumnType.LinkButton;
btncBC.Text = "Delete";
btncBC.CommandName = "Delete";

dgG.Columns.Add(btncBC);


dgG.DataSource = dtT;
dgG.DataBind();

dgG = qabQA.makeWorkOrder(dstForm1370.Tables[Constants.WorkOrder]);
dgG.EditCommand += new DataGridCommandEventHandler(dgG_EditCommand);
dgG.DeleteCommand += new DataGridCommandEventHandler(dgG_DeleteCommand);
dgG.UpdateCommand += new DataGridCommandEventHandler(dgG_UpdateCommand);
dgG.ItemCommand += new DataGridCommandEventHandler(dgG_ItemCommand);
dgG.CancelCommand += new DataGridCommandEventHandler(dgG_CancelCommand);
tcC.Controls.Add(dgG); // tcC is a table cell


The event handlers are the standard stuff.

private void dgG_EditCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = e.Item.ItemIndex;

BindGrid();

}

private void dgG_DeleteCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}

private void dgG_ItemCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

if (((LinkButton)e.CommandSource).CommandName == "Delete")
{
dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}
else
{
}
}

private void dgG_UpdateCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
string sDGID;
int iPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

iPK = (int)e.Item.ItemIndex;

dtT.Rows[iPK]["WorkOrder"] = ((TextBox)(e.Item.FindControl(sDGID))).Text;

BindGrid();
}

private void BindGrid()
{
DataTable dtT;
Control ctlC;
string sDGID;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).DataSource = dtT;
((DataGrid)ctlC).DataBind();
Session["Form1370DataSet"] = dstForm1370.Copy();
}

private void dgG_CancelCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = -1;

BindGrid();

}



The Editcommand column renders properly on screen. When I click the Edit Link button, it correctly fires the Edit event which calls my EditGrid method and puts me in edit mode. So far, so good. But, when I click the update button or the cancel button, it fires the Edit event again instead of firing Update or Cancel events. In other words, no matter what button I click, it always puts me in my EditGrid method. I don't know why.


I found one other person who had the exact same problem. The advise was:

I think your problem may be in the page_load subroutine. Make sure you don't bind any data to the at a grid there unless IsPostBack is false (the first time you go there should be the only time data is bound to the grid). I think when you hit the Update link, it goes to the page_load sub before it does anything else. Thus, if you bind data there to the datagrid, you just bind what you already have--not the changes you want to make.

I do not think that applies to me. Here is my page_load event handler

private void Page_Load(object sender, System.EventArgs e)
{
DataAccess objDA;

if (Page.IsPostBack)
{
dstForm1370 = ((DataSet)Session["Form1370DataSet"]).Copy();
BuildSections(); // data bind when grid created
}
else
{
objDA = new DataAccess();
objDA.PlanID = Convert.ToInt32(Session["PlanID"]);
dstForm1370 = objDA.GetDataSet("Form1370Data");

BuildSections(); // data bind when grid created

Session["Form1370DataSet"] = dstForm1370.Copy();
}
}


I am at a loss. Anyone have any suggestions?

Thanks.

Steve
 
A

Alvin Bruney [MVP]

This looks like the problem
BuildSections(); // data bind when grid created

it's as the user replied. You are binding everytime in your page load. You should be binding once in your page_load inside the PageIsPostback block, not outside.

see if that helps

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
No not yet. Do you have a solution?

Steve



"Alvin Bruney [MVP]" <vapor at steaming post office> wrote in message Your post went unanswered. Have you resolved this issue?

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/3he3b
I have a datagrid that is created at run time

DataGrid dgG = new DataGrid();
BoundColumn bcB;

dgG.CellPadding = 5;
dgG.CellSpacing = 0;
dgG.GridLines = GridLines.Both;
dgG.CssClass = "SectionTableLines";
dgG.DataKeyField = "PlanWorkOrderID";
dgG.ID = DataSetName + "." + Constants.datagrid + "." + "0";

dgG.HeaderStyle.CssClass = "SectionHeader";


bcB = new BoundColumn();

bcB.DataField = "WorkOrder";
bcB.HeaderText = "WorkOrder";
dgG.Columns.Add(bcB);

dgG.AutoGenerateColumns = false;

EditCommandColumn eccE = new EditCommandColumn();
eccE.EditText = "Edit";
eccE.CancelText = "Cancel";
eccE.UpdateText = "Update";
eccE.HeaderText = "Edit";
eccE.ButtonType = ButtonColumnType.LinkButton;
eccE.ItemStyle.CssClass = "TxtBox";

dgG.Columns.Add(eccE);
dgG.EnableViewState =false;


ButtonColumn btncBC = new ButtonColumn();
btncBC.HeaderText = "Delete";
btncBC.ButtonType = ButtonColumnType.LinkButton;
btncBC.Text = "Delete";
btncBC.CommandName = "Delete";

dgG.Columns.Add(btncBC);


dgG.DataSource = dtT;
dgG.DataBind();

dgG = qabQA.makeWorkOrder(dstForm1370.Tables[Constants.WorkOrder]);
dgG.EditCommand += new DataGridCommandEventHandler(dgG_EditCommand);
dgG.DeleteCommand += new DataGridCommandEventHandler(dgG_DeleteCommand);
dgG.UpdateCommand += new DataGridCommandEventHandler(dgG_UpdateCommand);
dgG.ItemCommand += new DataGridCommandEventHandler(dgG_ItemCommand);
dgG.CancelCommand += new DataGridCommandEventHandler(dgG_CancelCommand);
tcC.Controls.Add(dgG); // tcC is a table cell


The event handlers are the standard stuff.

private void dgG_EditCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = e.Item.ItemIndex;

BindGrid();

}

private void dgG_DeleteCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}

private void dgG_ItemCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
int dgPK;

if (((LinkButton)e.CommandSource).CommandName == "Delete")
{
dtT = dstForm1370.Tables[Constants.WorkOrder];
dgPK = (int)e.Item.ItemIndex;
dtT.Rows[dgPK].Delete();

BindGrid();
}
else
{
}
}

private void dgG_UpdateCommand(object source, DataGridCommandEventArgs e)
{
DataTable dtT;
string sDGID;
int iPK;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

iPK = (int)e.Item.ItemIndex;

dtT.Rows[iPK]["WorkOrder"] = ((TextBox)(e.Item.FindControl(sDGID))).Text;

BindGrid();
}

private void BindGrid()
{
DataTable dtT;
Control ctlC;
string sDGID;

dtT = dstForm1370.Tables[Constants.WorkOrder];

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).DataSource = dtT;
((DataGrid)ctlC).DataBind();
Session["Form1370DataSet"] = dstForm1370.Copy();
}

private void dgG_CancelCommand(object source, DataGridCommandEventArgs e)
{
Control ctlC;
string sDGID;

sDGID = Constants.WorkOrder + "." + Constants.datagrid + "." + "0";

ctlC = FindControl(sDGID);

((DataGrid)ctlC).EditItemIndex = -1;

BindGrid();

}



The Editcommand column renders properly on screen. When I click the Edit Link button, it correctly fires the Edit event which calls my EditGrid method and puts me in edit mode. So far, so good. But, when I click the update button or the cancel button, it fires the Edit event again instead of firing Update or Cancel events. In other words, no matter what button I click, it always puts me in my EditGrid method. I don't know why.


I found one other person who had the exact same problem. The advise was:

I think your problem may be in the page_load subroutine. Make sure you don't bind any data to the at a grid there unless IsPostBack is false (the first time you go there should be the only time data is bound to the grid). I think when you hit the Update link, it goes to the page_load sub before it does anything else. Thus, if you bind data there to the datagrid, you just bind what you already have--not the changes you want to make.

I do not think that applies to me. Here is my page_load event handler

private void Page_Load(object sender, System.EventArgs e)
{
DataAccess objDA;

if (Page.IsPostBack)
{
dstForm1370 = ((DataSet)Session["Form1370DataSet"]).Copy();
BuildSections(); // data bind when grid created
}
else
{
objDA = new DataAccess();
objDA.PlanID = Convert.ToInt32(Session["PlanID"]);
dstForm1370 = objDA.GetDataSet("Form1370Data");

BuildSections(); // data bind when grid created

Session["Form1370DataSet"] = dstForm1370.Copy();
}
}


I am at a loss. Anyone have any suggestions?

Thanks.

Steve
 

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

Top