How to get a particular selected row ID from a grid view .

  • Thread starter Thread starter Maheshkumar.R
  • Start date Start date
M

Maheshkumar.R

Hi groups,

How to get a particular selected row ID from a grid view control where i have placed imagebutton.
On click event how i can get the ROW ID ?
 
Yeah it's not real obvious. I think you can get what you want from
this:

DataRowView rv = (DataRowView)dg.SelectedRows[0].DataBoundItem;

DataRow row = rv.Row;

//Use row like normal.
 
Hi,

It depends of how you created the grid, did you bind the rowID to any column or control?
how you declared your grid?

you could do like this ( CtpRecord is a business class and I'm binding the grid to a collection ) :

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="100" ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<asp:LinkButton Runat=server CommandName="edit" CommandArgument='<%# ((CtpRecord)Container.DataItem).ID.ToString()%>' >
<span ><%# ((CtpRecord)Container.DataItem).LogNumber%></span>
</asp:LinkButton>

</itemtemplate>
</asp:templatecolumn>


Then in the code behind:
protected void RecordEditCommand(object sender, DataGridCommandEventArgs e)
{
int ID = Convert.ToInt32( e.CommandArgument ) );

}


Cheers,

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



Hi groups,

How to get a particular selected row ID from a grid view control where i have placed imagebutton.
On click event how i can get the ROW ID ?
 
Thanks a lot Machin..
Mahes


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message Hi,

It depends of how you created the grid, did you bind the rowID to any column or control?
how you declared your grid?

you could do like this ( CtpRecord is a business class and I'm binding the grid to a collection ) :

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="100" ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<asp:LinkButton Runat=server CommandName="edit" CommandArgument='<%# ((CtpRecord)Container.DataItem).ID.ToString()%>' >
<span ><%# ((CtpRecord)Container.DataItem).LogNumber%></span>
</asp:LinkButton>

</itemtemplate>
</asp:templatecolumn>


Then in the code behind:
protected void RecordEditCommand(object sender, DataGridCommandEventArgs e)
{
int ID = Convert.ToInt32( e.CommandArgument ) );

}


Cheers,

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



Hi groups,

How to get a particular selected row ID from a grid view control where i have placed imagebutton.
On click event how i can get the ROW ID ?
 
Hi,


Did it work?

After reading the other reply I was unsure if it was a web grid or a win one.

Also r u using C# 2.0 ?

The code I sent was done under 1.1 but it should work the same under 2.0

cheers,

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

Thanks a lot Machin..
Mahes


"Ignacio Machin ( .NET/ C# MVP )" <ignacio.machin AT dot.state.fl.us> wrote in message Hi,

It depends of how you created the grid, did you bind the rowID to any column or control?
how you declared your grid?

you could do like this ( CtpRecord is a business class and I'm binding the grid to a collection ) :

<asp:templatecolumn ItemStyle-VerticalAlign="Top" ItemStyle-Width="100" ItemStyle-HorizontalAlign="left" >
<itemtemplate>
<asp:LinkButton Runat=server CommandName="edit" CommandArgument='<%# ((CtpRecord)Container.DataItem).ID.ToString()%>' >
<span ><%# ((CtpRecord)Container.DataItem).LogNumber%></span>
</asp:LinkButton>

</itemtemplate>
</asp:templatecolumn>


Then in the code behind:
protected void RecordEditCommand(object sender, DataGridCommandEventArgs e)
{
int ID = Convert.ToInt32( e.CommandArgument ) );

}


Cheers,

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



Hi groups,

How to get a particular selected row ID from a grid view control where i have placed imagebutton.
On click event how i can get the ROW ID ?
 
Back
Top