简体   繁体   中英

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

I have a gridview and a button.When I click the button a sentence is written in my form. I want call the datas entered in the gridview and see them in the label. Here is my code but it does not work:

//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;
       
    }

Ok, so we want to get/grab/fetch data from a given row in the grid. And in this case, we want the first row.

For data bound fields/columns, you have to use the .cells collection of the given row. (for templated controls (say standard asp.net text boxes, you have to use find control).

So, to get that data, you can use this: 0 would be image, 1 would be Description 2 would be ProductName 3 would be productID

Hence:

GridViewRow gvRow = GridView1.Rows[0];

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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