To achieve this, you can modify the existing code as follows:

  1. Add a new method to fetch the data for all selected items in listBox1:
private DataTable GetDataForSelectedMoNos(ListBox listBox)
{
    DataTable dataTable = new DataTable();
    dataTable.Columns.Add("制令单号");
    dataTable.Columns.Add("成品编号");
    dataTable.Columns.Add("预计开工日期");
    dataTable.Columns.Add("结束日期");
    dataTable.Columns.Add("生产数量");
    dataTable.Columns.Add("缴库数量");
    dataTable.Columns.Add("生产成品");

    string connectionString = "Server=192.168.1.180;initial catalog=DB_2015_20211029;user id=sa;password=gdzy5tgb6yhn;Connect Timeout=5";
    
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        connection.Open();

        string query = "SELECT mo_no as '制令单号', mrp_no as '成品编号', sta_dd as '预计开工日期', end_dd as '结束日期', qty as '生产数量', qty_fin as '缴库数量', (select name from mf_bom where prd_no = mf_mo.mrp_no) as '生产成品' from mf_mo WHERE mo_no = @MoNo";
        SqlCommand command = new SqlCommand(query, connection);

        foreach (var item in listBox.SelectedItems)
        {
            string selectedMoNo = item.ToString();
            command.Parameters.Clear();
            command.Parameters.AddWithValue("@MoNo", selectedMoNo);

            using (SqlDataReader reader = command.ExecuteReader())
            {
                while (reader.Read())
                {
                    DataRow row = dataTable.NewRow();
                    row["制令单号"] = reader.GetString(0);
                    row["成品编号"] = reader.GetString(1);
                    row["预计开工日期"] = reader.GetDateTime(2);
                    row["结束日期"] = reader.GetDateTime(3);
                    row["生产数量"] = reader.GetInt32(4);
                    row["缴库数量"] = reader.GetInt32(5);
                    row["生产成品"] = reader.GetString(6);

                    dataTable.Rows.Add(row);
                }
            }
        }
    }

    return dataTable;
}
  1. Modify the cboDept_SelectedIndexChanged and dtpkanBan_ValueChanged event handlers to retrieve the data for all selected items in listBox1 and populate dataGridView1:
private void cboDept_SelectedIndexChanged(object sender, EventArgs e)
{
    // Existing code

    DataTable dataTable = GetDataForSelectedMoNos(listBox1);
    dataGridView1.DataSource = dataTable;
}

private void dtpkanBan_ValueChanged(object sender, EventArgs e)
{
    // Existing code

    DataTable dataTable = GetDataForSelectedMoNos(listBox1);
    dataGridView1.DataSource = dataTable;
}

By calling the GetDataForSelectedMoNos method, you can fetch the data for all selected items in listBox1 and populate the dataGridView1 with the retrieved data

private void dtpkanBan_ValueChangedobject sender EventArgs e DateTime selectedDate = dtpKanBanValue; DateTime selectedDate = dtpKanBanValueDate; 只获取日期部分 strin

原文地址: https://www.cveoy.top/t/topic/iKMY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录