Monday, 24 December 2012 13:56

how to get selected row values from a Gridview control

Written by 
Rate this item
(1 Vote)

sometimes we need to show the selected row values of a gridview.
in this article i am gonna show how to show the values in
textboxes by c# programming language.

how to select a row in GridView control::

at first add the Gridview then bind a data by using SqlDataSource.
also add three textboxes to show a data in these Textboxes
when the row of the Gridview is selected.

you must need to set Bound column to view the text in the
Gridview control cell.

<asp:GridView runat ="Server" ID="myGridview" AutoGenerateColumns="False"
DataSourceID="SqlDataSrce" AutoGenerateSelectButton="True"
OnSelectedIndexChanged="myGridview_SelectIndexChange
Width="300px" Height="140px">
<Columns >
<asp:BoundField HeaderText ="Rowno" DataField ="Rowno" />
<asp:BoundField HeaderText ="FieldName" DataField ="FieldName"/>
<asp:BoundField HeaderText ="MarksValue" DataField ="marksValue" />
</Columns>
</asp:GridView><asp:SqlDataSource ID="SqlDataSrce" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectString %>"
SelectCommand="SELECT * FROM [emp]">
</asp:SqlDataSource>

so in Gridview properties box set AutoGenerateSelectButton to
true In Gridview Properties set 'AutoGenerateSelectButton=True'
so that when you See the Desing view of Gridview control,you
will see a link with the text "Select".

when you make a click on this 'select' link the select row
event is fired.so after that using cells property you can
select any needed item in the row and show them in the
textboxes.for this double click on the selectedIndexChanged
event and put the code below in the event function of the
aspx.cs file -

protected void myGridview_SelectIndexChange(object sender
,EventArgs e){
txtBoxRowid.Text = myGridview.SelectedRow.Cells[1].Text;
txtBoxName.Text = myGridview.SelectedRow.Cells[2].Text;
txtBoxMarks.Text = myGridview.SelectedRow.Cells[3].Text}

summary::
in this article we learned how to show the values of a
specific row of a Gridview control when it is selected.

Read 16577 times
Super User

Email This email address is being protected from spambots. You need JavaScript enabled to view it.

Latest discussions

  • No posts to display.