【问题标题】:adding a gridview's values in a label with asp.net C#使用asp.net C#在标签中添加gridview的值
【发布时间】:2021-09-09 09:01:27
【问题描述】:

我有一个 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;
       
    }

【问题讨论】:

  • 您希望此按钮从网格中的哪一行获取数据?是否选择了当前行?
  • 我的网格视图中只有一行

标签: c# asp.net gridview webforms


【解决方案1】:

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

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

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

因此:

GridViewRow gvRow = GridView1.Rows[0];

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多