简体   繁体   English

datagridview事件导致datagridview无法正确填充

[英]datagridview event causing datagridview to no fill up properly

Both of these functions are working fine, one should fill up datagrid with data, the other, should be triggered when i select a row from the first datagrid. 这两个函数都工作正常,一个应该用数据填充datagrid,另一个应该在我从第一个datagrid中选择一行时触发。 But for some reason the second function is causing datagrid to fill just one row of data. 但是由于某种原因,第二个功能导致datagrid仅填充一行数据。

private void fillDataGrid(DataGridView dg, string query) {
            OracleCommand cmd = new OracleCommand(query);
            cmd.CommandType = CommandType.Text;
            cmd.Connection = conn;
            OracleDataAdapter oda = new OracleDataAdapter(cmd);
            DataTable dt = new DataTable();
            try {
                cmd.ExecuteNonQuery();
                oda.Fill(dt);
                **dg.DataSource = dt;**
            }
            catch(Exception e) {
                MessageBox.Show(e.ToString());
            }
        }

This function is called when i load the form, and it is working fine on its own. 当我加载表单时,将调用此函数,并且它可以正常工作。 But when the next function is causing some kind of problem and i don't know why. 但是当下一个功能引起某种问题时,我不知道为什么。

private void dataGridView1_SelectionChanged_1(object sender, EventArgs e)
        {
            String s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            String upit = "select dn.* from detaljinarudzbe dn, narudzba n where dn.nid = n.nid and n.jmbg = " + s + "";
            fillDataGrid(dataGridView2, upit);
        }

It is working fine, but i get just one row in first datagridview. 它工作正常,但是我在第一个datagridview中只有一行。 And expection says 期望说

system.argument out of range exception:must be non negative and less than the size of collection system.argument超出范围异常:必须为非负且小于集合的大小

Caused by the ** line in first method. 由第一种方法中的**行引起。 I don't get why is the event causing this problem. 我不明白为什么该事件会导致此问题。 when i use the second function on a button it is working fine. 当我在按钮上使用第二个功能时,它工作正常。

You can try (I using SQL Server) : attention check SelectedRows[0] by SelectedRows.Count 您可以尝试(我使用SQL Server):通过SelectedRows.Count注意检查SelectedRows [0]

private void fillDataGrid(DataGridView dg, string query) {
        SqlCommand cmd = new SqlCommand(query);
        cmd.CommandType = CommandType.Text;
        cmd.Connection = conn;

        SqlDataAdapter oda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        try {
            conn.Open();
            cmd.ExecuteNonQuery();
            oda.Fill(dt);
            conn.Close();
            dg.DataSource = dt;
        }
        catch(Exception e) {
            MessageBox.Show(e.ToString());
        }
        finally
        {

        }
    }
private void dataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        if(dataGridView1.SelectedRows.Count > 0)
        {
            String s = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
            String upit = "Select * from CARD_NO where CT_ID = " + s + "";
            fillDataGrid(dataGridView2, upit);
        }

    }
private void Form1_Load(object sender, EventArgs e)
    {
        fillDataGrid(dataGridView1, "Select * from CARD_TYPE");
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM