当前位置:编程学习 > C#/ASP.NET >>

gridControl 单元格换行的问题

找了半天不知道在哪设置!!
就是数据满行后自动换下一行显示!!怎么设置!!?? --------------------编程问答-------------------- gridcontrol// 不知道是什么控件,不过帮顶一下. --------------------编程问答-------------------- DataGrid控件的单元格内文字列的换行表示,可以通过作成DataGridColumnStyle类的派生类,重载Paint方法描绘文字列实现。

下面的例子是继承DataGridTextBoxColumn类作成新类(DataGridTextBoxColumnEx)。

补充:如果使用VB.NET时,需要PropertyDescriptor可以把C#代码生成DLL然后引用。

C#]
 using System;
 using System.Drawing;
 using System.Windows.Forms;
 using System.Data;
 using System.ComponentModel;
 
 namespace Dobon.Samples.Forms
 {
     /// <summary>
     ///为了在DataGrid上表示改行字符串的DataGridColumnStyle
     /// </summary>
     public class DataGridTextBoxColumnEx : DataGridTextBoxColumn
     {
         //指定描绘文字列的Margin
         Point _margin = new Point(1, 2);
 
         //TypeConverter取得
         private TypeConverter _typeConverter = null; 
         public override PropertyDescriptor PropertyDescriptor
         {
             set
             {
                 base.PropertyDescriptor = value;
                 if (PropertyDescriptor != null &&
                     PropertyDescriptor.PropertyType != typeof(object))
                 {
                     _typeConverter =
                         System.ComponentModel.TypeDescriptor.GetConverter(
                         PropertyDescriptor.PropertyType);
                 }
             }
         }
 
         // Paint对象重载
         protected  override void Paint(Graphics g, 
             Rectangle bounds, 
             CurrencyManager source, 
             int rowNum, 
             Brush backBrush, 
             Brush foreBrush, 
             bool alignToRight)
         {
             //取得表示的字符串
             string text =
                 GetText(GetColumnValueAtRow(source, rowNum));
 
             StringFormat sf = new StringFormat();
             //指定配置
             switch (this.Alignment)
             {
                 case HorizontalAlignment.Left:
                     sf.Alignment = StringAlignment.Near;
                     break;
                 case HorizontalAlignment.Center:
                     sf.Alignment = StringAlignment.Center;
                     break;
                 case HorizontalAlignment.Right:
                     sf.Alignment = StringAlignment.Far;
                     break;
             }
             //指定文本的方向
             if (alignToRight)
                 sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
 
             //给背景涂颜色
             g.FillRectangle(backBrush, bounds);
 
             RectangleF rectf = new RectangleF(
                 bounds.X, bounds.Y, bounds.Width, bounds.Height);
             rectf.Inflate(-_margin.X, -_margin.Y);
 
             //文字列描绘
             g.DrawString(text, DataGridTableStyle.DataGrid.Font,
                 foreBrush, rectf, sf);
 
             sf.Dispose();
         }
 
         //返回表示的文字列
         private string GetText(object val)
         {
             if ((val as DBNull) != null)
                 return NullText;
 
             if (Format != null && Format.Length != 0 &&
                 (val as IFormattable) != null)
             {
                 try
                 {
                     return ((IFormattable) val).ToString(
                         Format, FormatInfo);
                 }
                 catch
                 {
                 }
             }
 
             if (_typeConverter != null &&
                 _typeConverter.CanConvertTo(typeof(string)))
             {
                 return (string) _typeConverter.ConvertTo(
                     val, typeof(string)); 
             }
 
             return (val != null ? val.ToString() : "");
         }
     }
 }
                 return (string) _typeConverter.ConvertTo(
                     val, typeof(string)); 
             }
 
             return (val != null ? val.ToString() : "");
         }
     }
 }
  



this.dataGridView1.RowsDefaultCellStyle.WrapMode = DataGridViewTriState.True;//换行
this.dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCells;//自动行高
foreach (DataGridViewColumn c in this.dataGridView1.Columns)
    c.AutoSizeMode = DataGridViewAutoSizeColumnMode.DisplayedCellsExceptHeader;//行动列宽


http://www.web521.com/design/P39/A073295.shtml

http://www.fish888.com/GridView-DataGrid-t118997 --------------------编程问答-------------------- gridControl.DataSource=ds;
gridControl.DataMember=ds.Tables[0].TableName;
DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit x=new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
gridView1.Columns[1].ColumnEdit=x;
gridView1.Columns[1].FiledName="内容";//绑定的字段
gridView1.Columns[1].Width=100;
gridView1.OptionsView.RowAutoHeight=true;

补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,