日期:2014-05-20  浏览次数:20860 次

winform中,怎么用datagrid绑定指定的列啊
我看到属性那里有   tablestyles这个属性,是这里设置的吗?还有,怎么设置每个列的宽度啊?谢谢拉

------解决方案--------------------
1.DataGrid有一属性“TableStyles”,在那里设置就行了,宽度在GridColumnStyles里可以设。
2.如果写代码的话,如下:
string strSql = "select ZLXH,ZLMC,ZLYS from jc_gykz_zlsy where ZLLX_DM= '002 ' and NBBH= ' "+this.NBBH+ " ' and ZLXH not in (select ZLXH from ZDAJ_ZLSY A,ZDAJ_ZAXX B WHERE A.zabh=B.zabh and b.nbbh= ' "+this.NBBH+ " ') ";
dtqtzj = IComDb.ExecuteQuery(strSql);
this.dgQTZJ.DataSource = dtqtzj.DefaultView;

DataGridTableStyle ts1 = new DataGridTableStyle();
ts1.MappingName = dtqtzj.TableName.ToString();

DataGridColumnStyle TextCol = new DataGridTextBoxColumn();
TextCol.MappingName = "ZLXH ";
TextCol.HeaderText = "序号 ";
TextCol.Width = 40;
ts1.GridColumnStyles.Add(TextCol);

DataGridColumnStyle TextCol2 = new DataGridTextBoxColumn();
TextCol2.MappingName = "ZLMC ";
TextCol2.HeaderText = "资料名称 ";
TextCol2.Width = 200;
ts1.GridColumnStyles.Add(TextCol2);

DataGridColumnStyle TextCol3 = new DataGridTextBoxColumn();
TextCol3.MappingName = "ZLYS ";
TextCol3.HeaderText = "页数 ";
TextCol3.Width = 40;
ts1.GridColumnStyles.Add(TextCol3);

this.dgQTZJ.TableStyles.Add(ts1);
------解决方案--------------------
可以在tablestyles--> GridColumnStyles中设置,PerferredWidth单位列宽,MappingName为绑定的字段