By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,081 Members | 1,317 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,081 IT Pros & Developers. It's quick & easy.

How to correctly identify cells within a datagrid

P: n/a
Morning!
After several hours breaking my mind, finally my code works, but i think is
trash-code, can someone tellme how is the correct way to access the value of
my cells within a datagrid?... in some cases i can access the values with
e.Item.Cells[NumCell].Text, but in other cases i have to do weird thinks
like the following:

TextBox pp; //I know...disgusting!!!
pp= (TextBox) e.Item.Cells[0].Controls[0] ;
int id = Convert.ToInt32(pp.Text );
Bussiness biz = new Bussiness();
pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma chanchada...
biz.UpdateUser(id,pp.Text);
BindData();

Please any information who helpme to understand the way asp.net render this
cells will be welcome!

TIA
Ariel Gimenez
Argentina
Nov 15 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Ariel,

This is one problem that I come across everytime the grid is in edit mode.
Its perfectly normal (at least in my case).

regards,
Marco
"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Morning!
After several hours breaking my mind, finally my code works, but i think is trash-code, can someone tellme how is the correct way to access the value of my cells within a datagrid?... in some cases i can access the values with
e.Item.Cells[NumCell].Text, but in other cases i have to do weird thinks
like the following:

TextBox pp; //I know...disgusting!!!
pp= (TextBox) e.Item.Cells[0].Controls[0] ;
int id = Convert.ToInt32(pp.Text );
Bussiness biz = new Bussiness();
pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma chanchada... biz.UpdateUser(id,pp.Text);
BindData();

Please any information who helpme to understand the way asp.net render this cells will be welcome!

TIA
Ariel Gimenez
Argentina

Nov 15 '05 #2

P: n/a
Hi Ariel,

