GridView中的OnRowDataBound事件根本没有触发

时间:2017-01-24 20:50:20

标签: c# gridview code-behind

我通常不使用OnRowDataBound,但在这种情况下我需要根据特定字段更改行的背面颜色。

这是我的ASPX:

<div id="divGrid" style='width:920px; height:430px; overflow:auto'>
    <asp:DataGrid ID="DataGrid_AuditSearch" runat="server"
        AllowPaging="True" AllowSorting="True" CellPadding="4" ForeColor="#333333" 
        GridLines="None" AutoGenerateColumns="False" 
        OnRowDataBound="DataGrid_AuditSearch_RowDataBound" 
        OnCancelCommand="DataGrid_AuditSearch_CancelCommand" 
        OnUpdateCommand="DataGrid_AuditSearch_UpdateCommand" 
        OnEditCommand="DataGrid_AuditSearch_EditCommand">
        <AlternatingItemStyle Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <EditItemStyle BackColor="#999999" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <FooterStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <HeaderStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <PagerStyle BackColor="#5D7B9D" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Width="920px" Font-Underline="False" />
        <SelectedItemStyle BackColor="#E2DED6" Font-Bold="False" Font-Italic="False" 
            Font-Overline="False" Font-Strikeout="False" Font-Underline="False" />
        <Columns>
            <asp:EditCommandColumn ButtonType="PushButton" CancelText="Cancel" 
                EditText="Select" UpdateText="Update"></asp:EditCommandColumn>

            <asp:BoundColumn DataField="AUDIT_ID" HeaderText="Audit ID"/>  
            <asp:BoundColumn DataField="PLAN_ID" HeaderText="Plan ID"/>  
            <asp:BoundColumn DataField="PLAN_DESC" HeaderText="Plan Desc"/>  
            <asp:BoundColumn DataField="DOC_TYPE" HeaderText="Doc Type"/>
            <asp:BoundColumn DataField="PRODUCT" HeaderText="Product"/>
            <asp:BoundColumn DataField="PLAN_TYPE" HeaderText="Plan Type"/>  
            <asp:BoundColumn DataField="Auditor_ID" HeaderText="Auditor ID"/> 
        </Columns>

    </asp:DataGrid>
    <asp:Label ID="lblEmpty" runat="server" Visible="false" Style="font-weight:bold; font-size:large;"></asp:Label>
</div> 

这是我的C#Code-Behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        //Sometimes you need the parameter, sometimes you don't.  This works for both cases.
        txtAuditSearch.Text = (Request["AuditID"] ?? String.Empty).ToString();

        Show_Data(0);
    }
    else
    {

    }
}

public void Show_Data(int AuditID)
{
    OracleConnection conn = GetConnection();
    try
    {

        {
            conn.ConnectionString = ConfigurationManager.ConnectionStrings["ConnCST"].ToString();

            OracleCommand cmd3 = new OracleCommand();
            cmd3.Connection = conn;


            string sqlquery2;

            //sqlquery2 = "SELECT * FROM F_Audit_Plan Where Audit_ID = " + AuditID + "";

            sqlquery2 = "SELECT FAP.AUDIT_ID, FAP.PLAN_ID, FAP.PLAN_DESC, DTY.DOC_TY AS DOC_TYPE, DP.PRODUCT, ";
            sqlquery2 = sqlquery2 + "DPT.PLAN_TYPE, FA.Auditor_Lan_ID AS Auditor_ID, FAP.Plan_Review_Ind ";
            sqlquery2 = sqlquery2 + "FROM F_Audit_Plan FAP ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_DOC_TY DTY ";
            sqlquery2 = sqlquery2 + "ON FAP.DOC_TY_ID = DTY.DOC_TY_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_PRODUCT DP ";
            sqlquery2 = sqlquery2 + "ON FAP.PRODUCT_ID = DP.PRODUCT_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN D_PLAN_TYPE DPT ";
            sqlquery2 = sqlquery2 + "ON FAP.PLAN_TY_ID = DPT.PLAN_TY_ID ";
            sqlquery2 = sqlquery2 + "LEFT JOIN F_Audit FA ";
            sqlquery2 = sqlquery2 + "ON FA.Audit_ID = FAP.Audit_ID ";
            sqlquery2 = sqlquery2 + "Where FAP.Audit_ID = " + AuditID + " ";
            sqlquery2 = sqlquery2 + "ORDER BY FAP.PLAN_DESC ASC ";

            cmd3.CommandText = sqlquery2;

            var SearchAdapter = new OracleDataAdapter(cmd3);
            var ds = new DataSet();
            SearchAdapter.Fill(ds);

            // Perform the binding.
            DataGrid_AuditSearch.DataSource = ds;
            DataGrid_AuditSearch.DataBind();

            if (DataGrid_AuditSearch.Items.Count < 1)
            {
                    lblEmpty.Visible = true;
                    lblEmpty.Text = "There is no data to display";
            }
            else
            {
                    lblEmpty.Visible = false;
            }

            conn.Close();
            //DataGrid_AuditSearch.Columns[3].Visible = false;
            //DataGrid_AuditSearch.Columns[1].Visible = false;
        }
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        conn.Close();
    }

}

protected void DataGrid_AuditSearch_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        string Status = Convert.ToString(DataBinder.Eval(e.Row.DataItem, "Plan_Review_Ind"));

        if (Status == "Y")
        {
                e.Row.Attributes["style"] = "background-color: #28b779";
        }
        else
        {
                e.Row.Attributes["style"] = "background-color: #da5554";
        }
    }
}

我在DataGrid_AuditSearch_RowDataBound函数的第一行放置了一个断点,它甚至都没有命中它。知道我做错了吗?

2 个答案:

答案 0 :(得分:0)

Check this guy out,看起来设置AutoGenerateColumns="False"会打破这一点。

答案 1 :(得分:0)

您使用的是DataGrid,而不是GridView。 DataGrid没有OnRowDataBound事件。请改用OnItemDataBound事件。

Microsoft了解详情。