求教高手,C#WINFORM编程时,怎么将dataGrid中选中的一条数据显示到richTextBox中?
也就是说,选中dataGrid中的任一行中的一列,在RICHtextBOX中将这一行的信息显示出来。
如:
列名 A B C
a b c
d e f
当选中b时,在richTextBox显示如下信息:
A:a
B: b
C: c
怎么实现呢,高手帮帮忙咯,急用!
也就是说,选中dataGrid中的任一行中的一列,在RICHtextBOX中将这一行的信息显示出来。
如:
列名 A B C
a b c
d e f
当选中b时,在richTextBox显示如下信息:
A:a
B: b
C: c
怎么实现呢,高手帮帮忙咯,急用!
解决方案 »
- 对于WCF的几个问题
- udp获取每一个客户端的IP地址
- 送分,求个c# winform的简单问题
- 哪位能好心提供下呢?《C# COM+编程指南》中文版电子书!
- ComboBox,TextBox,DataGird复合控件的问题?牛哥们,帮帮忙
- 在TabPageControl中点右键的时候,如何判断鼠标在哪一个Page上面?
- .Net开发环境中如何实现Image的缩放?谢谢...
- 500分,简单的reader问题!
- 我有一个C++写的DLL,能够提供类似.net各类控件的外观,在C#里能够引用它吗?
- 关于数据库中图形的存储与显示的问题
- 面试一家CMMI5公司终试时候遇到的题目,疑惑
- 请教关于复合自定义控件的事件含义
{
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";
}
}
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
下面是我的代码:
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";
} }
private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
{}如果只显示一行数据,在这一事件最前面加一行
richTextBox1.Clear();
{
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的问题么?
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();这是做什么的,写了一大串代码最后什么都没做