Getting value from a cell from a gridview on RowDataBound event

前端 未结 12 1018
[愿得一人]
[愿得一人] 2020-12-23 16:48
string percentage = e.Row.Cells[7].Text;

I am trying to do some dynamic stuff with my GridView, so I have wired up some code to the RowDataBound ev

12条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-23 17:39

    I had a similar question, but found the solution through a slightly different approach. Instead of looking up the control as Chris suggested, I first changed the way the field was specified in the .aspx page. Instead of using a tag, I changed the field in question to use . Then, when I got to the RowDataBound event, the data could be accessed in the cell directly.

    The relevant fragments: First, the aspx page:

    
    ...Other fields...
        
    ...
    
    

    Then in the RowDataBound event I can access the values directly:

    protected void gvVarianceReport_Sorting(object sender, GridViewSortEventArgs e)
    {
        if (e.Row.Cells[2].Text == "0")
        {
            e.Row.Cells[2].Text = "N/A";
            e.Row.Cells[3].Text = "N/A";
            e.Row.Cells[4].Text = "N/A";
        }
    }
    

    If someone could comment on why this works, I'd appreciate it. I don't fully understand why without the BoundField the value is not in the cell after the bind, but you have to look it up via the control.

提交回复
热议问题