使用 Visual C# 2010 根据数据网格视图线显示图表

本文关键字:视图 网格 显示图 数据网 数据 Visual 2010 使用 | 更新日期: 2023-09-27 18:33:38

我有一个带有只读数据网格视图的表单。当用户在数据网格视图行上下移动光标时,我希望显示与突出显示的线条相关的图形。我尝试使用DataGridView1_SelectionChanged事件,但它从未被执行。

dataGridView1_CellContentClick_1可以解决问题,但需要用户单击我想避免的。

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

使用 Visual C# 2010 根据数据网格视图线显示图表

您可以使用事件 CellMouseEnter 来避免用户单击单元格/行。

但请注意,将为每个单元格调用该事件,如果用户水平移动光标,可能会导致一些性能问题。您可以做的是声明一个全局变量来保存当前行索引,并首先检查何时调用事件以查看行是否更改。