This is the way to go unfortunally, you could try to simplify a little it
anyway, maybe using a CommandArgument to receive the ID ( the cells[0] ? )
but to get the rest of the values entered by the users in the controls when
editing you will need to cast them back ,
One advice that I would give you is the use of FindControl( "Control
Name" ) instead of referencing the Controls[] by index, this give you
flexibility to add one or more controls inside the same cell.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Morning!
After several hours breaking my mind, finally my code works, but i think is trash-code, can someone tellme how is the correct way to access the value of my cells within a datagrid?... in some cases i can access the values with
e.Item.Cells[NumCell].Text, but in other cases i have to do weird thinks
like the following:

TextBox pp; //I know...disgusting!!!
pp= (TextBox) e.Item.Cells[0].Controls[0] ;
int id = Convert.ToInt32(pp.Text );
Bussiness biz = new Bussiness();
pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma chanchada... biz.UpdateUser(id,pp.Text);
BindData();

Please any information who helpme to understand the way asp.net render this cells will be welcome!

TIA
Ariel Gimenez
Argentina

Nov 15 '05 #3

P: n/a
Thanks Ignacio Machin!!!
Ill try the findcontrol, i suppose i will need to asign an id to the
controls in the datagrid hum...
I think my english is worst than my code so here is in spanish...

--translation spanish--
Gracias Ignacio Machin!!!
Voy a probar el findcontrol, supongo que para poder utilizarlo debo
asignarle algun id a los controles del datagrid...

Thanks again
Ariel
Argentina
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:%2***************@tk2msftngp13.phx.gbl...
Hi Ariel,

This is the way to go unfortunally, you could try to simplify a little it
anyway, maybe using a CommandArgument to receive the ID ( the cells[0] ? )
but to get the rest of the values entered by the users in the controls when editing you will need to cast them back ,
One advice that I would give you is the use of FindControl( "Control
Name" ) instead of referencing the Controls[] by index, this give you
flexibility to add one or more controls inside the same cell.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Morning!
After several hours breaking my mind, finally my code works, but i think is
trash-code, can someone tellme how is the correct way to access the value of
my cells within a datagrid?... in some cases i can access the values

with e.Item.Cells[NumCell].Text, but in other cases i have to do weird thinks
like the following:

TextBox pp; //I know...disgusting!!!
pp= (TextBox) e.Item.Cells[0].Controls[0] ;
int id = Convert.ToInt32(pp.Text );
Bussiness biz = new Bussiness();
pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma

chanchada...
biz.UpdateUser(id,pp.Text);
BindData();

Please any information who helpme to understand the way asp.net render

this
cells will be welcome!

TIA
Ariel Gimenez
Argentina


Nov 15 '05 #4

P: n/a
Hi Ariel,

Of course you should, I paste here a piece of one of my grids just to show
you how to do it, also there is a piece of a handler

<asp:templatecolumn ItemStyle-Width="420" >
<itemtemplate>
<asp:Label CssClass="text" Runat="server" Text='<%#
((Action)Container.DataItem).Comments%>' ID="Label8">
</asp:Label>
</itemtemplate>
<EditItemTemplate>
<asp:TextBox ID="CommentTXT" Runat=server TextMode=MultiLine
Text='<%# ((Action)Container.DataItem).Comments%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:templatecolumn>
In the code behind:

protected void ActionUpdateCommand(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Action action;
Label id =
(System.Web.UI.WebControls.Label)e.Item.FindContro l("ActionID");
if ( id.Text == "-1" )
{
action = new Action( theAccident);
action.DocStorePath = Server.MapPath( DocStorePath );
theAccident.Actions.Add( action);
}
else
action = theAccident.Actions.Find( Convert.ToInt32( id.Text));

TextBox CommentTXT = (TextBox)e.Item.FindControl("CommentTXT");
action.Comments = CommentTXT.Text;

}
Un Saludo
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl...
Thanks Ignacio Machin!!!
Ill try the findcontrol, i suppose i will need to asign an id to the
controls in the datagrid hum...
I think my english is worst than my code so here is in spanish...

--translation spanish--
Gracias Ignacio Machin!!!
Voy a probar el findcontrol, supongo que para poder utilizarlo debo
asignarle algun id a los controles del datagrid...

Thanks again
Ariel
Argentina
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message news:%2***************@tk2msftngp13.phx.gbl...
Hi Ariel,

This is the way to go unfortunally, you could try to simplify a little it
anyway, maybe using a CommandArgument to receive the ID ( the cells[0] ? ) but to get the rest of the values entered by the users in the controls

when
editing you will need to cast them back ,
One advice that I would give you is the use of FindControl( "Control
Name" ) instead of referencing the Controls[] by index, this give you
flexibility to add one or more controls inside the same cell.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:ex**************@TK2MSFTNGP12.phx.gbl...
Morning!
After several hours breaking my mind, finally my code works, but i think
is
trash-code, can someone tellme how is the correct way to access the

value
of
my cells within a datagrid?... in some cases i can access the values

with e.Item.Cells[NumCell].Text, but in other cases i have to do weird

thinks like the following:

TextBox pp; //I know...disgusting!!!
pp= (TextBox) e.Item.Cells[0].Controls[0] ;
int id = Convert.ToInt32(pp.Text );
Bussiness biz = new Bussiness();
pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma

chanchada...
biz.UpdateUser(id,pp.Text);
BindData();

Please any information who helpme to understand the way asp.net render

this
cells will be welcome!

TIA
Ariel Gimenez
Argentina



Nov 15 '05 #5

P: n/a
Thanks again!!!

Salu2
Ariel Gimenez
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote
in message news:Of*************@tk2msftngp13.phx.gbl...
Hi Ariel,

Of course you should, I paste here a piece of one of my grids just to show
you how to do it, also there is a piece of a handler

<asp:templatecolumn ItemStyle-Width="420" >
<itemtemplate>
<asp:Label CssClass="text" Runat="server" Text='<%#
((Action)Container.DataItem).Comments%>' ID="Label8">
</asp:Label>
</itemtemplate>
<EditItemTemplate>
<asp:TextBox ID="CommentTXT" Runat=server TextMode=MultiLine
Text='<%# ((Action)Container.DataItem).Comments%>'>
</asp:TextBox>
</EditItemTemplate>
</asp:templatecolumn>
In the code behind:

protected void ActionUpdateCommand(object sender,
System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
Action action;
Label id =
(System.Web.UI.WebControls.Label)e.Item.FindContro l("ActionID");
if ( id.Text == "-1" )
{
action = new Action( theAccident);
action.DocStorePath = Server.MapPath( DocStorePath );
theAccident.Actions.Add( action);
}
else
action = theAccident.Actions.Find( Convert.ToInt32( id.Text));

TextBox CommentTXT = (TextBox)e.Item.FindControl("CommentTXT");
action.Comments = CommentTXT.Text;

}
Un Saludo
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message
news:O8**************@TK2MSFTNGP11.phx.gbl...
Thanks Ignacio Machin!!!
Ill try the findcontrol, i suppose i will need to asign an id to the
controls in the datagrid hum...
I think my english is worst than my code so here is in spanish...

--translation spanish--
Gracias Ignacio Machin!!!
Voy a probar el findcontrol, supongo que para poder utilizarlo debo
asignarle algun id a los controles del datagrid...

Thanks again
Ariel
Argentina
"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us>

wrote
in message news:%2***************@tk2msftngp13.phx.gbl...
Hi Ariel,

This is the way to go unfortunally, you could try to simplify a little it anyway, maybe using a CommandArgument to receive the ID ( the cells[0] ? ) but to get the rest of the values entered by the users in the controls

when
editing you will need to cast them back ,
One advice that I would give you is the use of FindControl( "Control
Name" ) instead of referencing the Controls[] by index, this give you
flexibility to add one or more controls inside the same cell.
Cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Ariel Gimenez" <arielgimenez@--sacar--esto--yahoo.com> wrote in message news:ex**************@TK2MSFTNGP12.phx.gbl...
> Morning!
> After several hours breaking my mind, finally my code works, but i think is
> trash-code, can someone tellme how is the correct way to access the

value
of
> my cells within a datagrid?... in some cases i can access the values

with
> e.Item.Cells[NumCell].Text, but in other cases i have to do weird thinks > like the following:
>
> TextBox pp; //I know...disgusting!!!
> pp= (TextBox) e.Item.Cells[0].Controls[0] ;
> int id = Convert.ToInt32(pp.Text );
> Bussiness biz = new Bussiness();
> pp= (TextBox) e.Item.Cells[1].Controls[0]; // de nuevo la misma
chanchada...
> biz.UpdateUser(id,pp.Text);
> BindData();
>
> Please any information who helpme to understand the way asp.net render this
> cells will be welcome!
>
> TIA
> Ariel Gimenez
> Argentina
>
>



Nov 15 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.