在一个Button事件里面!!就是点击一下button按钮就会显示出datagridview,然后想让datagridview里面的单元格内容居中

解决方案 »

  1.   

    就在按钮事件里
    foreach (DataGridViewColumn item in this.dataGridView1.Columns)
    {
        item.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    }
          
    但是如果某些单元格单独设置了样式这样就不起作用了
      

  2.   


            private void dataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
            {
                e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            }
      

  3.   

    把你的代码放在dataGridView的CellFormatting事件里就行了!
      

  4.   

    一句代码搞定的问题,放在load事件里就可以了
    dgv.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
      

  5.   


    dgv.RowsDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
    直接放在load里,就可以了