简体   繁体   English

从gridview获取值

[英]Grabbing a value from gridview

I'm new to gridviews and I'm a bit confused. 我是gridview的新手,我有点困惑。

My gridview has autogeneratecolumn on false and I use 我的gridview的false上有autogeneratecolumn,我用

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

to fill the gridview. 填充gridview。 From what I understand, I have to use a RowCreated function to pull a value? 据我了解,我必须使用RowCreated函数提取值吗?

I have to parse all the values in one column. 我必须将所有值解析为一列。

You can use RowDataBound Event of the Gridview. 您可以使用Gridview的RowDataBound事件。

OnRowDataBound="RowDataboundEventHandler" Add this to your gridview and the handler will be: OnRowDataBound="RowDataboundEventHandler"将此添加到您的gridview中,该处理程序将是:

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. 假设reader是一个DataTable并且没有有关数据类型的任何信息,则可以使用以下内容。 You'll have to provide more information or write the yourCustomConversion() method yourself. 您必须提供更多信息或自己编写yourCustomConversion()方法。

var reader = getYourData();

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

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

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

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