DataGridView合并单元格后如何使文字居中显示,如果文本长度超出单元格长度,隐藏超出部分

解决方案 »

  1.   

    System.Windows.Forms.DataGridViewCellStyle   dataGridViewCellStyle1   =   new   System.Windows.Forms.DataGridViewCellStyle(); dataGridViewCellStyle1.Alignment   =   System.Windows.Forms.DataGridViewContentAlignment.MiddleCenter; this.fParentDataGridViewTextBoxColumn.DefaultCellStyle   =   dataGridViewCellStyle1;
    DefaultCellStyle  
     
      

  2.   

    附上重绘单元格代码
    #region 重绘单元格
            /// <summary>
            /// 重绘单元格
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void dataGridViewBom_CellPainting(object sender, DataGridViewCellPaintingEventArgs e)
            {
                if (e.RowIndex >= 0 && e.ColumnIndex > -1)
                {
                    if (this.dataGridViewBom.Rows[e.RowIndex].DefaultCellStyle.BackColor == Color.Gray && this.dataGridViewBom.Rows[e.RowIndex].ReadOnly == true)
                    {
                        Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
                        Editor ed = doc.Editor;
                        using (Brush gridBrush = new SolidBrush(this.dataGridViewBom.GridColor), backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                        {
                            using (Pen gridLinePen = new Pen(gridBrush))
                            {
                                //擦出单元格背景
                                e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
                                if (e.ColumnIndex == this.dataGridViewBom.ColumnCount - 1)
                                {
                                    //画右侧线
                                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, e.CellBounds.Top, e.CellBounds.Right - 1, e.CellBounds.Bottom);
                                }
                                double rowLen = this.dataGridViewBom.Columns.GetColumnsWidth(DataGridViewElementStates.Displayed);
                                Graphics g = this.CreateGraphics();
                                //if (e.Value != null)
                                //{
                                    
                                //}
                                string s = "";
                                for (int i = 0; i < equipNameList.Count; i++)
                                {
                                    if (e.RowIndex == Convert.ToInt32(equipNameList[i][1]))
                                    {
                                        s = equipNameList[i][0];
                                        break;
                                    }
                                }
                                try
                                {
                                    e.Graphics.DrawString(this.dataGridViewBom.Rows[e.RowIndex].Cells[0].Value.ToString(), e.CellStyle.Font, Brushes.Red, (float)rowLen / 2 - 20, e.CellBounds.Y + 5, StringFormat.GenericDefault);
                                }
                                catch
                                {
                                    e.Graphics.DrawString(s, e.CellStyle.Font, Brushes.Red, (float)rowLen / 2 - 20, e.CellBounds.Y + 5, StringFormat.GenericDefault);
                                }
                                //else
                                //{
                                //    e.Graphics.DrawString("", e.CellStyle.Font, Brushes.Red, (float)rowLen / 2 - 20, e.CellBounds.Y + 5, StringFormat.GenericDefault);
                                //}
                                //下边缘线
                                e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1, e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);
                                e.Handled = true;
                            }
                        }
                        ed.UpdateScreen();
                    }
                }
            }
            #endregion
      

  3.   


    .cut_div{white-space:nowrap;overflow:hidden;text-overflow:ellipsis;}<div class="cut_div" title='<%#((DataRowView)Container.DataItem)[""]%>'></div>