Click here to Skip to main content
15,911,306 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am using a datagrid for fetching and display datafrom the database...
its is working well...
now i need to fetch the value of the first column of the selected row. how can i fetch it...
pls tell me...

My design for datagrid is as below..

ASP
<asp:DataGrid ID="Grid1" runat="server" AllowPaging="True" AutoGenerateColumns="False" AutoGenerateSelectButton="True" CellPadding="4" DataKeyField="ano" Font-Bold="False" Font-Names="Constantia" ForeColor="#333333" GridLines="Both" PageSize="15" Width="100%" onselectedindexchanged="Grid1_SelectedIndexChanged">
<Columns>
<asp:ButtonColumn Text="Select" CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="ano" HeaderText="A.no."></asp:BoundColumn>
<asp:BoundColumn DataField="asset" HeaderText="Asset"></asp:BoundColumn>
<asp:BoundColumn DataField="asset_type" HeaderText="Asset Type"></asp:BoundColumn>
<asp:BoundColumn DataField="doc" HeaderText="Construction Date"></asp:BoundColumn>
<asp:BoundColumn DataField="coc" HeaderText="Construction Cost"></asp:BoundColumn>
</Columns>
<FooterStyle BackColor="#558ED5" Font-Bold="True" ForeColor="White" />
<SelectedItemStyle BackColor="#E9EDF4" Font-Bold="True" ForeColor="Navy" />
<PagerStyle BackColor="#558ED5" ForeColor="#333333" HorizontalAlign="Center" Mode="NumericPages" />
<AlternatingItemStyle BackColor="White" />
<ItemStyle BackColor="#E9EDF4" ForeColor="#333333" />
<HeaderStyle BackColor="#558ED5" Font-Bold="True" ForeColor="White" />
</asp:DataGrid>
Posted

C#
foreach (DataGridViewRow dr in Result.Rows)
//Result is the datagrid name
            {
                if (dr.Cells[0].Selected == true)
                {
//program to execute on datagrid click 
}

hope this will be helpfull for you
 
Share this answer
 
v2
try this

int Id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
 
Share this answer
 
Comments
dimtdj 4-Jul-13 6:48am    
its giving an error...
selectedRows is not a property defined... missing user directive or assemebly refernce
C#


Grid1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
{
value=Grid1.Rows[e.NewSelectedIndex].Cells[1].Text;
}
in Cells[enter your column index ] then u get the value.
 
Share this answer
 
Comments
dimtdj 4-Jul-13 6:54am    
its giving error with Rows...
same error as in the solution 2
Siddharth Ghule 4-Jul-13 7:01am    
which error is send.
Siddharth Ghule 4-Jul-13 7:05am    
onselectedindexchanged="Grid1_SelectedIndexChanged"
change this event and done same work in Grid1_SelecedIndexChaning.
C#
string str = Grid1.Rows[Grid1.SelectedRows[0].Index].Cells[your column].Value.ToString();


Regards..:laugh:
 
Share this answer
 

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