简体   繁体   中英

Load data from gridview into a textbox

在此输入图像描述

I want to load data from a row that when selected by means of a button loads them into a format. For this I am doing with the CommandName, as if they were coordinates, clicking on such a row will load this data and so on for all. But I do not know how to do it in rows and cells with textbox.

I did so like it's in the code but I do not know if it's fine.

Where you have two dashed lines is how I wrote it for the Textbox.

In the form (format) I have Textbox and there should automatically appear the data when selecting any row.

if (e.CommandName.ToString() == "clic") {

//DETERMINING THE INDEX OF THE SELECTED ROW
    int indexrow = int.Parse(e.CommandArgument.ToString());

     int id = (int)this.gridview.DataKeys[indexrow]["Id"];
   -- TextBox1.Text = gridview.Rows[indexrow].Cells[2].ToString();

This is how you accomplish what you want (I think). But in your question I don't see a TextBox present in the GridView.

if (e.CommandName.ToString() == "clic")
{
    //cast the container of the sender back to a gridviewrow
    GridViewRow row = (GridViewRow)((Control)e.CommandSource).NamingContainer;

    //get the row number
    int indexrow = row.RowIndex;

    //get the value of cell 2 in the textbox
    TextBox1.Text = gridview.Rows[indexrow].Cells[2].Text;
}

If you mean to use "Edit" mode in the GridView with EditItemTemplate , then have a look at this tutorial .

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