简体   繁体   中英

Display chart according to datagridview line using Visual C# 2010

I have a form with a read only datagrid view. As the user moves the cursor up and down the datagrid view lines I would like to display a graph that is related to the highlighted line. I tried to use DataGridView1_SelectionChanged event, but it never gets executed.

dataGridView1_CellContentClick_1 does the trick, but requires the user to click which I would like to avoid.

public partial class conf_results : Form
{
    private DataSet ds = new DataSet();
    private DataTable dt = new DataTable();
    private NpgsqlDataAdapter da = new NpgsqlDataAdapter();
    private NpgsqlCommandBuilder sBuilder = new NpgsqlCommandBuilder();
    public conf_results()
    {
        InitializeComponent();
        try
        {
            // PostgeSQL-style connection string
            // Making connection with Npgsql provider
            NpgsqlConnection conn;
            conn = new NpgsqlConnection(Properties.Settings.Default.connString);
            conn.Open();
            string sql = "SELECT m.orig_code,m.sejtvonal,round_dbl(m.parm_b,2),round_dbl(m.parm_c,2),round_dbl(m.variance,2),round_dbl(100 /(2^(m.ic50 - 1)),2),m.toxic,m.meres_id, " +
                "d.sejtvonal,round_dbl(d.parm_b,2),round_dbl(d.parm_c,2),round_dbl(d.variance,2),round_dbl(100 /(2^(d.ic50 - 1)),2),d.toxic,d.meres_id " +
                "from vegyulet_curve m, vegyulet_curve d where m.assay_id=d.assay_id and m.orig_code=d.orig_code "+
                "and m.sejtvonal='Mes-Sa' and d.sejtvonal='Dx5'";
            da = new NpgsqlDataAdapter(sql, conn);
            sBuilder = new NpgsqlCommandBuilder(da);
            DataSet ds = new DataSet();
            // filling DataSet with result from NpgsqlDataAdapter
            //da.Fill(ds);
            da.Fill(ds, "vegyulet_curve");
            // since it C# DataSet can handle multiple tables, we will select first
            dt = ds.Tables["vegyulet_curve"];
            // connect grid to DataTable
            dataGridView1.DataSource = ds.Tables["vegyulet_curve"];

             conn.Close();
                        }
        catch (Exception msg)
        {
            // something went wrong, and you wanna know why
            MessageBox.Show(msg.ToString());
            throw;
        }
    }

    private void DataGridView1_SelectionChanged(object sender, EventArgs e)
    {
        int i = dataGridView1.SelectedRows[0].Index;;    
        //  I am rewriting the code to use the chart on the form, 
        //  I was debugging, but I could ot get the control
    }

    private void dataGridView1_CellContentClick_1(object sender, DataGridViewCellEventArgs e)
    {
        if (e.RowIndex > -1)
        {
            //detailForm f = new detailForm(dataGridView1.Rows[e.RowIndex].Cells[11].Value.ToString(),
            //dataGridView1.Rows[e.RowIndex].Cells[12].Value.ToString(),
            //dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString());
            //this.AddOwnedForm(f);
            //f.ShowDialog();                
            grafikon f = new grafikon(Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[7].Value.ToString()),
                dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString(),
                Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[6].Value.ToString()),
                Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[14].Value.ToString()),
                Convert.ToBoolean(dataGridView1.Rows[e.RowIndex].Cells[13].Value.ToString()));
            this.AddOwnedForm(f);
            f.ShowDialog();
        }

    }

You can use the event CellMouseEnter to avoid having the user click the cell/row.

But take note, that the event will be invoked for each cell and might cause some performance issue if the user moves the cursor horizontally. What you can do is declare a global variable to hold the current row index, and first check when the event is invoked to see if the row changed or not.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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