日期:2014-05-18  浏览次数:20722 次

dataGird 读取数据问题
string sql11="select depart.departname,employee.姓名";
for(int i=0; i<dsxm.Tables["gzxm"].Rows.Count;i++)
{
xm=dsxm.Tables["gzxm"].Rows[i]["id"].ToString().Trim();
  xmname=dsxm.Tables["gzxm"].Rows[i]["name"].ToString().Trim();
sql11+=","+xm+" as "+xmname+"";
}
sql11+=" from gzb,employee,depart where depart.departid=gzb.s_bm_id and employee.编号=gzb.s_ry_id and s_year='"+this.comboBox1.Text+"' and s_month='"+this.comboBox2.Text+"' and zt_id='"+zt+"' ";
if(dbCon==null) dbCon = MainForm.getConnection();
dbAdapter = new OleDbDataAdapter(sql11, dbCon);
dbAdapter.TableMappings.Add("Table","gzb");
ds=new DataSet("gzb");
ds.Clear();
dbAdapter.Fill(ds);

dataGrid1.TableStyles.Clear();  
DataGridTableStyle myGridTableStyle = new DataGridTableStyle();
myGridTableStyle.MappingName="gzb";
DataGridTextBoxColumn mycol=new DataGridTextBoxColumn();
mycol.MappingName="姓名";
mycol.HeaderText="姓名"; mycol.ReadOnly=true;

myGridTableStyle.GridColumnStyles.Add(mycol);


DataGridTextBoxColumn mycol1=new DataGridTextBoxColumn();
mycol1.MappingName="departname";
mycol1.HeaderText="部门";
mycol1.ReadOnly=true;
myGridTableStyle.GridColumnStyles.Add(mycol1);
dataGrid1.TableStyles.Add(myGridTableStyle);
以上红色部分在dataGrid1中能显示出来,问题是怎么把sql11+=","+xm+" as "+xmname+""; 中的列也读取出来加到dataGrid1中?




------解决方案--------------------
给dsxm添加列,然后把sql11+=","+xm+" as "+xmname+""; 上的数据读取出来绑定到添加列上!