0

我有一个带有只读数据网格视图的表单。当用户在数据网格视图线上下移动光标时,我想显示一个与突出显示的线相关的图形。我尝试使用 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();
        }

    }
4

1 回答 1

0

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

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

于 2013-10-02T11:55:45.023 回答