如何在asp.net中获取datagrid第一列的值 [英] how to get the value of first column of datagrid in asp.net

查看:175
本文介绍了如何在asp.net中获取datagrid第一列的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用数据网格从数据库中获取和显示数据...

它运行良好...

现在我需要获取值所选行的第一列。我怎么才能拿到它...

请告诉我...



我对datagrid的设计如下..



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: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>

推荐答案

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


试试这个



int Id =(int )dataGridView1.SelectedRows [0] .Cells [0] .Value;
try this

int Id = (int)dataGridView1.SelectedRows[0].Cells[0].Value;





Grid1_SelectedIndexChanging(object sender,GridViewSelectEventArgs e)

{

value = Grid1.Rows [e.NewSelectedIndex] .Cells [1] .Text;
单元格中的
}

[输入列索引]然后你得到值。


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.


这篇关于如何在asp.net中获取datagrid第一列的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