在左侧对齐网格视图列值

本文关键字:视图 网格 对齐 | 更新日期: 2023-09-27 18:35:49

在左侧对齐列值 我正在使用此代码

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

在左侧对齐网格视图列值

尝试这样做

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

试试这个

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

并且必须使用 RowDataBound 事件 。