0

这让我发疯。我已经尝试了网站上的每个示例,但无法使其正常工作。它不会触发事件

  • RowDataBound="SYSGrid_RowDataBound"在 Gridview 属性中
  • <%@ Import Namespace="System.Drawing" %>在 aspx 页面上,没有 .cs 文件

这是代码

protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)    
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
      if (e.Row.Cells[9].Text == "Missing")
      {
          e.Row.Cells[9].BackColor = Color.Red;
          e.Row.Cells[9].ForeColor = Color.White;
       }
    }
}

我是 C# 的新手,所以如果这是一个愚蠢的问题/问题,那么我愿意接受严厉的建设性批评。提前致谢。

4

4 回答 4

2

这是一个带有 aspx 本身代码的示例。我添加了一些内联注释。参考他们以获得理解。把它放在一个aspx页面中。你应该得到以下结果。

在此处输入图像描述

<%@ Page Language="C#" AutoEventWireup="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Drawing" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        //Doing the binding when the page is loading for the first time (not on postbacks)
        if (!IsPostBack)
        {
            //Test datasource (Creating a datatable with 10 columns. Then adding 3 rows. cell indeces are 0 based.)

            DataTable dt = new DataTable();

            DataColumn dc1 = new DataColumn("col1");
            DataColumn dc2 = new DataColumn("col2");
            DataColumn dc3 = new DataColumn("col3");
            DataColumn dc4 = new DataColumn("col4");
            DataColumn dc5 = new DataColumn("col5");
            DataColumn dc6 = new DataColumn("col6");
            DataColumn dc7 = new DataColumn("col7");
            DataColumn dc8 = new DataColumn("col8");
            DataColumn dc9 = new DataColumn("col9");
            DataColumn dc10 = new DataColumn("col10");

            dt.Columns.Add(dc1);
            dt.Columns.Add(dc2);
            dt.Columns.Add(dc3);
            dt.Columns.Add(dc4);
            dt.Columns.Add(dc5);
            dt.Columns.Add(dc6);
            dt.Columns.Add(dc7);
            dt.Columns.Add(dc8);
            dt.Columns.Add(dc9);
            dt.Columns.Add(dc10);

            //Second row index 9 has "Missing" as the text
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "Missing" });
            dt.Rows.Add(new object[] { "cell1", "cell2", "cell3", "cell4", "cell5", "cell6", "cell7", "cell8", "cell9", "cell10" });

            //Set datasource. Then bind it. (here the grid is using auto generated columns)
            SYSGrid.DataSource = dt;
            SYSGrid.DataBind();
        }
    }

    protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)    
    {
       if (e.Row.RowType == DataControlRowType.DataRow)
       {
          if (e.Row.Cells[9].Text == "Missing")
          {
              e.Row.Cells[9].BackColor = Color.Red;
              e.Row.Cells[9].ForeColor = Color.White;
           }
        }
    }
</script>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>    
        <asp:gridview runat="server" ID="SYSGrid" OnRowDataBound="SYSGrid_RowDataBound"></asp:gridview>
    </div>
    </form>
</body>
</html>
于 2013-09-27T02:31:02.740 回答
1

确保你的<asp:GridView>hasOnRowDataBound="SYSGrid_RowDataBound"在定义中并且知道它.Cells[9]是从零开始的。

<asp:GridView runat="server" 
              ID="SYSGrid" 
              AutoGenerateColumns="false" 
              OnRowDataBound="SYSGrid_RowDataBound">
      <Columns>
            <asp:BoundField DataField="Column0" HeaderText="Column0" />
            <asp:BoundField DataField="Column1" HeaderText="Column1" />
            <asp:BoundField DataField="Column2" HeaderText="Column2" />
            <asp:BoundField DataField="Column3" HeaderText="Column3" />
            <asp:BoundField DataField="Column4" HeaderText="Column4" />
            <asp:BoundField DataField="Column5" HeaderText="Column5" />
            <asp:BoundField DataField="Column6" HeaderText="Column6" />
            <asp:BoundField DataField="Column7" HeaderText="Column7" />
            <asp:BoundField DataField="Column8" HeaderText="Column8" />
            <asp:BoundField DataField="Column9" HeaderText="Column9" />
      </Columns>
</asp:GridView>
于 2013-09-27T02:11:45.443 回答
0
    //.aspx   
     <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
                    DataKeyNames="ID" DataSourceID="AccessDataSource1"
                    ondatabound="GridView1_DataBound" onrowdatabound="GridView1_RowDataBound">
                    <Columns>
                        <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                            ReadOnly="True" SortExpression="ID" />
                        <asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
                        <asp:BoundField DataField="Location" HeaderText="Location"
                            SortExpression="Location" />
                        <asp:BoundField DataField="ParentID" HeaderText="ParentID"
                            SortExpression="ParentID" />
                        <asp:BoundField DataField="Content" HeaderText="Content"
                            SortExpression="Content" />
                        <asp:BoundField DataField="ShortContent" HeaderText="ShortContent"
                            SortExpression="ShortContent" />


//Change the Status Cell Color-----
                        <asp:TemplateField HeaderText="Status" ControlStyle-Width="75px" >
                            <ItemTemplate>
                                <asp:Label ID="Label1" runat="server" Text='<%# Eval("ParentID") %>'></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
//--------


                    </Columns>
                </asp:GridView>
                <asp:AccessDataSource ID="AccessDataSource1" runat="server"
                    DataFile="App_Data/Database1.accdb" SelectCommand="SELECT CMSMenus.ID, CMSMenus.Title, CMSMenus.Location, CMSMenus.ParentID, CMSMenus.Content, CMSMenus.ShortContent
            FROM CMSMenus;
            ">
                </asp:AccessDataSource>



        //C#

        protected void GridView1_DataBound(object sender, EventArgs e)
            {
                for (int i =0 ; i <= GridView1.Rows.Count -1 ;i++)
                {
                    Label lblparent = (Label)GridView1.Rows[i].FindControl("Label1");//Get ParentID

                    if (lblparent.Text == "1")
                    {
                        GridView1.Rows[i].Cells[6].BackColor = Color.Yellow;
                        lblparent.ForeColor = Color.Black;
                    }
                    else
                    {
                        GridView1.Rows[i].Cells[6].BackColor = Color.Green;
                        lblparent.ForeColor = Color.Yellow;
                    }

                }
            }
于 2013-11-29T11:50:32.210 回答
0

试试这个代码

  protected void SYSGrid_RowDataBound(object sender, GridViewRowEventArgs e)    
        {
           if (e.Row.RowType == DataControlRowType.DataRow)
           {
// Convert which control you use 
              Label lblcol = (Label) e.Row.findcontrol("yourcolumnname") ;

              if (lblcol.Text == "Missing")
              {
                  e.Row.Cells[9].BackColor = Color.Red;
                  e.Row.Cells[9].ForeColor = Color.White;
               }
            }
        }
于 2013-09-27T07:05:04.400 回答