C# 如何保存datagridview中的选择

C# 如何保存datagridview中的选择,c#,winforms,datagridview,C#,Winforms,Datagridview,我在datagridview中使用cell_click事件,这样无论我选择什么,都会显示在this.spncodelabel.Text label和this.form3picbox.Image pictureBox等中,所有这些都可以正常工作,但我的问题是每次选择记录时,选择模式都会返回到第一行。如何将所选内容保留到该特定行或我所选的任何记录,以显示我已从该特定行中选择 下面是我的datagridview单元格点击事件代码 这显然是因为dataGridView1.DataSource=dt;。您

我在datagridview中使用cell_click事件,这样无论我选择什么,都会显示在this.spncodelabel.Text label和this.form3picbox.Image pictureBox等中,所有这些都可以正常工作,但我的问题是每次选择记录时,选择模式都会返回到第一行。如何将所选内容保留到该特定行或我所选的任何记录,以显示我已从该特定行中选择

下面是我的datagridview单元格点击事件代码


这显然是因为dataGridView1.DataSource=dt;。您不需要在每个单元格单击事件中填充数据表。只需在表单加载或其他加载数据的地方填写数据表。显然,我得到的索引超出范围错误在哪一行有索引超出范围?对不起,我的不好…非常感谢!!!关于你的问题,你可以看看我的另一份报告。希望对您有所帮助:
   private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {

            OleDbCommand command = new OleDbCommand();
            command.Connection = conDB;
            string query = "select CCSpn_CODE,CCLname,CCFname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage from abaanaCC";
           CCSpn_CODE,CCFname,CCLname,CCMname,CCDOB,CCgender,CCSchool,CaClass,CCVillage,CCSiblings,CCGuardian,CCContact,CCImage from abaanaCC where CCSpn_CODE = '" + txtspn_codesch.Text + "'";

                command.CommandText = query;
                OleDbDataAdapter da = new OleDbDataAdapter(command);
                DataTable dt = new DataTable();
                da.Fill(dt);

                dataGridView1.DataSource = dt;
                if (e.RowIndex < 0 || e.ColumnIndex < 0)
                    return;
                this.spncodelabel.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCSpn_CODE"].Value.ToString();
                byte[] mydata = (byte[])dt.Rows[e.RowIndex]["CCImage"];
                MemoryStream stream = new MemoryStream(mydata);
                this.form3picbox.Image = Image.FromStream(stream);
                this.lblfname.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCFname"].Value.ToString();
                this.lbllastname.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCLname"].Value.ToString();
                this.lblguardian.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCGuardian"].Value.ToString();
                this.lblcontact.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCContact"].Value.ToString();
                this.lblschool.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCSchool"].Value.ToString();
                this.lblvillage.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCVillage"].Value.ToString();
                this.lblgender.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCgender"].Value.ToString();
                this.doblbl.Text = this.dataGridView1.Rows[e.RowIndex].Cells["CCDOB"].Value.ToString();                        

       }