public void BindData() { SqlConn conn = new SqlConn(); conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from v_info_gl order by info_code,u_code",conn.connstr); ds = new DataSet(); sda.Fill(ds,"info_gl"); DataGrid1.DataSource = ds.Tables["info_gl"].DefaultView; DataGrid1.DataKeyField = "info_code";//用于配合后面的DataGrid1.DataKeys[e.Item.ItemIndex].ToString(); DataGrid1.DataBind(); conn.Close(); }
这么说吧,SqlDateAdapter的Fill方法的第二个参数myadp.Fill(ds,"abcd"); 这个abcd和你的那三个表跟本没有任何关系,也就是说名字随便起,因为ds被填充后是和数据库完全脱离的,datamember就可以是abcd,abcd中保存的就是你从3个表中用SELECT xx,xx,xx FROM INNER JOIN xx ON........... 查询出来的全部数据.
或许在Select 中就将需要显示的列都写出来也可以,我没试过
{
SqlConn conn = new SqlConn();
conn.Open();
SqlDataAdapter sda = new SqlDataAdapter("select * from v_info_gl order by info_code,u_code",conn.connstr);
ds = new DataSet();
sda.Fill(ds,"info_gl");
DataGrid1.DataSource = ds.Tables["info_gl"].DefaultView;
DataGrid1.DataKeyField = "info_code";//用于配合后面的DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
DataGrid1.DataBind();
conn.Close();
}
我有CTabCustomerList,CTabProductDetail,CTabAllBusiness,CTabRelate四个表,分别对应于客户信息、产品的信息、每一条货物记录的集合、每一单货物记录 四个内容.
我想通过组合查询,给定产品编号、客户名、发货时间或定单号中的任意一条或几条,都可以查询出包含了该产品的所有定单
private void button1_Click(object sender, System.EventArgs e)
{
int CompanyValue,ProductValue,SendNo;
//combobox1中对应的是客户名,CompanyValue得到对应的客户ID编号
CompanyValue=(int)Convert.ChangeType(comboBox1.SelectedValue,typeof(int));
//comboxBox2对应为产品名,ProductValue对应产品ID编号
ProductValue=(int)Convert.ChangeType(comboBox2.SelectedValue,typeof(int));
QueryParam="SELECT CTabRelate.SendNoticeNo,CTabCustomerList.CustomerName,CTabRelate.TotalPrice,CTabRelate.PayoffDate FROM CTabRelate INNER JOIN CTabCustomerList ON CTabRelate.ReceiveCompany = CTabCustomerList.No CROSS JOIN CTabAllBusiness WHERE ";//查询checkbox的状态,确定给出哪些条件,组合成查询语句
if (checkBox1.Checked==true)
{
if (checkBox2.Checked==true || checkBox3.Checked==true || checkBox4.Checked==true)
QueryParam=QueryParam+"CTabRelate.ReceiveCompany="+CompanyValue+" AND ";
else
QueryParam=QueryParam+"CTabRelate.ReceiveCompany="+CompanyValue;
} if (checkBox2.Checked==true)
{
if (checkBox3.Checked==true || checkBox4.Checked==true )
QueryParam+="CTabRelate.SendNoticeNo=CTabAllBusiness.SendNoticeNo AND CTabAllBusiness.ProductNo="+ProductValue+" AND ";
else
QueryParam+="CTabRelate.SendNoticeNo=CTabAllBusiness.SendNoticeNo AND CTabAllBusiness.ProductNo="+ProductValue;
}
if (checkBox4.Checked==true)
{
SendNo=(int)Convert.ChangeType(textBox1.Text,typeof(int));
if (checkBox3.Checked==true)
{
QueryParam+="CTabRelate.SendNoticeNo="+SendNo+" AND ";
}
else
QueryParam+="CTabRelate.SendNoticeNo="+SendNo;
}
if (checkBox3.Checked==true)
QueryParam+="CTabRelate.PayoffDate BETWEEN "+dateTimePicker1.Value.Date+" AND "+dateTimePicker2.Value.Date; dsQuerySend1.Clear();
System.Data.OleDb.OleDbCommand QueryCmd;
System.Data.OleDb.OleDbConnection QueryConnect= new System.Data.OleDb.OleDbConnection("Integrated Security=SSPI;Packet Size=4096;Data Source=FISCHSUPPE;Tag with column collation when possible=False;Provider='SQLOLEDB.1';Use Procedure for Prepare=1;Auto Translate=True;Persist Security Info=False;Workstation ID=FISCHSUPPE;Use Encryption for Data=False");
QueryCmd=new System.Data.OleDb.OleDbCommand(QueryParam,QueryConnect);
oleDbDataAdapter3.SelectCommand=QueryCmd;
oleDbDataAdapter3.Fill(dsQuerySend1);
}
}最后形成的SELECT语句类似于
SELECT CTabRelate.SendNoticeNo,CTabCustomerList.CustomerName,CTabRelate.TotalPrice,CTabRelate.PayoffDate FROM CTabRelate INNER JOIN CTabCustomerList ON CTabRelate.ReceiveCompany = CTabCustomerList.No CROSS JOIN CTabAllBusiness WHERE CTabRelate.ReceiveCompany=1 AND CTabRelate.ProductNo=1这样的语句..问题是在,DataSet与DataGrid绑定的时候. DataGrid的datasource可以选dsQuerySend,但是datamember只能选CTabRelate,CTabAllBusiness 这些表,而不能选我select的具体字段.
请问是哪有问题吗
2,如果dataset里面就一个表,binding dataset就行了,它会自动找到那个表的。
楼主不要使用使用强类型,DataGrid也不要指定列的映射,显示出来就是所有的数据。