To modify the code to query all items instead of the selected items and display the result in a DataGridView, you can make the following changes:

  1. Change the foreach loop to iterate over all items in the ListBox:
foreach (var item in listBox.Items)
  1. Remove the check for selected items and the selectedMoNo variable since we are querying all items:
// Remove the following lines
// foreach (var item in listBox.SelectedItems)
// {
//     string selectedMoNo = item.ToString();
//     ...
// }
  1. Modify the query to remove the WHERE clause:
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";
  1. Modify the while loop to read all rows from the SqlDataReader:
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);
}
  1. After the foreach loop, bind the DataTable to the DataGridView:
dataGridView1.DataSource = dataTable;

Here's the modified code:

private void GetDataForAllMoNos(DataGridView dataGridView1)
{
    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";
        SqlCommand command = new SqlCommand(query, connection);

        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);
            }
        }
    }

    dataGridView1.DataSource = dataTable;
}

You can call this method with the DataGridView as an argument to retrieve the data and display it in the DataGridView:

GetDataForAllMoNos(dataGridView1);
``
private DataTable GetDataForSelectedMoNosListBox listBox DataTable dataTable = new DataTable; dataTableColumnsAdd制令单号; dataTableColumnsAdd成品编号; dataTableColumnsAdd预计开工日期; dataTableColum

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

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