CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
可用分押宝游戏火热进行中... 专题改版:Java Web 专题
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  .NET技术 >  C#

(线上等)高分请教如何在DataGrid中自己添加控件,如CheckBox、IMG等等,要求都是动态写语句添加

楼主jjkkwwww(第六感气味)2005-08-02 13:52:49 在 .NET技术 / C# 提问

现在碰到个问题,想请教大家,就是如何用语句添加控件到DataGrid中,其中DataGrid已经绑定了DataTable,但是我想要加一列CheckBox,其中DataGrid也是自己手写添加程序代码的,谢谢了。 问题点数:50、回复次数:6Top

1 楼openxyj(爱我所爱,想我所想)回复于 2005-08-02 14:04:28 得分 0

顶,共同关注中!  
   
   
  Top

2 楼rainlake(rainlake)回复于 2005-08-02 14:26:46 得分 0

TableStyles.Add....Top

3 楼tianxm(人生几何)回复于 2005-08-02 14:33:29 得分 10

在绑定数据之前,先定义TableStyles  
  在TableStyle定义一列“你样式”的列  
  就OK了Top

4 楼openxyj(爱我所爱,想我所想)回复于 2005-08-02 15:24:26 得分 0

强烈关注中!Top

5 楼zhqs1000(子鱼)回复于 2005-08-02 15:37:36 得分 0

加个模板列Top

6 楼jerry_qin()回复于 2005-08-02 16:00:21 得分 40

