2013-10-17 33 views
0

對齊列值我使用這個代碼在左邊對齊GridView的列值在左側

protected void GridView1_RowDataBound(object o, GridViewRowEventArgs e) 
{ 
    //Assumes the Price column is at index 4 
    if (e.Row.RowType == DataControlRowType.DataRow) 
     e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Right; 
} 

但是我沒有得到,因爲我控制電網這樣

protected void GridView1_DataBound1(object sender, EventArgs e) 
{ 
    for (int rowIndex = grdtcwisetarget.Rows.Count - 2; rowIndex >= 0; rowIndex--) 
    { 
     GridViewRow gvRow = grdtcwisetarget.Rows[rowIndex]; 
     GridViewRow gvPreviousRow = grdtcwisetarget.Rows[rowIndex + 1]; 
     for (int cellCount = 0; cellCount < 2;cellCount++) 
     { 
      if (gvRow.Cells[cellCount].Text == gvPreviousRow.Cells[cellCount].Text) 
      { 
       if (gvPreviousRow.Cells[cellCount].RowSpan < 2) 
       { 
        gvRow.Cells[cellCount].RowSpan = 2; 
       } 
       else 
       { 
        gvRow.Cells[cellCount].RowSpan = 
        gvPreviousRow.Cells[cellCount].RowSpan + 1; 
       } 
      gvPreviousRow.Cells[cellCount].Visible = false; 
      } 
     } 
     } 
    } 
+0

我正在嘗試這種代碼也保護無效GridView1_RowDataBound(對象o ,GridViewRowEventArgs e) { //假設價格列在索引4 if(e.RowType == DataControlRowType.DataRow) e.Row.Cells [2] .Horizo​​ntalAlign = Horizo​​ntalAlign.Right; } –

回答

0

嘗試這

e.Row.Cells[2].HorizontalAlign = HorizontalAlign.Left;// Not Right 

,並且必須使用的RowDataBound事件。

+0

@REx和Ramesh ,,,謝謝你的迴應,,,,雷克斯我明白了,,,,但所有的列對齊左,,,,但我只需要一列左側,,,其他列的值是數字,,,那是y –

1

嘗試這樣

foreach (GridViewRow row in GridView1.Rows) 
     { 
      foreach (TableCell cell in row.Cells) 
      { 
       cell.Attributes.CssStyle["text-align"] = "left"; 
      } 
     } 

而且檢查這個屬性ItemStyle-HorizontalAlign

<Columns> 
    //any type of column here 
    ItemStyle-HorizontalAlign="Right" ItemStyle-Width="80" /> 
</Columns> 

爲特定的列僅

foreach (GridViewRow row in grdTest.Rows) 
     { 
      row.Cells["cell index here"].Attributes.CssStyle["text-align"] = "left"; 
     } 
+0

,,謝謝你的迴應,,,,雷克斯我知道了,,,,但所有列對齊左,,,,但我只需要一列左側,,,其他列右值是數字,,,這是y –

+0

只有一列ItemStyle-Horizo​​ntalAlign =「右」只有該列 – Rex

+0

我綁定數據動態Rex –