Problem Retrieving Cell Value From Datagrid

I

Iain

Hi All

Using Delphi 2006 developer - C# Project

I have the following 2 event handlers for the datagrid - see botton of
page
Both events will fire off correctly but i have a problem collecting the
correct data in teh Update event - I have noted the problem in the
event handler

protected void Livery_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string LLivery = e.Item.Cells[1].Text;
string LLeadTime = e.Item.Cells[2].Text;
int LRow = e.Item.ItemIndex;

// If I step through this I can see the values of the cells of the
row highlighted when I press the delete button
}

protected void Livery_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string LLivery = e.Item.Cells[1].Text;
string LLeadTime = e.Item.Cells[2].Text;
int LRow = e.Item.ItemIndex;

// If I step through this the values of e.Item.Cells[1].Text and
// e.Item.Cells[2].Text are blank. I could understand if the
// value contained the old (pre-update) values or the
// new (post-update) values but the fields are blank ?????
// e.Item.ItemIndex is showing the correct row index
}

Can anyone advise me why this is so and if so how do I rectify the
situation.

Many thanks in advance for assistance offered

Iain

<asp:datagrid id="Livery" runat="server"
allowsorting="True"
EnableViewState="true"
itemstyle-verticalalign="top"
headerstyle-font-bold="true"
headerstyle-forecolor="white"
headerstyle-backcolor="black"
autogeneratecolumns="false" font-size="8pt"
cellpadding="5"
width="92%"
onsortcommand="Livery_SortCommand"
ondeletecommand="Livery_DeleteCommand"
oneditcommand="Livery_EditCommand"
onupdatecommand="Livery_UpdateCommand"
oncancelcommand="Livery_CancelCommand">
<EditItemStyle borderstyle="Dashed"
bordercolor="#0000C0"
backcolor="#FFFFC0">
</EditItemStyle>
<ItemStyle verticalalign="Top">
</ItemStyle>
<HeaderStyle font-bold="True"
forecolor="White"
backcolor="Black">
</HeaderStyle>
<Columns>
<ASP:EditCommandColumn
buttontype="PushButton"
updatetext="Update"
headertext="Edit"
canceltext="Cancel"
edittext="Edit">
</ASP:EditCommandColumn>
<ASP:BoundColumn datafield="LIVNAME"
headertext="Livery Name">
</ASP:BoundColumn>
<ASP:BoundColumn datafield="LEADTIME"
headertext="Lead Time">
</ASP:BoundColumn>
<ASP:ButtonColumn text="Delete"
buttontype="PushButton"
headertext="Delete"
commandname="Delete">
</ASP:ButtonColumn>
</Columns>
</asp:datagrid>
 
T

Tim Mackey

hi iain,
you probably have to dig in to the controls property of the cell in
question.
Control.Text only works if there is plain text with no server controls
inside the control.
in your case, i presume the edit mode of the datagrid has rendered some
textboxes instead.
you probably want something like this:

// if the textbox is the first control in the cell in edit mode...
string LLivery = (e.Item.Cells[1].Controls[0] as TextBox).Text;

// or you can use FindControl()
string LLivery = (e.Item.Cells[1].FindControl("txtLivery") as TextBox).Text;

hope this does it for you
tim

Iain said:
Hi All

Using Delphi 2006 developer - C# Project

I have the following 2 event handlers for the datagrid - see botton of
page
Both events will fire off correctly but i have a problem collecting the
correct data in teh Update event - I have noted the problem in the
event handler

protected void Livery_DeleteCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string LLivery = e.Item.Cells[1].Text;
string LLeadTime = e.Item.Cells[2].Text;
int LRow = e.Item.ItemIndex;

// If I step through this I can see the values of the cells of the
row highlighted when I press the delete button
}

protected void Livery_UpdateCommand(object source,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string LLivery = e.Item.Cells[1].Text;
string LLeadTime = e.Item.Cells[2].Text;
int LRow = e.Item.ItemIndex;

// If I step through this the values of e.Item.Cells[1].Text and
// e.Item.Cells[2].Text are blank. I could understand if the
// value contained the old (pre-update) values or the
// new (post-update) values but the fields are blank ?????
// e.Item.ItemIndex is showing the correct row index
}

Can anyone advise me why this is so and if so how do I rectify the
situation.

Many thanks in advance for assistance offered

Iain

<asp:datagrid id="Livery" runat="server"
allowsorting="True"
EnableViewState="true"
itemstyle-verticalalign="top"
headerstyle-font-bold="true"
headerstyle-forecolor="white"
headerstyle-backcolor="black"
autogeneratecolumns="false" font-size="8pt"
cellpadding="5"
width="92%"
onsortcommand="Livery_SortCommand"
ondeletecommand="Livery_DeleteCommand"
oneditcommand="Livery_EditCommand"
onupdatecommand="Livery_UpdateCommand"
oncancelcommand="Livery_CancelCommand">
<EditItemStyle borderstyle="Dashed"
bordercolor="#0000C0"
backcolor="#FFFFC0">
</EditItemStyle>
<ItemStyle verticalalign="Top">
</ItemStyle>
<HeaderStyle font-bold="True"
forecolor="White"
backcolor="Black">
</HeaderStyle>
<Columns>
<ASP:EditCommandColumn
buttontype="PushButton"
updatetext="Update"
headertext="Edit"
canceltext="Cancel"
edittext="Edit">
</ASP:EditCommandColumn>
<ASP:BoundColumn datafield="LIVNAME"
headertext="Livery Name">
</ASP:BoundColumn>
<ASP:BoundColumn datafield="LEADTIME"
headertext="Lead Time">
</ASP:BoundColumn>
<ASP:ButtonColumn text="Delete"
buttontype="PushButton"
headertext="Delete"
commandname="Delete">
</ASP:ButtonColumn>
</Columns>
</asp:datagrid>
 
I

Iain

Hi Tim

Thanks for your assistance.
The string LLivery = (e.Item.Cells[1].Controls[0] as TextBox).Text;
appears to work well.
Much appreciated

Iain
 

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,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top