C# - 使用空值填充网格

时间:2017-06-20 20:28:01

标签: c# sql database

我正在尝试使用 Access 数据库中的值填充网格,但没有成功。

当我点击网格中的按钮时,我得到null值。哪个是问题?有人可以给我一个想法/建议吗?

我的代码是:

private void btnComenzi_Click(object sender, EventArgs e)
    {
        //string owner = cboAgenti.SelectedValue.ToString();
        string AgentAsig = "";
        //string Data = "";
        string Client = "";
        //int Client = 0;
        //int NrCom = 0;
        string NrCom = "";
        double ValCom = 0;
        double Discount = 0;
        double ValFinala = 0;
        string Status = "";
        double Volum = 0;
        DateTime data = new DateTime();

        OleDbConnection conDataBase = RealizeazaConexiune();
        //select
        OleDbCommand selectCommand = new OleDbCommand();
        selectCommand.Connection = conDataBase;
        string query1 = "SELECT Cod_Client, Cod_Comanda, Valoare_Comanda, Discount_Comanda, Valoare_Finala, Owner, Status_Comanda, Data_Comanda FROM Comenzi WHERE Owner = '" + cboAgenti.SelectedValue + "'";
        selectCommand.CommandText = query1;
       // OleDbDataReader myReader1 = selectCommand.ExecuteReader();

       using (OleDbDataReader myReader1 = selectCommand.ExecuteReader())
        {
            while (myReader1 != null && myReader1.Read())
            {
                // Data = myReader1[0].ToString(); Client = myReader1[1].ToString();
                Client = myReader1[0].ToString();
                NrCom = myReader1[1].ToString();
                ValCom = double.Parse(myReader1[2].ToString());
                Discount = double.Parse(myReader1[3].ToString());
                ValFinala = double.Parse(myReader1[4].ToString());
                AgentAsig = myReader1[5].ToString();
                Status = myReader1[6].ToString();
                data = DateTime.Parse(myReader1[7].ToString());
                Volum = Volum + ValFinala;
            }
        } 

        DataRow dr1 = dt.NewRow();
       // dr1[0] = Data;
        dr1[0] = Client;
        dr1[1] = NrCom;
        dr1[2] = ValCom;
        dr1[3] = Discount;
        dr1[4] = ValFinala;
        dr1[5] = AgentAsig;
        dr1[6] = Status;
        dr1[7] = data;
        dt.Rows.Add(dr1);
        dgvComenzi.DataSource = dt;
    }

1 个答案:

答案 0 :(得分:0)

谢谢! 我用代码移动了一些代码,最后,它的工作原理。我没有成功使用刷新网格,但我一直在努力。