03的代码,然后用在05上就出现这样的错误了,是因为什么呢?如何修改?
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;namespace 进销存管理系统
{
public class DataGridComboBox : ComboBox
{
public DataGridComboBox()
{
}
private void InitializeComponent()
{ } }
public class DataGridComboBoxColumn : DataGridColumnStyle
{
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();
Combo.Visible = false;
Combo.DataSource = DataSource;
Combo.DisplayMember = DisplayMember;
Combo.ValueMember = ValueMember;
Combo.DropDownStyle = ComboBoxStyle.DropDown;
}
protected override void Abort(int rowNum)
{
throw new System.Exception("The method or operation is not implemented.");
System.Diagnostics.Debug.WriteLine("Abort()");
RollBack();
HideComboBox();
EndEdit();
}
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
HideComboBox();
if (!InEdit)
{
return true;
}
try
{
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;
}
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()
{
return Combo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g, object value)
{
System.Diagnostics.Debug.WriteLine("GetPreferredHeight()");
int NewLinIndex = 0;
int NewLines = 0;
string ValueString = this.GetText(value);
do
{
NewLinIndex = ValueString.IndexOf("r\n", NewLinIndex + 1);
NewLines += 1;
} while (NewLinIndex != -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 SetDataGrid(DataGrid value)
{
base.SetDataGridInColumn(value);
if (Combo.Parent != null)
{
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();
}
}
}
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using System.Data;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;namespace 进销存管理系统
{
public class DataGridComboBox : ComboBox
{
public DataGridComboBox()
{
}
private void InitializeComponent()
{ } }
public class DataGridComboBoxColumn : DataGridColumnStyle
{
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();
Combo.Visible = false;
Combo.DataSource = DataSource;
Combo.DisplayMember = DisplayMember;
Combo.ValueMember = ValueMember;
Combo.DropDownStyle = ComboBoxStyle.DropDown;
}
protected override void Abort(int rowNum)
{
throw new System.Exception("The method or operation is not implemented.");
System.Diagnostics.Debug.WriteLine("Abort()");
RollBack();
HideComboBox();
EndEdit();
}
protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
HideComboBox();
if (!InEdit)
{
return true;
}
try
{
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;
}
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()
{
return Combo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g, object value)
{
System.Diagnostics.Debug.WriteLine("GetPreferredHeight()");
int NewLinIndex = 0;
int NewLines = 0;
string ValueString = this.GetText(value);
do
{
NewLinIndex = ValueString.IndexOf("r\n", NewLinIndex + 1);
NewLines += 1;
} while (NewLinIndex != -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 SetDataGrid(DataGrid value)
{
base.SetDataGridInColumn(value);
if (Combo.Parent != null)
{
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();
}
}
}
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货