一个在DataGrid实现ComboBox的类:  
  using   System.Collections;  
  using   System.ComponentModel;  
  using   System.Drawing;  
  using   System.Windows.Forms;  
  using   System.Data;  
   
  namespace   DataGridAndComboBox  
  {  
  public   class   DataGridComboBox:ComboBox  
  {  
  //   继承下拉列表框类  
  public   DataGridComboBox()  
  {  
  }  
  private   void   InitializeComponent()  
  {  
   
  }  
  }  
  public   class   DataGridComboBoxColumn:DataGridColumnStyle  
  {  
  //在dataGrid中创建一个下拉列表框,dataGrid中的这一列都拥有相同的下拉选项    
  private   int   xMargin   =   2;  
  private   int   yMargin   =   1;  
  private   DataGridComboBox   Combo;  
  private   string   _DisplayMember;  
  private   string   _ValueMember;  
  //获取编辑状态  
  private   string   OldVal=new   string(string.Empty.ToCharArray());  
  private   bool   InEdit=   false;  
  //   根据顺序号建立一个新的下拉列    
  public   DataGridComboBoxColumn(DataTable   DataSource,   int   DisplayMember,int   ValueMember)  
  {  
  Combo   =   new   DataGridComboBox();  
  _DisplayMember   =   DataSource.Columns[DisplayMember].ToString();  
  _ValueMember   =   DataSource.Columns[ValueMember].ToString();  
   
  Combo.Visible=false;  
  Combo.DataSource   =   DataSource;  
  Combo.DisplayMember   =   _DisplayMember;  
  Combo.ValueMember   =   _ValueMember;  
  Combo.DropDownStyle   =   ComboBoxStyle.DropDown;  
  }  
  //根据字符串建立新的下拉列  
  public   DataGridComboBoxColumn(DataTable   DataSource,string   DisplayMember,string   ValueMember)  
  {  
  Combo   =   new   DataGridComboBox();  
  Combo.Visible   =   false;  
  Combo.DataSource   =   DataSource;  
  Combo.DisplayMember   =   DisplayMember;  
  Combo.ValueMember   =   ValueMember;  
  Combo.DropDownStyle   =   ComboBoxStyle.DropDown;  
  }  
  //   重写DataGridColumnStyle中的方法  
  //   重写Abort  
  protected   override   void   Abort(int   RowNum)  
  {  
  System.Diagnostics.Debug.WriteLine("Abort()");  
  RollBack();  
  HideComboBox();  
  EndEdit();  
  }  
  //   重写Commit  
  protected   override   bool   Commit(CurrencyManager   DataSource,int   RowNum)  
  {  
  HideComboBox();  
  if(!InEdit)  
  {  
  return   true;  
  }  
  try  
  {  
  //如果是:   Combo.DropDownStyle   =   ComboBoxStyle.DropDownList;  
  //object   Value   =   Combo.SelectedValue;  
  //如果是:   Combo.DropDownStyle   =   ComboBoxStyle.DropDown;  
  object   Value   =   Combo.Text;  
  if(NullText.Equals(Value))  
  {  
  Value   =   System.Convert.DBNull;    
  }  
  SetColumnValueAtRow(DataSource,   RowNum,   Value);  
  }  
  catch  
  {  
  RollBack();  
  return   false;  
  }  
   
  this.EndEdit();  
  return   true;  
  }  
  //移出焦点  
  protected   override   void   ConcedeFocus()  
  {  
  Combo.Visible=false;  
  }  
  //重写编辑dataGrid的方法edit  
  protected   override   void   Edit(CurrencyManager   Source   ,int   Rownum,Rectangle   Bounds,   bool   ReadOnly,string   InstantText,   bool   CellIsVisible)  
  {  
  Combo.Text   =   string.Empty;  
  Rectangle   OriginalBounds   =   Bounds;  
  OldVal   =   Combo.Text;  
   
  if(CellIsVisible)  
  {  
  Bounds.Offset(xMargin,   yMargin);  
  Bounds.Width   -=   xMargin   *   2;  
  Bounds.Height   -=   yMargin;  
  Combo.Bounds   =   Bounds;  
  Combo.Visible   =   true;  
  }  
  else  
  {  
  Combo.Bounds   =   OriginalBounds;  
  Combo.Visible   =   false;  
  }  
   
  Combo.SelectedValue   =   GetText(GetColumnValueAtRow(Source,   Rownum));  
   
  if(InstantText!=null)  
  {  
  Combo.SelectedValue   =   InstantText;  
  }  
  Combo.RightToLeft   =   this.DataGridTableStyle.DataGrid.RightToLeft;  
  if(InstantText==null)  
  {  
  Combo.SelectAll();  
  }  
  else  
  {  
  int   End   =   Combo.Text.Length;  
  Combo.Select(End,   0);  
  }  
  if(Combo.Visible)  
  {  
  DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);  
  }  
   
  InEdit   =   true;  
  }  
  protected   override   int   GetMinimumHeight()  
  {  
  //   设置combobox的最小高度  
  return   Combo.PreferredHeight   +   yMargin;  
  }  
  protected   override   int   GetPreferredHeight(Graphics   g   ,object   Value)  
  {  
  System.Diagnostics.Debug.WriteLine("GetPreferredHeight()");  
  int   NewLineIndex     =   0;  
  int   NewLines   =   0;  
  string   ValueString   =   this.GetText(Value);  
  do  
  {  
  NewLineIndex   =   ValueString.IndexOf("r\n",   NewLineIndex   +   1);  
  NewLines   +=   1;  
  }while(NewLineIndex   !=   -1);  
  return   FontHeight   *   NewLines   +   yMargin;  
  }  
  protected   override   Size   GetPreferredSize(Graphics   g,   object   Value)  
  {  
  Size   Extents   =   Size.Ceiling(g.MeasureString(GetText(Value),   this.DataGridTableStyle.DataGrid.Font));  
  Extents.Width   +=   xMargin   *   2   +   DataGridTableGridLineWidth   ;  
  Extents.Height   +=   yMargin;  
  return   Extents;  
  }  
  protected   override   void   Paint(Graphics   g,Rectangle   Bounds,CurrencyManager   Source,int   RowNum)  
  {  
  Paint(g,   Bounds,   Source,   RowNum,   false);  
  }  
  protected   override   void   Paint(Graphics   g,Rectangle   Bounds,CurrencyManager   Source,int   RowNum,bool   AlignToRight)  
  {  
  string   Text   =   GetText(GetColumnValueAtRow(Source,   RowNum));  
  PaintText(g,   Bounds,   Text,   AlignToRight);  
  }  
  protected   override   void   Paint(Graphics   g,Rectangle   Bounds,CurrencyManager   Source,int   RowNum,Brush   BackBrush   ,Brush   ForeBrush   ,bool   AlignToRight)  
  {  
  string   Text   =   GetText(GetColumnValueAtRow(Source,   RowNum));  
  PaintText(g,   Bounds,   Text,   BackBrush,   ForeBrush,   AlignToRight);  
  }  
  protected   override   void   SetDataGridInColumn(DataGrid   Value)  
  {  
  base.SetDataGridInColumn(Value);  
  if(Combo.Parent!=Value)  
  {  
  if(Combo.Parent!=null)  
  {  
  Combo.Parent.Controls.Remove(Combo);  
  }  
  }  
  if(Value!=null)    
  {  
  Value.Controls.Add(Combo);  
  }  
  }  
  protected   override   void   UpdateUI(CurrencyManager   Source,int   RowNum,   string   InstantText)  
  {  
  Combo.Text   =   GetText(GetColumnValueAtRow(Source,   RowNum));  
  if(InstantText!=null)  
  {  
  Combo.Text   =   InstantText;  
  }  
  }    
   
  private   int   DataGridTableGridLineWidth  
  {  
  get  
  {  
  if(this.DataGridTableStyle.GridLineStyle   ==   DataGridLineStyle.Solid)    
  {    
  return   1;  
  }  
  else  
  {  
  return   0;  
  }  
  }  
  }  
  public   void   EndEdit()  
  {  
  InEdit   =   false;  
  Invalidate();  
  }  
  private   string   GetText(object   Value)  
  {  
  if(Value==System.DBNull.Value)  
  {  
  return   NullText;  
  }  
  if(Value!=null)  
  {  
  return   Value.ToString();  
  }  
  else  
  {  
  return   string.Empty;  
  }  
  }  
  private   void   HideComboBox()  
  {  
  if(Combo.Focused)  
  {  
  this.DataGridTableStyle.DataGrid.Focus();  
  }  
  Combo.Visible   =   false;  
  }  
  private   void   RollBack()  
  {  
  Combo.Text   =   OldVal;  
  //编辑结束  
  }  
  private   void   PaintText(Graphics   g   ,Rectangle   Bounds,string   Text,bool   AlignToRight)  
  {  
  Brush   BackBrush   =   new   SolidBrush(this.DataGridTableStyle.BackColor);  
  Brush   ForeBrush=   new   SolidBrush(this.DataGridTableStyle.ForeColor);  
  PaintText(g,   Bounds,   Text,   BackBrush,   ForeBrush,   AlignToRight);  
  }  
  private   void   PaintText(Graphics   g   ,   Rectangle   TextBounds,   string   Text,   Brush   BackBrush,Brush   ForeBrush,bool   AlignToRight)  
  {  
  Rectangle   Rect   =   TextBounds;  
  RectangleF   RectF     =   Rect;    
  StringFormat   Format   =   new   StringFormat();  
  if(AlignToRight)  
  {  
  Format.FormatFlags   =   StringFormatFlags.DirectionRightToLeft;  
  }  
  switch(this.Alignment)  
  {  
  case   HorizontalAlignment.Left:  
  Format.Alignment   =   StringAlignment.Near;  
  break;  
  case   HorizontalAlignment.Right:  
  Format.Alignment   =   StringAlignment.Far;  
  break;  
  case   HorizontalAlignment.Center:  
  Format.Alignment   =   StringAlignment.Center;  
  break;  
  }  
  Format.FormatFlags   =Format.FormatFlags;  
  Format.FormatFlags   =StringFormatFlags.NoWrap;  
  g.FillRectangle(BackBrush,   Rect);  
  Rect.Offset(0,   yMargin);  
  Rect.Height   -=   yMargin;  
  g.DrawString(Text,   this.DataGridTableStyle.DataGrid.Font,   ForeBrush,   RectF,   Format);  
  Format.Dispose();  
  }  
  }  
  }Top

相关问题

  • datagrid绑定sql语句出现错误
  • 请教sql语句和datagrid的问题
  • 用什么语句判断checkbox 已经被选中呢
  • 小问题:checkbox语句出错,请帮忙更正
  • datagrid如何绑定sql语句?不用adodc
  • 急求一个SQL语句,填充DataGrid控件
  • 求一sql语句,并用datagrid返回结果
  • 求一sql语句,并用datagrid返回结果
  • 100分请教datagrid模板列写判断语句问题!!!
  • 如何把datagrid的修改转成SQL语句?

关键词

  • datagrid
  • 添加
  • datagridcombobox
  • tablestyle
  • private
  • using system

得分解答快速导航

  • 帖主:jjkkwwww
  • tianxm
  • jerry_qin

相关链接

  • CSDN .NET频道
  • .NET类图书
  • C#类图书
  • .NET类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
世纪乐知(北京)网络技术有限公司 版权所有, 京 ICP 证 020026 号
北京创新乐知广告有限公司 提供技术支持
Copyright © 2000-2007, CSDN.NET, All Rights Reserved
GongshangLogo