110,534
社区成员
发帖
与我相关
我的任务
分享
/// <summary>
/// 从数据库查询数据展现到datagridview里
/// </summary>
/// <param name="Sql"></param>
private void select(String Sql)
{
this.dataGridView1.Rows.Clear();
List<SysPlannedProduction> IPim = new List<SysPlannedProduction>();
string connstr = ConfigurationManager.ConnectionStrings["SQLConnString"].ConnectionString;
SqlConnection con = new SqlConnection(connstr);
con.Open();
SqlDataAdapter dtp = new SqlDataAdapter(Sql, con);
try
{
DataTable dt = new DataTable();
dtp.Fill(dt);
foreach (var item in dt.Rows)
{
SysPlannedProduction model = new SysPlannedProduction();
//计划编号 1
model.plannedId = ((DataRow)item)["plannedld"].ToString();
//产品编号 2
model.productId = ((DataRow)item)["productId"].ToString();
//产品名称3
model.productName = ((DataRow)item)["productName"].ToString();
//型号 4
model.productModelNumber = ((DataRow)item)["ProductModel"].ToString();
//状态 6
model.productionstate = ((DataRow)item)["State"].ToString();
//开始日期 7
model.startDate = ((DataRow)item)["startDate"].ToString();
////计划完成日期 8
model.planDate = ((DataRow)item)["planDate"].ToString();
//计划数量 9
model.planNumber = ((DataRow)item)["planNumber"].ToString();
//当前完成数量 10
model.completionOfQuantity = ((DataRow)item)["completionOfQuantity"].ToString();
//完成比例 11
model.completionRatio = ((DataRow)item)["completeRatio"].ToString();
//剩余数量 12
model.surplusNumber = ((DataRow)item)["surplusNumber"].ToString();
//备注13
model.remarks = ((DataRow)item)["Note"].ToString();
//详情
model.details = ((DataRow)item)["Details"].ToString();
IPim.Add(model);
}
}
catch
{
con.Close();
}
foreach (var item in IPim)
{
int index = this.dataGridView1.Rows.Add();
//计划编号
this.dataGridView1.Rows[index].Cells[0].Value = Convert.ToString(item.plannedId);
//产品编号
this.dataGridView1.Rows[index].Cells[1].Value = Convert.ToString(item.productId);
//产品名称
this.dataGridView1.Rows[index].Cells[2].Value = Convert.ToString(item.productName);
//型号
this.dataGridView1.Rows[index].Cells[3].Value = Convert.ToString(item.productModelNumber);
//详情
this.dataGridView1.Rows[index].Cells[4].Value = Convert.ToString(item.details);
//状态
this.dataGridView1.Rows[index].Cells[5].Value = Convert.ToString(item.productionstate);
//开始日期
this.dataGridView1.Rows[index].Cells[6].Value = Convert.ToString(item.startDate);
//计划完成日期
this.dataGridView1.Rows[index].Cells[7].Value = Convert.ToString(item.planDate);
//计划数量
this.dataGridView1.Rows[index].Cells[8].Value = Convert.ToString(item.planNumber);
//当前完成数量
this.dataGridView1.Rows[index].Cells[9].Value = Convert.ToString(item.completionOfQuantity);
//完成比例
this.dataGridView1.Rows[index].Cells[10].Value = Convert.ToString(item.completionRatio);
//剩余数量
this.dataGridView1.Rows[index].Cells[11].Value = Convert.ToString(item.surplusNumber);
//备注
this.dataGridView1.Rows[index].Cells[12].Value = Convert.ToString(item.remarks);
}
}
if (checkBox_DataTime.Checked)//日期checkBox_选中
{
String m_date;
//日期combox控件中有两项 为开始时间,计划完成时间
if (comboBox1.SelectedIndex == 0)
{
m_date = "startDate";//开始时间
}
else
{
m_date = "planDate";//计划完成时间
}
String m_startTime = dateTimePicker1.Text.ToString();// 获取dateTimePicker1时间,
String m_endTime = dateTimePicker2.Text.ToString();//获取dateTimePicker2时间
if (m_bool)
{
Sql += " and " + m_date + ">" + "'" + m_startTime + "'" + " and " + m_date + "<" + "'" + m_endTime + "'";
}
else
{
//查询时间在dateTimePicker1和dateTimePicker2日期间隔中的数据
Sql = "select top 10 * from SysPlannedProduction where " + m_date + ">" + "'" + m_startTime + "'" + " and " + m_date + " <" + "'" + m_endTime + "'";
m_bool = true;
}
}