Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi -

I have a data bound grid and when a row is clicked I need the value out of the first cell of the row that was clicked. I'm not exactly sure how to achieve this on a data grid. Here is the code for the data grid. Ideally what I want is when the row is clicked get the value of the first cell so in the code behind I can pass that value as a parameter in a store procedure.

<asp:datagrid id=dgListaDistributori runat="server" CssClass="tblDGStandard" Width="100%" HeaderStyle-VerticalAlign="Middle" HeaderStyle-Height="20" ShowHeader="True" AutoGenerateColumns="False" OnPageIndexChanged="PageGrid2" AllowPaging="True" CellSpacing="1" CellPadding="0" BorderWidth="0px">  <AlternatingItemStyle CssClass="tblDGStandardAlt"></AlternatingItemStyle>
	 <Columns>
		 <asp:BoundColumn DataField="DISV_CODDIS" HeaderText="Cod.Distr.">
			<ItemStyle Height="20"></ItemStyle>
							                <HeaderStyle CssClass="tblDGStandardHeader" Width="60"></HeaderStyle>
						                </asp:BoundColumn>
						                
						                <asp:BoundColumn DataField="DISV_NAZIONE" HeaderText="Naz.">
							                <ItemStyle Height="20"></ItemStyle>
							                <HeaderStyle CssClass="tblDGStandardHeader" Width="20"></HeaderStyle>
						                </asp:BoundColumn>
						                				
						                	
						                <asp:TemplateColumn HeaderStyle-CssClass="tblDGStandardHeader" HeaderStyle-Width="20">
                                            <ItemTemplate>
                                                <asp:ImageButton id="btnCancelDis" Runat="server" ImageUrl="img\delete.gif" CommandName="CancellaDis"></asp:ImageButton>
                                                <ajaxToolkit:ConfirmButtonExtender ID="ajaxCBE1"  runat="server" TargetControlID="btnCancelDis" />                                                               
                                            </ItemTemplate>
                                        </asp:TemplateColumn>
                                        <asp:TemplateColumn HeaderStyle-CssClass="tblDGStandardHeader" HeaderStyle-Width="20">
                                            <ItemTemplate>
                                                <asp:ImageButton id="switchAccount" Runat="server" ImageUrl="img\refresh.png"  CommandName="logOut"></asp:ImageButton>                                                
                                            </ItemTemplate>
                                        </asp:TemplateColumn>
</Columns>
					               
</asp:datagrid>
Posted

1 solution

Based on what is listed above, you need to create an event handler for the OnSelectedIndexChanged event - see below;

ASP.NET
<asp:datagrid id="dgListaDistributori" runat="server" cssclass="tblDGStandard" width="100%" headerstyle-verticalalign="Middle" headerstyle-height="20" showheader="True" autogeneratecolumns="False" onpageindexchanged="PageGrid2" allowpaging="True" cellspacing="1" cellpadding="0" borderwidth="0px" xmlns:asp="#unknown"> OnSelectedIndexChanged="gListaDistributori_SelectedIndexChanged"></asp:datagrid>


In your code behind you can then access the cell value as follows;
VB
Dim strMyValue as String
strMyValue = dgListaDistributori.SelectedItem.Cells(1).Text
 
Share this answer
 
Comments
Troy Bryant 6-Jul-15 11:57am    
I originally had that but was getting a null object exception hence I started thinking there was another way to access the rows and cells

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900