如何获得c#dataGridView 选中行的主键? DataTable dt = DataGridView1.DataSource; dt.PrimaryKey; 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 datagridview1.Rows[i].Cells["列名"].Value dataGridView1.CurrentRow.Cells[""].Value datagridview1.Rows[i].Cells["列名"].Value dataGridView1.CurrentRow.Cells[""].Value 这个应该不复杂,你把主键列也查询出来绑定在datagrid(也可以隐藏主键列,设置其visible属性就可以了)然后:this.datagridview.Rows.["所选行"].Cells["主键列名或Index"].value;就可以了。 dataGridView1.CurrentRow.Cells["主键"].Value 如果DataGridView绑定的是强类型数据集,并且是通过BindingSource绑定的,比如你是通过“数据源”-“添加数据源”把Northwind数据库的Categories表拖到Form上生成的DataGirdView, BidningSource, BindingNavigator等等,那么:DataRowView drv = categoriesBindingSource.Current as DataRowView;NorthwindDataSet.CategoriesRow row = drv.Row as NorthwindDataSet.CategoriesRow;MessageBox.Show(row.CategoryID.ToString()); 看不到图片也打不开网址, 你选中的话肯定有个事件啊 ,你在事件里面写不就行了好像是这个GridViewRowEventArgs ee.Rows.keys这样吧 如果你不用‘数据源’这种方式,给你个完整的读取Northwind的Products的例子,用到BindingSource.Current.public partial class Form1 : Form{ private BindingSource bs; private bool loaded = false; private DataTable dt; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { using (SqlConnection conn = new SqlConnection()) { conn.ConnectionString = "database=northwind;uid=sa;pwd=;server=."; SqlCommand cmd = conn.CreateCommand(); cmd.CommandText = "select * from Products"; conn.Open(); dt = new DataTable(); bs = new BindingSource(); dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection)); bs.DataSource = dt; dataGridView1.DataSource = bs; } loaded = true; } private void dataGridView1_SelectionChanged(object sender, EventArgs e) { if (loaded) { DataRowView drv = bs.Current as DataRowView; MessageBox.Show(drv["ProductID"].ToString()); } }} 设置GridView 的 DataKeyNames="你的主键字段" 在程序中取值: GridView1.DataKeys[e.RowIndex][0].ToString() 想让label里字体滚动 如何获取某路径下的文件的文件名? 2个项目之间的数据互访问题,看似简单却难倒很多人,用于项目混淆加密太有价值了。 水晶报表问题 <input id="hf" type="hidden"/>我用变量result赋给隐藏域 求根据输入汉语得到拼音码的C#程序(不要带数据库的) 有关打开Word文档的问题,开始时打开一切是正常的,调试其他问题一段时间后执行无发应!!参与有分!!!!! 【【【为嘛这么坑爹?】】】 repeater的一个问题 高手请进! c# Mscomm窗体间调用 backgroundworker TargetInvocationException
dataGridView1.CurrentRow.Cells[""].Value
dataGridView1.CurrentRow.Cells[""].Value
这个应该不复杂,你把主键列也查询出来绑定在datagrid(也可以隐藏主键列,设置其visible属性就可以了)
然后:this.datagridview.Rows.["所选行"].Cells["主键列名或Index"].value;
就可以了。
比如你是通过“数据源”-“添加数据源”把Northwind数据库的Categories表拖到Form上生成的DataGirdView, BidningSource, BindingNavigator等等,那么:DataRowView drv = categoriesBindingSource.Current as DataRowView;
NorthwindDataSet.CategoriesRow row = drv.Row as NorthwindDataSet.CategoriesRow;
MessageBox.Show(row.CategoryID.ToString());
e.Rows.keys
这样吧
public partial class Form1 : Form
{
private BindingSource bs;
private bool loaded = false;
private DataTable dt;
public Form1()
{
InitializeComponent();
} private void Form1_Load(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = "database=northwind;uid=sa;pwd=;server=.";
SqlCommand cmd = conn.CreateCommand();
cmd.CommandText = "select * from Products";
conn.Open();
dt = new DataTable();
bs = new BindingSource(); dt.Load(cmd.ExecuteReader(CommandBehavior.CloseConnection));
bs.DataSource = dt;
dataGridView1.DataSource = bs;
}
loaded = true;
} private void dataGridView1_SelectionChanged(object sender, EventArgs e)
{
if (loaded)
{
DataRowView drv = bs.Current as DataRowView;
MessageBox.Show(drv["ProductID"].ToString());
}
}}
在程序中取值:
GridView1.DataKeys[e.RowIndex][0].ToString()