2013-09-27 72 views
0

這讓我非常興奮。我已經在網站上嘗試過每一個這樣的例子,並且無法使其工作。它不會觸發事件C#根據同一單元格中的文本更改ASP:Gridview單元格的背景色

  • 我在GridView性能
  • <%@ Import Namespace="System.Drawing" %> aspx頁面上RowDataBound="SYSGrid_RowDataBound",沒有.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#相當陌生,所以如果這是一個愚蠢的問題/問題,那麼我願意採取苛刻的建設性批評。提前致謝。

+1

你在哪裏綁定您的數據? – Youssef

回答

1

請確保您的<asp:GridView>在定義中有OnRowDataBound="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> 
+0

謝謝你的迴應。來自Prashanth Thurairatnam 的回答爲我工作。你的代碼也指出了我在GridView定義中犯的一個錯誤。我有RowDataBound =「SYSGrid_RowDataBound」,這是不同於OnRowDataBound =「SYSGrid_RowDataBound」...的缺失。 –

+0

太棒了!您應該考慮選擇一個答案並將其標記爲「已接受」 - 這將鼓勵其他人回答您可能遇到的任何問題,如果他們看到您是該網站的活躍成員。 – ethorn10

2

這裏是aspx本身的代碼示例。我添加了一些內嵌評論。請參閱他們以瞭解。將它放在一個aspx頁面中。你應該得到以下結果。

enter image description here

<%@ 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> 
+0

太棒了。這工作就像一個魅力!我不得不適應它到我現有的代碼中,但它完美地解決了我的問題。它沒有被解僱的主要原因是我的代碼錯誤。所有提供的解決方案都幫助我找到了它。非常感謝你。 –

+0

@ user1944143很高興解決了您的問題。你應該考慮接受一個合適的答案。 –

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; 
       } 
      } 
     } 
+0

謝謝你的迴應。來自Prashanth Thurairatnam 的回答爲我工作。 –

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; 
        } 

       } 
      } 
相關問題