How to correctly identify cells within a datagrid

  • Thread starter Thread starter Ariel Gimenez
  • Start date Start date
A

Ariel Gimenez

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
 
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
 
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,
 
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 ) said:
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 said:
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
 
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.FindControl("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 said:
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:%[email protected]...
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 said:
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
 
Thanks again!!!

Salu2
Ariel Gimenez
Ignacio Machin ( .NET/ C# MVP ) said:
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.FindControl("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 said:
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:%[email protected]...
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

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
 
Back
Top