求教高手,C#WINFORM编程时,怎么将dataGrid中选中的一条数据显示到richTextBox中?
也就是说,选中dataGrid中的任一行中的一列,在RICHtextBOX中将这一行的信息显示出来。
如:
列名 A  B   C
     a  b   c
     d  e   f
当选中b时,在richTextBox显示如下信息:
A:a
B: b
C: c
怎么实现呢,高手帮帮忙咯,急用!

解决方案 »

  1.   

    dataGridView?tryif (dataGridView1.SelectedCells.Count > 0)
    {
        int index = dataGridView1.SelectedCells[0].RowIndex;
        for (int i = 0; i < dataGridView1.ColumnCount; i++)
        {
            richTextBox1.Text += dataGridView1.Columns[i].Name + ":" + dataGridView1[i, index].Value + "\n";
        }
    }
      

  2.   

    哦,dataGrid,最近晕的很,我再弄个dataGrid试下,应该也差不多
      

  3.   

    -_-#,以前没用过datagrid,它的功能还真弱,只能从DataSource上入手,这样int index = dataGrid1.CurrentRowIndex;
    DataTable dt = (DataTable)dataGrid1.DataSource;for(int i=0;i<dt.Columns.Count;i++)
    {
        richTextBox1.Text += dt.Columns[i].ColumnName + ":" + ndt.Rows[index][i] + "\n";
    }注意一下它绑定的数据源是什么,这里假定它为DataTable
      

  4.   

    把你绑定数据源的代码贴一下另外你想在会么情况下显示,选中一个单元格就显示吗PS:上面有一个地方复制代码的时候dt没有改richTextBox1.Text += dt.Columns[i].ColumnName + ":" + dt.Rows[index][i] + "\n";
      

  5.   

    程序运行没出错,但是实现不了。我想做的是选中任意一个单元格就显示。
    下面是我的代码:
    1、绑定数据源
    private void myShowData(string strTable)
    {
    this.label4.Text=strTable;
    string sql="select * from " + strTable;
    SqlConnection conn=new SqlConnection("server=;database=电子机械零件设计手册数据库;uid=sa;pwd=;");
    SqlCommand com=new SqlCommand(sql,conn);
    conn.Open();
    SqlDataAdapter a1=new SqlDataAdapter();
    a1.SelectCommand=com;
    SqlCommandBuilder b1=new SqlCommandBuilder(a1);
    DataSet ds = new DataSet();
    a1.Fill(ds,strTable);
    dg.SetDataBinding(ds,strTable);
    DataTable  dt  =  ds.Tables["strTable"];   
    conn.Close();
    }
    2、下面是我根据你的程序写的那个事件代码
    private void dg_Click(object sender, System.EventArgs e)
    {
    int index = this.dg.CurrentRowIndex;
    if(index < 0)
    return;
    DataRow row = (this.dg.DataSource as DataTable).Rows[index]; 
    for(int i=0;i<row.ItemArray.Length;i++)
    {
    this.richTextBox1.Text += "" + (this.dg.DataSource as DataTable).Columns[i].ColumnName+ ":" + row.ItemArray[i] + "\n";
    } }
      

  6.   

    代码不是写在Click事件里,写在CurrentCellChanged事件里
    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {}如果只显示一行数据,在这一事件最前面加一行
    richTextBox1.Clear();
      

  7.   

    private void dg_CurrentCellChanged(object sender, System.EventArgs e)
    {
    richTextBox1.Clear();
    int index = this.dg.CurrentRowIndex;
    if(index < 0)
    return;
    DataRow row = (this.dg.DataSource as DataTable).Rows[index]; 
    for(int j=0;j<row.ItemArray.Length;j++)
    {
    this.richTextBox1.Text += "" + (this.dg.DataSource as DataTable).Columns[j].ColumnName+ ":" + row.ItemArray[j] + "\n";
    }

    string sql="select * from "+ this.label4.Text;
    SqlConnection conn=new SqlConnection("server=;database=电子机械零件设计手册数据库;uid=sa;pwd=;");
    SqlCommand com=new SqlCommand(sql,conn);
    conn.Open();
    SqlDataReader dr = com.ExecuteReader();
    DataTable dt=new DataTable("table"); conn.Close();
    }
    选中单元格时,程序突然中断,错误信息:未处理的“System.NullReferenceException”类型的异常出现在 零件设计手册5.25.exe 中。其他信息: 未将对象引用设置到对象的实例。怎么回事,是那个DataTable的问题么?
      

  8.   

    try...catch一下看看错误信息另外
    string sql="select * from "+ this.label4.Text;
    SqlConnection conn=new SqlConnection("server=;database=电子机械零件设计手册数据库;uid=sa;pwd=;");
    SqlCommand com=new SqlCommand(sql,conn);
    conn.Open();
    SqlDataReader dr = com.ExecuteReader();
    DataTable dt=new DataTable("table");conn.Close();这是做什么的,写了一大串代码最后什么都没做
      

  9.   

    lxcnn(过客) 谢谢你了,终于搞定了。分已送上。再次谢谢了。