DataGridView表中,如何获得某行的内容,并赋值给textbox里。

img


实现该表格的代码如下:

    string connect_str = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=d:\\db.mdb";
    OleDbConnection thisConnection = new OleDbConnection(connect_str);      
    string sql = "select * from txt ";       
    OleDbDataAdapter thisAdapter = new OleDbDataAdapter(sql, thisConnection);        
    DataSet thisDataSet = new System.Data.DataSet();     
    thisAdapter.Fill(thisDataSet, "table");       
    DataTable dt = thisDataSet.Tables["table"];   
    dataGridView1.DataSource = dt;        
    thisConnection.Close();

咨询一下:表格中的文字内容,鼠标双击某一行,就获得文字内容,再赋给textbox.text 里。
感谢积极回复,望能详细些。

只需要添加 CellDoubleClick 方法即可,DataGridViewCellEventArgs 回调这里有点击单元格的列和行信息:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1[e.ColumnIndex, e.RowIndex].Value.ToString();
        }

以下是类似的运行截图:

img

相关代码我已经提交在:gitcode,如果还有问题可前往下载运行和查看。
https://gitcode.net/marin1993/questions7882891

在dataGridView的CellDoubleClick事件中,e.RowIndex和e.ColumnIndex就是行号和列号
dataGridView[e.ColumnIndex, e.RowIndex].Value也可以
或者dataGridView.Rows[e.ColumnIndex].Cells[e.RowIndex].Value也行
或者如果你的dataGridView绑定了DataSource,可以直接访问它绑定的DataTable

object a = dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].EditedFormattedValue;

感谢大家积极回复。问题已解决了。

不知道你这个问题是否已经解决, 如果还没有解决的话:

如果你已经解决了该问题, 非常希望你能够分享一下解决方案, 写成博客, 将相关链接放在评论区, 以帮助更多的人 ^-^