直接用一句sql語句搞定private void button9_Click(object sender, System.EventArgs e) { string source=@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\North.mdb"; string strsql="select a.id,a.name,b.item,b.num,c.store from ta a left join tb b on a.id=b.id left join tb c on a.id=c.id"; OleDbDataAdapter da=new OleDbDataAdapter(strsql,source); DataSet ds=new DataSet(); da.Fill(ds,"tab"); dataGrid1.SetDataBinding(ds,"tab");}
wzcnet,sql出来的,不能用视图构造,因为这些sql出来的都是随意挑选的
两种方法可供参考(1)用视图非常简单的 (2)你可以看一下我的以下代码,自创的我起的名字叫,邦定空视图法 private System.Data.DataView GetNullDV()///////DataGrid绑定空视图的技巧 { string Sql="Select 'ID' as WY,LYMC,LYBH,'ZTS'as ZTS,'DS'as DS,'YD'as YD,'RG' as RG,'QY'as QY,'BL'as BL From XMGL_LYXX Where DEL_FLAG='0'and XMID='0' order by ID"; System.Data.SqlClient.SqlConnection Cn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]); System.Data.SqlClient.SqlDataAdapter DA=new SqlDataAdapter(Sql,Cn); System.Data.DataSet DS=new DataSet(); DA.Fill(DS,"XMGL_LYXX");
{
string source=@"Provider=Microsoft.Jet.OLEDB.4.0; Data Source=e:\North.mdb";
string strsql="select a.id,a.name,b.item,b.num,c.store from ta a left join tb b on a.id=b.id left join tb c on a.id=c.id"; OleDbDataAdapter da=new OleDbDataAdapter(strsql,source);
DataSet ds=new DataSet();
da.Fill(ds,"tab");
dataGrid1.SetDataBinding(ds,"tab");}
(2)你可以看一下我的以下代码,自创的我起的名字叫,邦定空视图法
private System.Data.DataView GetNullDV()///////DataGrid绑定空视图的技巧
{
string Sql="Select 'ID' as WY,LYMC,LYBH,'ZTS'as ZTS,'DS'as DS,'YD'as YD,'RG' as RG,'QY'as QY,'BL'as BL From XMGL_LYXX Where DEL_FLAG='0'and XMID='0' order by ID";
System.Data.SqlClient.SqlConnection Cn=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnectionString"]);
System.Data.SqlClient.SqlDataAdapter DA=new SqlDataAdapter(Sql,Cn);
System.Data.DataSet DS=new DataSet();
DA.Fill(DS,"XMGL_LYXX");
System.Data.DataView DV;
DV=new DataView(DS.Tables["XMGL_LYXX"]);
return DV;
}
private void AddViewDR()//////向绑定了空视图的DataGrid添加一行数据
{
System.Data.DataView DV=GetNullDV();
System.Data.DataTable DT=new DataTable();
DT=DV.Table;
if(GetLYDS().Tables["XMGL_LYXX"].Rows.Count>0)
{
for(int i=0;i<GetLYDS().Tables["XMGL_LYXX"].Rows.Count;i++)
{
System.Data.DataRow DR=DT.NewRow();
DR[0]=GetLYDS().Tables["XMGL_LYXX"].Rows[i]["ID"].ToString();
DR[1]=GetLYDS().Tables["XMGL_LYXX"].Rows[i]["LYMC"].ToString();
DR[2]=GetLYDS().Tables["XMGL_LYXX"].Rows[i]["LYBH"].ToString(); DR[3]= GetZTS(i);
DR[4]=(int.Parse(GetZTS(i))-int.Parse(GetZTXX(i,"预定"))-int.Parse(GetZTXX(i,"认购"))-int.Parse(GetZTXX(i,"签约"))-int.Parse(GetZTXX(i,"保留"))).ToString();
DR[5]=GetZTXX(i,"预定");
DR[6]=GetZTXX(i,"认购");
DR[7]=GetZTXX(i,"签约");
DR[8]=GetZTXX(i,"保留");
DT.Rows.Add(DR);
}
DataGrid1.DataSource=DT.DefaultView;
DataGrid1.DataBind();
DataGrid1.Items[0].Cells[0].RowSpan=DataGrid1.Items.Count;
for(int i=1;i<DataGrid1.Items.Count;++i)
{
DataGrid1.Items[i].Cells[0].Visible=false;
}
for(int i=0;i<DataGrid1.Items.Count;++i)
{
System.Web.UI.WebControls.ListItem ss=new ListItem("",i.ToString());
((RadioButtonList)DataGrid1.Items[0].Cells[0].Controls[1]).Items.Add(ss);
}
}
else
{
this.Response.Write("<script Language=javascript> window.alert('没有记录!!')</script>");
}
}
換成
string conStr="provider=sqloledb;uid=aaa;pwd=12345;initial catalog=hdh";