毕业论文开发语言企业开发JAVA技术.NET技术WEB开发Linux/Unix数据库技术Windows平台移动平台嵌入式论文范文英语论文
您现在的位置: 毕业论文 >> net技术 >> 正文

Datagridview如何合并单元格和自动换行

更新时间:2015-12-18:  来源:毕业论文

首先我是读取excel然后存在dataset里放进datagridview,然后因为excel文件中有一些合并单元格的列,所以我用了DataGridViewCellPaintingEventArgs 绘制表格,产生合并单元格的效果

 public  void dgCellPainting(DataGridView dgStandard, string strColumnName, DataGridViewCellPaintingEventArgs e)
        {
            if (dgStandard.Rows.Count > 0)
            {
 
                if (dgStandard.Rows[0].Cells[0].Value.ToString().Trim() != string.Empty)
                {
                    try
                    {
                        if (dgStandard.Columns[strColumnName].Index == e.ColumnIndex && e.RowIndex >= 0)
                        {
                            using (
                                Brush gridBrush = new SolidBrush(dgStandard.GridColor),
                                backColorBrush = new SolidBrush(e.CellStyle.BackColor))
                            {
                                using (Pen gridLinePen = new Pen(gridBrush))
                                {
                                    // 擦除原单元格背景
                                    e.Graphics.FillRectangle(backColorBrush, e.CellBounds);
 
                                    if (e.RowIndex != dgStandard.RowCount - 1)
                                    {
                                        if (e.Value.ToString() != dgStandard.Rows[e.RowIndex + 1].Cells[e.ColumnIndex].Value.ToString())
                                        {
                                            e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线
                                        }
                                    }
                                    else
                                    {
                                        e.Graphics.DrawLine(gridLinePen, e.CellBounds.Left, e.CellBounds.Bottom - 1,
                                            e.CellBounds.Right - 1, e.CellBounds.Bottom - 1);//下边缘的线                      
                                    }
 
                                    //右侧的线
                                    e.Graphics.DrawLine(gridLinePen, e.CellBounds.Right - 1, //x1,y1,x2,y2
                                        e.CellBounds.Top, e.CellBounds.Right - 1,
                                        e.CellBounds.Bottom - 1);
 
                                    if (e.RowIndex == 0)
                                    {
                                        //绘制值
                                        if (e.Value != null)
                                        {
 
                                            e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                                Brushes.Crimson,
                                                e.CellBounds.X +(e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width)/2,
                                                e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                        }
                                    }
                                    else
                                    {
                                        if (e.Value.ToString() != dgStandard.Rows[e.RowIndex - 1].Cells[e.ColumnIndex].Value.ToString())
                                        {
                                            //绘制值
                                            if (string.IsNullOrEmpty(e.Value.ToString().Trim()).Equals(false))
                                            {
                                                e.Graphics.DrawString((String)e.Value, e.CellStyle.Font,
                                                    Brushes.Crimson, e.CellBounds.X + (e.CellBounds.Width - e.Graphics.MeasureString(e.Value.ToString().Trim(), e.CellStyle.Font).Width) / 2,
                                                    e.CellBounds.Y + 2, StringFormat.GenericDefault);
                                            }
                                        }
                                    }
 
                                    e.Handled = true;
                                }
                            }
                        }
                    }
                    catch { }
                }
            }
        }
同时因为某些表格的字数又比较多,所以又用了自动换行
//设置自动换行
 this.dataGridView1.DefaultCellStyle.WrapMode = DataGridViewTriState.True;
 //设置自动调整高度
 this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;


然而这里就产生了一个问题,如果我没有用DataGridViewCellPaintingEventArgs 绘制表格表达合并单元格效果,自动换行是生效的。但是如果我用了绘制表格的效果,行的大小是会改变,但是文字是没有自动换行的

用了下devexpress的控件gridcontrol,合并单元格算是解决了……但是自动换行还是个坎儿……话说这种控件不是完成度很高了么……也会这么坑么…
,C#有没什么控件可以比较好的实现自动换行和合并单元格,查了很多资料,有在datagridview里的cellpaiting做修改的,要想达到预定效果的合并居中极其麻烦,并且没有看到同时达到自动换行的。又看了下用GDI自己绘制表格的,同样也觉得麻烦不小,而且我之前是把数据存在dataset里的,对GDI画的又不能像datagridview直接数据源导入

设为首页 | 联系站长 | 友情链接 | 网站地图 |

copyright©youerw.com 优尔论文网 严禁转载
如果本毕业论文网损害了您的利益或者侵犯了您的权利,请及时联系,我们一定会及时改正。