Getting value from a cell from a gridview on RowDataBound event

前端 未结 12 967
[愿得一人]
[愿得一人] 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:27
    protected void gvbind_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes["onmouseover"] = "this.style.cursor='hand';";
            e.Row.Attributes["onmouseout"] = "this.style.textDecoration='none';";
            e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink(this.gvbind, "Select$" + e.Row.RowIndex);
        }
    }
    
    0 讨论(0)
  • 2020-12-23 17:31

    If you set the attribute Visible on the asp:BoundField to False. Like this

    <asp:BoundField DataField="F1" HeaderText="F1" Visible="False"/>
    

    You will not get any Text in the Cells[i].Text property when you loop the rows. So

    foreach (GridViewRow row in myGrid.Rows)
    {
      userList.Add(row.Cells[0].Text); //this will be empty ""
    }
    

    But you can set a column not visible by connecting the grid to the event OnRowDataBound then from here do this

    e.Row.Cells[0].Visible = false //now the cell has Text but it's hidden
    
    0 讨论(0)
  • 2020-12-23 17:33

    When you use a TemplateField and bind literal text to it like you are doing, asp.net will actually insert a control FOR YOU! It gets put into a DataBoundLiteralControl. You can see this if you look in the debugger near your line of code that is getting the empty text.

    So, to access the information without changing your template to use a control, you would cast like this:

    string percentage = ((DataBoundLiteralControl)e.Row.Cells[7].Controls[0]).Text;
    

    That will get you your text!

    0 讨论(0)
  • 2020-12-23 17:35
    Label lblSecret = ((Label)e.Row.FindControl("lblSecret"));
    
    0 讨论(0)
  • 2020-12-23 17:39

    First you need to wrap your code in a Label or Literal control so that you can reference it properly. What's happening is that there's no way for the system to keep track of it, because there's no control associated with the text. It's the control's responsibility to add its contents to viewstate.

    You need to use gridView.FindControl("controlName"); to get the control in the row. From there you can get at its properties including Text.

    You can also get at the DataItem property of the Row in question and cast it to the appropriate type and extract the information directly.

    0 讨论(0)
  • 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 <asp:TemplateField ...> tag, I changed the field in question to use <asp:BoundField ...>. Then, when I got to the RowDataBound event, the data could be accessed in the cell directly.

    The relevant fragments: First, the aspx page:

    <asp:GridView ID="gvVarianceReport" runat="server" ... >
    ...Other fields...
        <asp:BoundField DataField="TotalExpected" 
         HeaderText="Total Expected <br />Filtration Events" 
         HtmlEncode="False" ItemStyle-HorizontalAlign="Left" 
         SortExpression="TotalExpected" />
    ...
    </asp:Gridview>
    

    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.

    0 讨论(0)
提交回复
热议问题