繁体   English   中英

使用 asp.net C# 在标签中添加 gridview 的值

[英]adding a gridview's values in a label with asp.net C#

我有一个 gridview 和一个按钮。当我点击按钮时,我的表单中会写一个句子。 我想调用在 gridview 中输入的数据并在标签中查看它们。 这是我的代码,但它不起作用:

//code of gridview:
 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" 
            CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource2" 
            CssClass="grid">
            <Columns>
                <asp:ImageField DataImageUrlField="ImageUrl" HeaderText="image">
                    <ControlStyle Height="130px" Width="130px" />
                </asp:ImageField>
                <asp:BoundField DataField="Description" HeaderText="department" 
                    SortExpression="Description" />
                <asp:BoundField DataField="ProductName" HeaderText="name" 
                    SortExpression="ProductName" />
                <asp:BoundField DataField="ProductID" HeaderText="code" 
                    SortExpression="ProductID" ReadOnly="True" />
            </Columns>
            <FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
            <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
            <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
            <RowStyle BackColor="White" ForeColor="#003399" />
            <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
            <SortedAscendingCellStyle BackColor="#EDF6F6" />
            <SortedAscendingHeaderStyle BackColor="#0D4AC4" />
            <SortedDescendingCellStyle BackColor="#D6DFDF" />
            <SortedDescendingHeaderStyle BackColor="#002876" />
        </asp:GridView>
//the code of button
protected void Button1_Click(object sender, EventArgs e)
    {
             if (RadioButton2.Checked)
                txtid.Text = "name"+ProductName+"department"+description+"code"+productID;
       
    }

好的,所以我们想要从网格中的给定行获取/抓取/获取数据。 在这种情况下,我们想要第一行。

对于数据绑定字段/列,您必须使用给定行的 .cells 集合。 (对于模板化控件(比如标准的 asp.net 文本框,你必须使用 find 控件)。

因此,要获取该数据,您可以使用以下命令:0 表示图像,1 表示描述 2 表示产品名称 3 表示产品 ID

因此:

GridViewRow gvRow = GridView1.Rows[0];

txtid.Text = "name" + gvRow.Cells[2].Text
   + "department" + gvRow.Cells[1].Text + "code" + gvRow.Cells[3].Text;
   

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM