Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Datagrid Control (http://www.velocityreviews.com/forums/f60-asp-net-datagrid-control.html)
-   -   DataGrid: OnUpdateCommand - getting the updated values (http://www.velocityreviews.com/forums/t762220-datagrid-onupdatecommand-getting-the-updated-values.html)

Dan 08-18-2005 10:16 AM

DataGrid: OnUpdateCommand - getting the updated values
 
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[i].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[i]).Text;
int x = e.Item.Cells[i].Controls.Count;
System.Web.UI.WebControls.TableCell t = e.Item.Cells[i];

try
{
TextBox y = (TextBox)e.Item.Cells[i].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



All times are GMT. The time now is 10:38 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.