private void Form1_Load(object sender, EventArgs e)
{
SqlConnection objcon = new SqlConnection("server=LENOVOT220;uid=sa;pwd=314;database=JxhSystem"); objcon.Open(); string sqltr = "select (case when xrate is not null then xrate else 0 end ) as xrate,cc=cast(case when stime is null then 0 when stime = 0 then 0 else sdtimep * 1.0/ stime*100 end as decimal(18,2)),(case when sdtimep is not null then sdtimep else 0 end) as sdtimep,cdepartname,invcode,ccuscode,csocode,qty,rqdate,mocode,prdatee,prqty,status,(case when ttime is not null then ttime else 0 end) as ttime,(case when stime is not null then stime else 0 end) as stime,prqty6,prqty7,prqty1,prqty2,prqty3,prqty4,prqty5,re from Jx_momorderp where(sdtime<=stime or sdtime<>stime or stime is null or sdtime is null) order by cdepartname,status desc"; SqlDataAdapter objpter = new SqlDataAdapter(sqltr, objcon); DataSet objset = new DataSet(); objpter.Fill(objset, "ds"); dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = objset; objcon.Close(); }
dataGridView1.DataSource = objset;
后面就是加不上dataGridView1.DatBind();上面的绑定代码该怎么写?谢谢大虾们
{
SqlConnection objcon = new SqlConnection("server=LENOVOT220;uid=sa;pwd=314;database=JxhSystem"); objcon.Open(); string sqltr = "select (case when xrate is not null then xrate else 0 end ) as xrate,cc=cast(case when stime is null then 0 when stime = 0 then 0 else sdtimep * 1.0/ stime*100 end as decimal(18,2)),(case when sdtimep is not null then sdtimep else 0 end) as sdtimep,cdepartname,invcode,ccuscode,csocode,qty,rqdate,mocode,prdatee,prqty,status,(case when ttime is not null then ttime else 0 end) as ttime,(case when stime is not null then stime else 0 end) as stime,prqty6,prqty7,prqty1,prqty2,prqty3,prqty4,prqty5,re from Jx_momorderp where(sdtime<=stime or sdtime<>stime or stime is null or sdtime is null) order by cdepartname,status desc"; SqlDataAdapter objpter = new SqlDataAdapter(sqltr, objcon); DataSet objset = new DataSet(); objpter.Fill(objset, "ds"); dataGridView1.AutoGenerateColumns = true; dataGridView1.DataSource = objset; objcon.Close(); }
dataGridView1.DataSource = objset;
后面就是加不上dataGridView1.DatBind();上面的绑定代码该怎么写?谢谢大虾们
已经绑定了
DataTable dt = new DataTable();
dt = objset.Tables[0];dataGirdView1.DataSource=dt;
dataGirdView1.DataSource=objset.Tables["ds"];
DataTable dt = objset.Tables[0];
for (int i = 0; i < dt.Rows.Count; i++)
{
//用一个对象数组保存数据表dt各字段的内容
object[] obj = new object[] {dt.Rows[i]["Field1"],
dt.Rows[i]["Field2"],
dt.Rows[i]["Field3"],
......
dt.Rows[i]["FieldN"]};
//循环添加所有的数据行
DataGridView.Rows.Add(obj);
}
datagridview.Columns[1].PropertyName=objset.Tables[0].Columns[1].Name;
datagridview.Columns[2].PropertyName=objset.Tables[0].Columns[2].Name;分别对应下去就好,注意不能对应错了