日期:2009-11-21 浏览次数:20460 次
最近windows自带的grid写了一个程序,觉得大家可能用得上,就写出来.已经在visual studio2005上测试通过
1、动态邦定数据库
IClientManager client = Hsf.Container.SpringHelper.GetObject("ShuiniManageClient") as IClientManager;
IList datas;
datas = client.FindByAddress(lstAddress.SelectedProvinceId, lstAddress.SelectedCityId, lstAddress.SelectedCountyId);//得到数据
System.Data.DataTable dt = DataUtils.Ienumerable2Datatable(datas, typeof(Client));
DataControlFieldCollection df = grdStandGrid.Columns;//数据绑定控件
df.Clear();
BoundField tx;
//加入选择按钮
ButtonField comselect = new ButtonField();
comselect.Text = "选择";
comselect.CommandName = "Select";
comselect.ButtonType = ButtonType.Button;
grdStandGrid.Columns.Insert(0, comselect);
//删除按钮
ButtonField comdel = new ButtonField();
comdel.CommandName = "Del";//切记不能将名字写成delete,如果是这样,系统会让你使用rowdeleteing事件,修改也一样(不能写成update)
comdel.Text = "删除";
comdel.ButtonType = ButtonType.Button;
grdStandGrid.Columns.Insert(1, comdel);
//修改按钮
ButtonField comupdate = new ButtonField();
comupdate.CommandName = "Updatecom";
comupdate.Text = "修改";
comupdate.ButtonType = ButtonType.Button;
grdStandGrid.Columns.Insert(2, comupdate);
grdStandGrid.DataKeyNames = new string[] { Client.ID,Client.UNITPRESENTATION };//我这里记了两个我需要的值。
tx = new BoundField();
tx.HeaderText = "代码";
tx.DataField = Client.CODE;
tx.HeaderStyle.Width = 100;
df.Add(tx);
tx = new BoundField();
tx.HeaderText = "名称";
tx.DataField = Client.NAME;
tx.HeaderStyle.Width = 200;