2017-02-06 2 views
0

最初の列のグリッドビューは、ヘッダーと同じです。すなわち、autogeneratecolumnsがtrueの場合と同じ値です。HeaderTextがIntersectと等しい場合RowDataboundで行う必要があります。最初の列のテキスト、黄色に色を変更します。 Desireed Gridview Outputの添付イメージをご覧ください。GridView:OnRowDatabound HeaderTextは、AutoGenerateColumnでColumnTextと同じです。C#Asp.net

GridViewDesiredOutput

HTML

<asp:GridView ID="GvSamples" OnRowDataBound="GvSamples_RowDataBound" runat="server" AutoGenerateColumns="True"> 

C#

public void BindSamplesGrid() 
    { 
     DataTable _dt = new DataTable(); 
     _dt = BL.GetSample(_conn); 
     GvSamples.DataSource = _dt; 
     GvSamples.DataBind(); 
    } 


protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      for (int i = 0; i < e.Row.Cells.Count; i++) 
      { 
       if (e.Row.Cells[i].Text == e.Row.Cells[i].Text) 
       e.Row.Cells[i].Text = e.Row.Cells[i].Text.Replace("_", " "); 
      } 
     } 
    } 

私はasp.net C#GridViewのを使用しています。

ありがとうございました

答えて

0

コードにいくつかの問題があります。まず、DataControlRowType.Headerのヘッダー行にしかアクセスできないので、希望通りにセルを着色するには、DataControlRowType.DataRowで行う必要があります。 次に、の代わりにセルの色を黄色にすることを除いて、これはあまり意味がありませんが、全く同じ値を評価しているので、常にtrueになります。e.Row.Cells[i].Text == e.Row.Cells[i].Text

以下のスニペットは必要なものです。現在の行番号を取得し、正しいセルの色を黄色にします。

protected void GvSamples_RowDataBound(object sender, GridViewRowEventArgs e) 
{ 
    //check if the row is a datarow 
    if (e.Row.RowType == DataControlRowType.DataRow) 
    { 
     //get the current row number 
     int rowIndex = e.Row.RowIndex; 

     //check if the rownumber does not exceed the column count 
     if (rowIndex < e.Row.Cells.Count - 1) 
     { 
      //make the cell yellow 
      e.Row.Cells[rowIndex + 1].BackColor = Color.Yellow; 
     } 
    } 
} 

enter image description here

+0

あなたにVDをありがとう、しかし、要件は、1列目の行とヘッダが同じであるべきです。添付の画像をご覧ください。もう一度ありがとう – user2394918

+0

私は実装して、それは動作します私の最後のコメントを無視してください、ありがとうVD! – user2394918

関連する問題