简体   繁体   中英

Grabbing a value from gridview

I'm new to gridviews and I'm a bit confused.

My gridview has autogeneratecolumn on false and I use

    GridView1.DataSource = reader;
    GridView1.DataBind();

to fill the gridview. From what I understand, I have to use a RowCreated function to pull a value?

I have to parse all the values in one column.

You can use RowDataBound Event of the Gridview.

OnRowDataBound="RowDataboundEventHandler" Add this to your gridview and the handler will be:

protect void RowDataboundEventHandler(object sender, GridViewRowEventArgs e)
{
   if(e.Row.RowType == DataControlRowType.DataRow)
    {
      // Get value from column directly
      string value =  e.Row.Cells[1].Text;

      // if you want to find control value
      e.Row.FindControl("controlID");
    }
}

Assuming reader is a DataTable and without any information about what the type of data is, you can use the below. You'll have to provide more information or write the yourCustomConversion() method yourself.

var reader = getYourData();

foreach (DataRow row in reader.Rows)
{
    row["affectedColumn"] = yourCustomConversion(row["affectedColumn"]);
}

GridView1.DataSource = reader;
GridView1.DataBind();

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