2011-09-08 2 views
0

내 CS 파일에 이러한 코드 있습니다 ErrorMessage() 메서드에 따라 Gridview의 텍스트 색을 변경하는 방법은 무엇입니까?

//A method to display errors in the gridview 
    private string ErrorMessage(string input) 

     { 
      { 
       //if there are null values, error message will be displayed. 
       if (!string.IsNullOrEmpty(input)) 
        return input; 

      } 
      return "No value entered";// I am supposed to change this to red colour 


     } 

    public System.Drawing.Color InStockColor(string inStock) 
    { 
     return System.Drawing.Color.Red; 
    } 




// to read all lines of the posted csv file and put the lines in the grid view 
     var data = File.ReadAllLines(Server.MapPath(FilePath)) 
      // to split the lines according to commas 
      .Select(line => line.Split(',')) 

      .Select(columns => new { A = ErrorMessage(columns[0]), B = ErrorMessage(columns[1]), C = ErrorMessage(columns[2]), D = ErrorMessage(columns[3]), E = ErrorMessage(columns[4]), F = ErrorMessage(columns[5]), G = ErrorMessage(columns[6]), H = ErrorMessage(columns[7]), I = ErrorMessage(columns[8]) }); 


     myGridView.DataSource = data; 
     myGridView.DataBind(); 



<asp:GridView ID="myGridView" runat="server" CellPadding="4" 
    EnableModelValidation="True" ForeColor="#333333" GridLines="None" 
    Width="414px" align="center"> 
    <AlternatingRowStyle BackColor="White" /> 

    <EditRowStyle BackColor="#2461BF" /> 
    <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" /> 
    <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" /> 
    <RowStyle BackColor="#EFF3FB" /> 
    <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" /> 

</asp:GridView> 

는 지금까지 내가 텍스트 색상을 변경하는 방법을 만들었습니다. 자, 어떻게이 방법을 구현할 수 있습니까? 빨간색으로 변경해야하는 텍스트는 "No value entered!"입니다.

답변

0

이렇게하면 문제를 해결할 수 있습니다.

using System.Drawing; 

private string ErrorMessage(string input, int rowNr, int colNr)   
{    
    if (!string.IsNullOrEmpty(input)) 
     return input; 

    myGridView.Rows[rowNr].Cells[colNr].ForeColor = Color.Red; 
    return "No value entered"; 
} 

또 다른 방법은 ...이 같은

using System.Drawing; 

private string ErrorMessage(string input, int rowNr, int colNr)   
{    
    if (!string.IsNullOrEmpty(input)) 
     return input; 
    return "<font color='Red'>" + "No value entered" + "</font>"; 
} 

를 html 태그를 추가하는 것입니다하지만 <font>가되지 않으며 실제로 셀의 텍스트 값을 설정 때문에 태그를하지 않는 것이 좋습니다 html 코드.

희망이 도움이됩니다.

편집 :

당신이 코드 조각 ... 메이비 어떤 상황 이 구문의 어떤 종류를 제공을 설명해 할 수 있습니까?

.Select (...) 명령은 어떻게 작동하는지 설명 할 수 있습니까? 그들이 무엇을 할?

// to read all lines of the posted csv file and put the lines in the grid view 
     var data = File.ReadAllLines(Server.MapPath(FilePath)) 
      // to split the lines according to commas 
      .Select(line => line.Split(',')) 

      .Select(columns => new { A = ErrorMessage(columns[0]), B = ErrorMessage(columns[1]), C = ErrorMessage(columns[2]), D = ErrorMessage(columns[3]), E = ErrorMessage(columns[4]), F = ErrorMessage(columns[5]), G = ErrorMessage(columns[6]), H = ErrorMessage(columns[7]), I = ErrorMessage(columns[8]) }); 


     myGridView.DataSource = data; 
     myGridView.DataBind(); 
+0

내가 제공 한 방법을 사용하면 작동하지 않았습니다. 그것은 = ErrorMessage (columns [0]), B = ErrorMessage (columns [1]) ... 부분 인 오류를 제공합니다. HTML 태그를 사용하면 ""+ "값을 입력하지 않음"+ ""과 같이 모든 것을 표시합니다. 따라서 여전히 해결되지 않습니다 =/ – Mark20

+0

전화를 수정 했습니까? 당신은 다음과 같이 호출해야합니다 : ErrorMessage (columns [0], row number, column number) 당신은 빨강이 될 필요가있는 셀 (텍스트)의 행과 열을 지정해야합니다. –

관련 문제