如题....详细:我在一窗体上添加了两个dataGridView控件,我想在我点击dataGridView1时,dataGridView2的也同时选中与dataGridView1相同的单元格,或是行,或是列...麻烦你们了.

解决方案 »

  1.   

    在DataGridView1的SelectedIndexChanged事件中,遍历DataGridView2查找相同的Cell,然后设置DataGridView2的SelectedIndex。
      

  2.   

            private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
            {
                dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true; //单元格
                //dataGridView2.Rows[e.RowIndex].Selected = true;         //行
                //dataGridView2.Columns[e.ColumnIndex].Selected = true;   //列
            }
      

  3.   


    dataGridView控件是没有SelectedIndexChange事件.汗...我给我的代码给你们看看:按逻辑我的代码没错.但是运行的时候,就出现一些不理想的效果.如:我随便选中了dataGridView1中的某一个单元格,而这时dataGridView2中的只能选中和dataGridView1的列索引相同的单元格.不过我第一次选中dataGridView1的时候,dataGridView2却可以正常的选择与dataGridView1相同的单元格.代码如下:
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.Data.SqlClient;namespace test.TestDataGridView
    {
        public partial class TestDataGridView : Form
        {
            public TestDataGridView()
            {
                InitializeComponent();
                loadDataSource();
                this.dataGridView1.SelectionMode = DataGridViewSelectionMode.CellSelect;
                this.dataGridView1.Columns.Clear();
                this.dataGridView2.Columns.Clear();
                this.dataGridView1.DataSource = ds.Tables[0];
                this.dataGridView2.DataSource = ds.Tables[0];
            }        private string con_str = @"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=ShoesManager;Data Source=JAVELIN\SQLEXPRESS";
            private string select_str = "select * from Goods";
            private DataSet ds;
            private SqlConnection con;
            private SqlDataAdapter dataDapter;
            int colindex = 0;
            int rowindex = 0;        private void loadDataSource()
            {
                con = new SqlConnection(con_str);
                dataDapter = new SqlDataAdapter(select_str, con);
                ds = new DataSet();
                dataDapter.Fill(ds);
            }        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
                this.dataGridView2[colindex, rowindex].Selected = false;
                this.dataGridView2[e.ColumnIndex, e.RowIndex].Selected = true;
                colindex = e.ColumnIndex;
                rowindex = e.RowIndex;
            }
        }
    }
      

  4.   


    该朋友的可以实现.不过还有一个问题.就是dataGridView2中的,始选择项,一直存在,不会消失.
      

  5.   

    加一句:
    dataGridView2.CurrentCell = dataGridView2.Rows[e.RowIndex].Cells[e.ColumnIndex];