dataGridView问题 查询出的数据显示在dataGridView上,我想点击dataGridView某一行,能让这一行的数据显示在界面的对应的几个textBox控件上。 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 你可以将ID绑定到一个隐藏的控件里面,然后在SelectedIndexChanging事件里面, GridViewRow row = GridView1.Rows[e.NewSelectedIndex];//获得选中的行 string id = ((HtmlInputHidden)row.FindControl("hiddenID")).Value.ToString();//hiddenID就是棒定ID的控件,这里就把主键的值获得了然后从数据库里面查询出来就OK了.还有一个办法,因为dataGridView显示在浏览器端是TABLE的形式,所以你可以用DOM这样去取得table里面的数据用JS填到控件里面,不过那样代码麻烦一点. this.textBox1.Text = this.dataGridView1[0, this.dataGridView1.CurrentCell.RowIndex].Value.ToString();0 也可以是列名"列名" 在DataGridView的SelectionChanged处理private void OnSelectionChanged(object sender, EventArgs e){ int index = dgv.CurrentCell.RowIndex; DataGridViewRow row = dgv.Rows[index]; textBoxName.Text = row.Cells["name"].Value.ToString() //以下类似......} 1) 使用BindingSource绑定数据源;2)DataGridView 和 TextBox 均绑定到 BindingSource 上此时,数据显示将同步 biningSource.DataSource=dt;DataGriView.DataSource=bindngSource;text.DataBinding.Add("Text",bindingSource."ColumnName");这样就行了 在GridView的RowDataBound事件中增加一个onMouseDown事件,然后在客户端通过JS即可读取table当前行鼠标所点击的行的数据,然后就可以赋值给几个TextBox控件了。 protected void Gridview1_SelectedIndexChanging(object sender, GridViewSelectEventArgs e) { txt1.Text= Gridview1.Rows[e.RowIndex].Cells[0].Text; txt2.Text = Gridview1.Rows[e.RowIndex].Cells[1].Text;//如果是用的BoundBind //.... //如果里面放的是TextBox txt1.Text = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[0].FindControl ("txtName")).Text;//txtName里面放的控件名称 txt2.Text = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[1].FindControl("txtAge")).Text; ("txtName")).Text;//txtName里面放的控件名称 } using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;namespace WindowsApplication6{ public partial class Form1 : Form { public Form1() { InitializeComponent(); DataTable table = new DataTable(); DataColumn c1 = new DataColumn("ID",typeof(int)); table.Columns.Add(c1); DataColumn c2 = new DataColumn("name", typeof(string)); table.Columns.Add(c2); DataRow r1 = table.NewRow(); r1["ID"] = 1; r1["name"] = "abc"; table.Rows.Add(r1); DataRow r2 = table.NewRow(); r2["ID"] = 2; r2["name"] = "bcd"; table.Rows.Add(r2); this.dataGridView1.DataSource = table; } private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e) { int rowIndex = e.RowIndex; this.textBox1.Text = this.dataGridView1.Rows[rowIndex].Cells["ID"].Value.ToString(); this.textBox2.Text = this.dataGridView1.Rows[rowIndex].Cells["name"].Value.ToString(); } }}我试了一下,可以实现功能 vs2010下 如何建立像vs2008 中的那样report项目文件夹? 各位大哥帮帮忙,最后78分了 在線等待,水晶報表字段參數傳遞問題 求一个快速识别印刷体数字的方案 关于datagrid高性价比问题!!!!!!!! 获得程序路径问题! 多表查询问题 在线等 来看看嘛~~~ 刚买了个短信猫,怎么在超级终端测试? 在线等待... 如何用C#来做Shell Extenstion 分析网页的内容,除正则表达之外还有其他什么方法? 字符串赋值 条形码打印程序
GridViewRow row = GridView1.Rows[e.NewSelectedIndex];//获得选中的行
string id = ((HtmlInputHidden)row.FindControl("hiddenID")).Value.ToString();//hiddenID就是棒定ID的控件,这里就把主键的值获得了
然后从数据库里面查询出来就OK了.还有一个办法,因为dataGridView显示在浏览器端是TABLE的形式,所以你可以用DOM这样去取得table里面的数据用JS填到控件里面,不过那样代码麻烦一点.
0 也可以是列名"列名"
{
int index = dgv.CurrentCell.RowIndex;
DataGridViewRow row = dgv.Rows[index];
textBoxName.Text = row.Cells["name"].Value.ToString()
//以下类似......
}
2)DataGridView 和 TextBox 均绑定到 BindingSource 上
此时,数据显示将同步
DataGriView.DataSource=bindngSource;
text.DataBinding.Add("Text",bindingSource."ColumnName");
这样就行了
{
txt1.Text= Gridview1.Rows[e.RowIndex].Cells[0].Text;
txt2.Text = Gridview1.Rows[e.RowIndex].Cells[1].Text;//如果是用的BoundBind
//....
//如果里面放的是TextBox
txt1.Text = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[0].FindControl ("txtName")).Text;//txtName里面放的控件名称
txt2.Text = ((TextBox)Gridview1.Rows[e.RowIndex].Cells[1].FindControl("txtAge")).Text;
("txtName")).Text;//txtName里面放的控件名称
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;namespace WindowsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
DataTable table = new DataTable();
DataColumn c1 = new DataColumn("ID",typeof(int));
table.Columns.Add(c1);
DataColumn c2 = new DataColumn("name", typeof(string));
table.Columns.Add(c2); DataRow r1 = table.NewRow();
r1["ID"] = 1;
r1["name"] = "abc";
table.Rows.Add(r1);
DataRow r2 = table.NewRow();
r2["ID"] = 2;
r2["name"] = "bcd";
table.Rows.Add(r2); this.dataGridView1.DataSource = table;
} private void dataGridView1_RowEnter(object sender, DataGridViewCellEventArgs e)
{
int rowIndex = e.RowIndex;
this.textBox1.Text = this.dataGridView1.Rows[rowIndex].Cells["ID"].Value.ToString();
this.textBox2.Text = this.dataGridView1.Rows[rowIndex].Cells["name"].Value.ToString();
}
}
}我试了一下,可以实现功能