datagrid设成可编辑,删除,添加。
当单击带*的新行,如果点的是我重载风格的列,单元格可以显示comboboxBm,但comboboxBm不响应事件。如果点的是其他列,并输入值,回出现下一新行,这时再点我重载的列,才能正常操作。
10 个解决方案
#1
你都没给代码出来?
#2
是不是你的comboboxbm的直关联到部门的ID?这样的话,如果是新加的行,因为id为空,所以就不能编辑了,你可以事先给他一个初始值。
#3
我就是wwwb:
public class DataGridColumnComboStyle : DataGridColumnStyle
{
private int xMargin = 1;
private int yMargin = 1;
private ComboBox controlBmCombo;
private object OldVal;
private bool InEdit= false;
public DataGridColumnComboStyle()
{
controlBmCombo = new ComboBox();
controlBmCombo.Items.Add("软件开发部");
controlBmCombo.Items.Add("硬件开发部");
controlBmCombo.Visible=false;
}
protected override void Abort(int RowNum)
{
RollBack();
HideComboBox();
EndEdit();
MessageBox.Show("操作已取消.");
}
protected override bool Commit(CurrencyManager DataSource,int RowNum)
{
Console.WriteLine("commit"+" "+RowNum.ToString()+" "+InEdit.ToString()+" "+OldVal.ToString());
HideComboBox();
if(InEdit)
{
Console.WriteLine("write");
try
{
object Value=controlBmCombo.SelectedValue;
SetColumnValueAtRow(DataSource,RowNum,Value);
}
catch (Exception e1)
{
MessageBox.Show(e1.Message+"\n操作将回滚.");
RollBack();
return false;
}
this.EndEdit();
return true;
}
return true;
}
protected override void ConcedeFocus()
{
// EndEdit();
controlBmCombo.Visible=false;
}
protected override void Edit(CurrencyManager Source ,int Rownum,Rectangle Bounds, bool ReadOnly,string InstantText, bool CellIsVisible)
{
Rectangle OriginalBounds=Bounds;
if(CellIsVisible)
{
OldVal=GetColumnValueAtRow(Source,Rownum);
if (OldVal!=System.DBNull.Value)
controlBmCombo.SelectedValue=OldVal;
else
controlBmCombo.SelectAll();
Bounds.Offset(xMargin, yMargin);
Bounds.Width -= xMargin * 2;
Bounds.Height -= yMargin;
controlBmCombo.Bounds = Bounds;
controlBmCombo.Visible = true;
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);
InEdit=true;
Console.WriteLine("InEdit=true");
}
}
protected override int GetMinimumHeight()
{
return controlBmCombo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g ,object Value)
{
int NewLineIndex = 0;
int NewLines = 0;
string ValueString =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(controlBmCombo.Parent!=Value)
{
if(controlBmCombo.Parent!=null)
{
controlBmCombo.Parent.Controls.Remove(controlBmCombo);
}
}
if(Value!=null)
{
Value.Controls.Add(controlBmCombo);
}
}
protected override void UpdateUI(CurrencyManager Source,int RowNum, string InstantText)
{
// try
// {
// controlBmCombo.Text = GetText(controlBmCombo.GetBmmcFromBmbh(Convert.ToInt32(GetColumnValueAtRow(Source, RowNum))));
// }
// catch (Exception e1)
// {
// MessageBox.Show("updateui,"+e1.Message);
// }
// if(InstantText!=null)
// {
// controlBmCombo.Text = InstantText;
// }
}
private int DataGridTableGridLineWidth
{
get
{
if(this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)
{
return 1;
}
else
{
return 0;
}
}
}
public void EndEdit()
{
InEdit = false;
Invalidate();
Console.WriteLine("InEdit=false");
}
private void RollBack()
{
if (OldVal==System.DBNull.Value)
controlBmCombo.Text="";
else
controlBmCombo.SelectedValue=OldVal;
EndEdit();
}
private string GetText(object Value)
{
if(Value==System.DBNull.Value)
{
return NullText;
}
else
{
return controlBmCombo.SelectedText;
}
}
private void HideComboBox()
{
// if(controlBmCombo.comboBox1.Focused)
// {
// this.DataGridTableStyle.DataGrid.Focus();
// }
controlBmCombo.Visible = false;
}
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.X,RectF.Y+2,Format);
Format.Dispose();
}
public class DataGridColumnComboStyle : DataGridColumnStyle
{
private int xMargin = 1;
private int yMargin = 1;
private ComboBox controlBmCombo;
private object OldVal;
private bool InEdit= false;
public DataGridColumnComboStyle()
{
controlBmCombo = new ComboBox();
controlBmCombo.Items.Add("软件开发部");
controlBmCombo.Items.Add("硬件开发部");
controlBmCombo.Visible=false;
}
protected override void Abort(int RowNum)
{
RollBack();
HideComboBox();
EndEdit();
MessageBox.Show("操作已取消.");
}
protected override bool Commit(CurrencyManager DataSource,int RowNum)
{
Console.WriteLine("commit"+" "+RowNum.ToString()+" "+InEdit.ToString()+" "+OldVal.ToString());
HideComboBox();
if(InEdit)
{
Console.WriteLine("write");
try
{
object Value=controlBmCombo.SelectedValue;
SetColumnValueAtRow(DataSource,RowNum,Value);
}
catch (Exception e1)
{
MessageBox.Show(e1.Message+"\n操作将回滚.");
RollBack();
return false;
}
this.EndEdit();
return true;
}
return true;
}
protected override void ConcedeFocus()
{
// EndEdit();
controlBmCombo.Visible=false;
}
protected override void Edit(CurrencyManager Source ,int Rownum,Rectangle Bounds, bool ReadOnly,string InstantText, bool CellIsVisible)
{
Rectangle OriginalBounds=Bounds;
if(CellIsVisible)
{
OldVal=GetColumnValueAtRow(Source,Rownum);
if (OldVal!=System.DBNull.Value)
controlBmCombo.SelectedValue=OldVal;
else
controlBmCombo.SelectAll();
Bounds.Offset(xMargin, yMargin);
Bounds.Width -= xMargin * 2;
Bounds.Height -= yMargin;
controlBmCombo.Bounds = Bounds;
controlBmCombo.Visible = true;
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);
InEdit=true;
Console.WriteLine("InEdit=true");
}
}
protected override int GetMinimumHeight()
{
return controlBmCombo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g ,object Value)
{
int NewLineIndex = 0;
int NewLines = 0;
string ValueString =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(controlBmCombo.Parent!=Value)
{
if(controlBmCombo.Parent!=null)
{
controlBmCombo.Parent.Controls.Remove(controlBmCombo);
}
}
if(Value!=null)
{
Value.Controls.Add(controlBmCombo);
}
}
protected override void UpdateUI(CurrencyManager Source,int RowNum, string InstantText)
{
// try
// {
// controlBmCombo.Text = GetText(controlBmCombo.GetBmmcFromBmbh(Convert.ToInt32(GetColumnValueAtRow(Source, RowNum))));
// }
// catch (Exception e1)
// {
// MessageBox.Show("updateui,"+e1.Message);
// }
// if(InstantText!=null)
// {
// controlBmCombo.Text = InstantText;
// }
}
private int DataGridTableGridLineWidth
{
get
{
if(this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)
{
return 1;
}
else
{
return 0;
}
}
}
public void EndEdit()
{
InEdit = false;
Invalidate();
Console.WriteLine("InEdit=false");
}
private void RollBack()
{
if (OldVal==System.DBNull.Value)
controlBmCombo.Text="";
else
controlBmCombo.SelectedValue=OldVal;
EndEdit();
}
private string GetText(object Value)
{
if(Value==System.DBNull.Value)
{
return NullText;
}
else
{
return controlBmCombo.SelectedText;
}
}
private void HideComboBox()
{
// if(controlBmCombo.comboBox1.Focused)
// {
// this.DataGridTableStyle.DataGrid.Focus();
// }
controlBmCombo.Visible = false;
}
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.X,RectF.Y+2,Format);
Format.Dispose();
}
#4
damekuler(damekuler) 用我的名字发的,分数照给,请放心。
#5
是空就不能编辑吗?可是先点其他字段再点它就可以,这时候也是空啊?你说的不是system.dbnull.value吗?
#6
我们用的方法不一样,我现在没有时间,所以没有好好研究这个问题。
我遇到的问题就是这样解决(我们的实现方法不同)的。
我遇到的问题就是这样解决(我们的实现方法不同)的。
#7
我也是在datagrid中选择部门(显示部门名称,读写的是编号),能说一下你的方法吗?
如果可能请发分原码damekuler@sohu.com damekuler@msn.com
如果可能请发分原码damekuler@sohu.com damekuler@msn.com
#8
為什么要重新寫一個DataGridColumnComboStyle 呢﹐用
Combox cb=new Combox;
DataGridColumnStyle.Controls.Add(cb);
設置好cb在.Dock屬性﹐直接操作cb就可以了。
Combox cb=new Combox;
DataGridColumnStyle.Controls.Add(cb);
設置好cb在.Dock屬性﹐直接操作cb就可以了。
#9
因为是一个部门树,我已写了这么一个combboxbm。在很的部门操作中重用。
#10
其实我的方法和bigtree21cn一样,但是我感觉效果并不理想!
后来我就采用了我以前在VB中的方案:用一个控件另外的控件结合DataGrid实现,就是需要自己写很多代码,但是自己感觉效果还可以。
后来我就采用了我以前在VB中的方案:用一个控件另外的控件结合DataGrid实现,就是需要自己写很多代码,但是自己感觉效果还可以。
#1
你都没给代码出来?
#2
是不是你的comboboxbm的直关联到部门的ID?这样的话,如果是新加的行,因为id为空,所以就不能编辑了,你可以事先给他一个初始值。
#3
我就是wwwb:
public class DataGridColumnComboStyle : DataGridColumnStyle
{
private int xMargin = 1;
private int yMargin = 1;
private ComboBox controlBmCombo;
private object OldVal;
private bool InEdit= false;
public DataGridColumnComboStyle()
{
controlBmCombo = new ComboBox();
controlBmCombo.Items.Add("软件开发部");
controlBmCombo.Items.Add("硬件开发部");
controlBmCombo.Visible=false;
}
protected override void Abort(int RowNum)
{
RollBack();
HideComboBox();
EndEdit();
MessageBox.Show("操作已取消.");
}
protected override bool Commit(CurrencyManager DataSource,int RowNum)
{
Console.WriteLine("commit"+" "+RowNum.ToString()+" "+InEdit.ToString()+" "+OldVal.ToString());
HideComboBox();
if(InEdit)
{
Console.WriteLine("write");
try
{
object Value=controlBmCombo.SelectedValue;
SetColumnValueAtRow(DataSource,RowNum,Value);
}
catch (Exception e1)
{
MessageBox.Show(e1.Message+"\n操作将回滚.");
RollBack();
return false;
}
this.EndEdit();
return true;
}
return true;
}
protected override void ConcedeFocus()
{
// EndEdit();
controlBmCombo.Visible=false;
}
protected override void Edit(CurrencyManager Source ,int Rownum,Rectangle Bounds, bool ReadOnly,string InstantText, bool CellIsVisible)
{
Rectangle OriginalBounds=Bounds;
if(CellIsVisible)
{
OldVal=GetColumnValueAtRow(Source,Rownum);
if (OldVal!=System.DBNull.Value)
controlBmCombo.SelectedValue=OldVal;
else
controlBmCombo.SelectAll();
Bounds.Offset(xMargin, yMargin);
Bounds.Width -= xMargin * 2;
Bounds.Height -= yMargin;
controlBmCombo.Bounds = Bounds;
controlBmCombo.Visible = true;
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);
InEdit=true;
Console.WriteLine("InEdit=true");
}
}
protected override int GetMinimumHeight()
{
return controlBmCombo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g ,object Value)
{
int NewLineIndex = 0;
int NewLines = 0;
string ValueString =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(controlBmCombo.Parent!=Value)
{
if(controlBmCombo.Parent!=null)
{
controlBmCombo.Parent.Controls.Remove(controlBmCombo);
}
}
if(Value!=null)
{
Value.Controls.Add(controlBmCombo);
}
}
protected override void UpdateUI(CurrencyManager Source,int RowNum, string InstantText)
{
// try
// {
// controlBmCombo.Text = GetText(controlBmCombo.GetBmmcFromBmbh(Convert.ToInt32(GetColumnValueAtRow(Source, RowNum))));
// }
// catch (Exception e1)
// {
// MessageBox.Show("updateui,"+e1.Message);
// }
// if(InstantText!=null)
// {
// controlBmCombo.Text = InstantText;
// }
}
private int DataGridTableGridLineWidth
{
get
{
if(this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)
{
return 1;
}
else
{
return 0;
}
}
}
public void EndEdit()
{
InEdit = false;
Invalidate();
Console.WriteLine("InEdit=false");
}
private void RollBack()
{
if (OldVal==System.DBNull.Value)
controlBmCombo.Text="";
else
controlBmCombo.SelectedValue=OldVal;
EndEdit();
}
private string GetText(object Value)
{
if(Value==System.DBNull.Value)
{
return NullText;
}
else
{
return controlBmCombo.SelectedText;
}
}
private void HideComboBox()
{
// if(controlBmCombo.comboBox1.Focused)
// {
// this.DataGridTableStyle.DataGrid.Focus();
// }
controlBmCombo.Visible = false;
}
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.X,RectF.Y+2,Format);
Format.Dispose();
}
public class DataGridColumnComboStyle : DataGridColumnStyle
{
private int xMargin = 1;
private int yMargin = 1;
private ComboBox controlBmCombo;
private object OldVal;
private bool InEdit= false;
public DataGridColumnComboStyle()
{
controlBmCombo = new ComboBox();
controlBmCombo.Items.Add("软件开发部");
controlBmCombo.Items.Add("硬件开发部");
controlBmCombo.Visible=false;
}
protected override void Abort(int RowNum)
{
RollBack();
HideComboBox();
EndEdit();
MessageBox.Show("操作已取消.");
}
protected override bool Commit(CurrencyManager DataSource,int RowNum)
{
Console.WriteLine("commit"+" "+RowNum.ToString()+" "+InEdit.ToString()+" "+OldVal.ToString());
HideComboBox();
if(InEdit)
{
Console.WriteLine("write");
try
{
object Value=controlBmCombo.SelectedValue;
SetColumnValueAtRow(DataSource,RowNum,Value);
}
catch (Exception e1)
{
MessageBox.Show(e1.Message+"\n操作将回滚.");
RollBack();
return false;
}
this.EndEdit();
return true;
}
return true;
}
protected override void ConcedeFocus()
{
// EndEdit();
controlBmCombo.Visible=false;
}
protected override void Edit(CurrencyManager Source ,int Rownum,Rectangle Bounds, bool ReadOnly,string InstantText, bool CellIsVisible)
{
Rectangle OriginalBounds=Bounds;
if(CellIsVisible)
{
OldVal=GetColumnValueAtRow(Source,Rownum);
if (OldVal!=System.DBNull.Value)
controlBmCombo.SelectedValue=OldVal;
else
controlBmCombo.SelectAll();
Bounds.Offset(xMargin, yMargin);
Bounds.Width -= xMargin * 2;
Bounds.Height -= yMargin;
controlBmCombo.Bounds = Bounds;
controlBmCombo.Visible = true;
DataGridTableStyle.DataGrid.Invalidate(OriginalBounds);
InEdit=true;
Console.WriteLine("InEdit=true");
}
}
protected override int GetMinimumHeight()
{
return controlBmCombo.PreferredHeight + yMargin;
}
protected override int GetPreferredHeight(Graphics g ,object Value)
{
int NewLineIndex = 0;
int NewLines = 0;
string ValueString =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(controlBmCombo.Parent!=Value)
{
if(controlBmCombo.Parent!=null)
{
controlBmCombo.Parent.Controls.Remove(controlBmCombo);
}
}
if(Value!=null)
{
Value.Controls.Add(controlBmCombo);
}
}
protected override void UpdateUI(CurrencyManager Source,int RowNum, string InstantText)
{
// try
// {
// controlBmCombo.Text = GetText(controlBmCombo.GetBmmcFromBmbh(Convert.ToInt32(GetColumnValueAtRow(Source, RowNum))));
// }
// catch (Exception e1)
// {
// MessageBox.Show("updateui,"+e1.Message);
// }
// if(InstantText!=null)
// {
// controlBmCombo.Text = InstantText;
// }
}
private int DataGridTableGridLineWidth
{
get
{
if(this.DataGridTableStyle.GridLineStyle == DataGridLineStyle.Solid)
{
return 1;
}
else
{
return 0;
}
}
}
public void EndEdit()
{
InEdit = false;
Invalidate();
Console.WriteLine("InEdit=false");
}
private void RollBack()
{
if (OldVal==System.DBNull.Value)
controlBmCombo.Text="";
else
controlBmCombo.SelectedValue=OldVal;
EndEdit();
}
private string GetText(object Value)
{
if(Value==System.DBNull.Value)
{
return NullText;
}
else
{
return controlBmCombo.SelectedText;
}
}
private void HideComboBox()
{
// if(controlBmCombo.comboBox1.Focused)
// {
// this.DataGridTableStyle.DataGrid.Focus();
// }
controlBmCombo.Visible = false;
}
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.X,RectF.Y+2,Format);
Format.Dispose();
}
#4
damekuler(damekuler) 用我的名字发的,分数照给,请放心。
#5
是空就不能编辑吗?可是先点其他字段再点它就可以,这时候也是空啊?你说的不是system.dbnull.value吗?
#6
我们用的方法不一样,我现在没有时间,所以没有好好研究这个问题。
我遇到的问题就是这样解决(我们的实现方法不同)的。
我遇到的问题就是这样解决(我们的实现方法不同)的。
#7
我也是在datagrid中选择部门(显示部门名称,读写的是编号),能说一下你的方法吗?
如果可能请发分原码damekuler@sohu.com damekuler@msn.com
如果可能请发分原码damekuler@sohu.com damekuler@msn.com
#8
為什么要重新寫一個DataGridColumnComboStyle 呢﹐用
Combox cb=new Combox;
DataGridColumnStyle.Controls.Add(cb);
設置好cb在.Dock屬性﹐直接操作cb就可以了。
Combox cb=new Combox;
DataGridColumnStyle.Controls.Add(cb);
設置好cb在.Dock屬性﹐直接操作cb就可以了。
#9
因为是一个部门树,我已写了这么一个combboxbm。在很的部门操作中重用。
#10
其实我的方法和bigtree21cn一样,但是我感觉效果并不理想!
后来我就采用了我以前在VB中的方案:用一个控件另外的控件结合DataGrid实现,就是需要自己写很多代码,但是自己感觉效果还可以。
后来我就采用了我以前在VB中的方案:用一个控件另外的控件结合DataGrid实现,就是需要自己写很多代码,但是自己感觉效果还可以。