datagrid和数据表a读数据,
我在datagrid中加了一列combo,combo是a表的关键字段关联的另外一张B表的对应值。以上我已经做好,但现在datagrid上显示的combo原始状态是null,只有下拉才能出现选项。如何使每行的combo一开始显示B数据表的对应值呢???
我在datagrid中加了一列combo,combo是a表的关键字段关联的另外一张B表的对应值。以上我已经做好,但现在datagrid上显示的combo原始状态是null,只有下拉才能出现选项。如何使每行的combo一开始显示B数据表的对应值呢???
员工号 部门编号
姓名 部门名称
年龄
性别
所属部门编号我把A表除了部门编号外的字段都放在datagrid里,另外在这个datagrid列中加了个combobox列放部门名称,那么怎么在form_load的时候使combobox就显示对应的部门名称呢???
{
public ComboBox DGCombo = new ComboBox();
private Boolean isEditing;
private string _strSelectedText ; public DataGridComboColumn():base()
{
DGCombo.Items.Add("abcddf");
DGCombo.Items.Add("fdsfds");
DGCombo.Items.Add("cccccc");
DGCombo.Visible =false;
} protected override void Abort(int rowNum)
{
isEditing=false;
DGCombo.SelectedValueChanged-=new EventHandler(DGCombo_SelectedValueChanged);
//Invalidate();
} protected override bool Commit(CurrencyManager dataSource, int rowNum)
{
DGCombo.Bounds=Rectangle.Empty;
DGCombo.SelectedValueChanged+=new EventHandler(DGCombo_SelectedValueChanged);
if (isEditing==false)
{return true;}
isEditing=false; try
{
DGCombo.Text =DGCombo.Text;
}
catch
{
DGCombo.Text=String.Empty;
} try
{
string value=_strSelectedText;
SetColumnValueAtRow(dataSource,rowNum,value);
}
catch
{
Abort(rowNum);
return false;
}
Invalidate();
return true;
} protected override void Edit(CurrencyManager source, int rowNum, Rectangle bounds, bool readOnly, string instantText, bool cellIsVisible)
{
string value;
try
{
value=GetColumnValueAtRow(source,rowNum).ToString ();
}
catch
{
SetColumnValueAtRow(source,rowNum,DGCombo.Text);
} value=GetColumnValueAtRow(source,rowNum).ToString (); if (cellIsVisible==true)
{
DGCombo.Bounds =new Rectangle (bounds.X,bounds.Y,bounds.Width ,bounds.Height);
DGCombo.Text =value;
DGCombo.Visible =true;
DGCombo.SelectedValueChanged +=new EventHandler(DGCombo_SelectedValueChanged);
}
else
{
DGCombo.Text =value;
DGCombo.Visible =false;
} if (DGCombo.Visible ==false)
{DataGridTableStyle.DataGrid.Invalidate (bounds);}
} protected override int GetMinimumHeight()
{
return 0;
}
protected override int GetPreferredHeight(Graphics g, object value)
{
return 0;
} protected override Size GetPreferredSize(Graphics g, object value)
{
return new Size ();
} protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum)
{
Paint(g,bounds,source,rowNum,true);
} protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, bool alignToRight)
{
Paint(g,bounds,source,rowNum,System.Drawing.Brushes.Red ,Brushes.Blue,alignToRight);
} protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
{
string strDate;
RectangleF rect= new RectangleF(0,0,0,0);
try
{
strDate=DGCombo.Text ;
strDate=GetColumnValueAtRow(source,rowNum).ToString ();
}
catch
{
SetColumnValueAtRow(source,rowNum,DGCombo.Text);
strDate=GetColumnValueAtRow(source,rowNum).ToString ();
} rect.X=bounds.X;
rect.Y=bounds.Y;
rect.Height =bounds.Height;
rect.Width =bounds.Width ; g.FillRectangle (backBrush,rect);
rect.Offset(0,2);
rect.Height -=2;
g.DrawString (strDate,this.DataGridTableStyle.DataGrid .Font ,foreBrush,rect); } protected override void SetDataGridInColumn(DataGrid value)
{
base.SetDataGridInColumn(value); if (DGCombo.Parent!=null)
{ DGCombo.Parent .Controls .Remove(DGCombo);}
if (value!=null)
{value.Controls.Add (DGCombo);}
} private void DGCombo_SelectedValueChanged(object sender,System.EventArgs e)
{
isEditing=true;
base.ColumnStartedEditing (DGCombo);
_strSelectedText=DGCombo.Text;
if (_strSelectedText==null)
{
_strSelectedText=string.Empty; } } }
如果有的话,combo的selectedValue绑定到所属部门编号就可以了