DataGrid: OnUpdateCommand - getting the updated values

D

Dan

Greeting all,

I have been reading a number of posts and all the samples I have seen
are the same, yet I can't access the updated values of a TabelRow via:

(TextBox)e.Item.Cells.Controls[0]

I get a index out of bound error as there seems (from degbugging) to be
no controls on *any* of the cells???

I moved away from the traditional method of using asp:EditCommandColumn
as the update event wasn't invoking the associated updated method and
now use:

<Columns>
<asp:ButtonColumn Text="Edit" ButtonType="LinkButton"
CommandName="Edit"></asp:ButtonColumn>
<asp:ButtonColumn Visible=False Text="Update" ButtonType="LinkButton"
CommandName="Update"></asp:ButtonColumn>
<asp:ButtonColumn Visible=False Text="Cancel" ButtonType="LinkButton"
CommandName="Cancel"></asp:ButtonColumn>
</Columns>

Which do fire the correct methods. Have I missed something? A snippet
from the C# code is:

public void MyDataGrid_Update(Object sender, DataGridCommandEventArgs
e)
{
SqlCommand cmd = new SqlCommand("usp_update_server", conn);
cmd.CommandType = CommandType.StoredProcedure;

String[] cols = {"@srv_id", "@srv_name", "@sql_instance",
"@nt_hostname", "@dt_purchased", "@directorate"};

for (int i=4; i < e.Item.Cells.Count; i++) //skip the first 3
columns (edit/update/cancel) and the 4th column which is the key
{
int offset = 3;

String colvalue = (e.Item.Cells).Text;
int x = e.Item.Cells.Controls.Count;
System.Web.UI.WebControls.TableCell t = e.Item.Cells;

try
{
TextBox y = (TextBox)e.Item.Cells.Controls[0];
}
catch
{
;
}

if (cols[i-offset] == "@dt_purchased" && colvalue == "")
cmd.Parameters.Add(new
SqlParameter(cols[i-offset],DateTime.Today.ToString()));
else if (cols[i-offset] == "@dt_purchased")
cmd.Parameters.Add(new
SqlParameter(cols[i-offset],DateTime.Parse(colvalue)));
else
cmd.Parameters.Add(new SqlParameter(cols[i-offset],colvalue));
}

and aas you can see, I've checked all the cells for the controls, but
nothing!

The ASP is:

<form runat="server" name="currentServers" id="currentServers">
<asp:datagrid id="MyDataGrid" runat="server"
Width="700px"
BackColor="#CCCCFF"
BorderColor="Black"
CellPadding="3"
Font-Name="Verdana"
Font-Size="8pt"
HeaderStyle-BackColor="#aaaadd"
EnableViewState="False"
OnUpdateCommand="MyDataGrid_Update"
OnEditCommand="MyDataGrid_Edit"
OnCancelCommand="MyDataGrid_Cancel"
DataKeyField="srv_id"
Font-Names="Verdana">
<Columns>
<asp:ButtonColumn Text="Edit" ButtonType="LinkButton"
CommandName="Edit"></asp:ButtonColumn>
<asp:ButtonColumn Visible=False Text="Update"
ButtonType="LinkButton" CommandName="Update"></asp:ButtonColumn>
<asp:ButtonColumn Visible=False Text="Cancel"
ButtonType="LinkButton" CommandName="Cancel"></asp:ButtonColumn>
</Columns>
</asp:datagrid>
</form>

Thanks in advance for any help offered!
Regards,
Dan
 

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,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top