DataTable dt = DataGridView1.DataSource;
                dt.PrimaryKey;

解决方案 »

  1.   

    datagridview1.Rows[i].Cells["列名"].Value 
    dataGridView1.CurrentRow.Cells[""].Value
      

  2.   

    datagridview1.Rows[i].Cells["列名"].Value 
    dataGridView1.CurrentRow.Cells[""].Value
      

  3.   


    这个应该不复杂,你把主键列也查询出来绑定在datagrid(也可以隐藏主键列,设置其visible属性就可以了)
    然后:this.datagridview.Rows.["所选行"].Cells["主键列名或Index"].value;
    就可以了。
      

  4.   

    dataGridView1.CurrentRow.Cells["主键"].Value 
      

  5.   

    如果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());
      

  6.   

    看不到图片也打不开网址, 你选中的话肯定有个事件啊 ,你在事件里面写不就行了好像是这个GridViewRowEventArgs  e
    e.Rows.keys
    这样吧 
      

  7.   

    如果你不用‘数据源’这种方式,给你个完整的读取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());
                }
            }}
      

  8.   

      设置GridView 的 DataKeyNames="你的主键字段"
      在程序中取值:
      GridView1.DataKeys[e.RowIndex][0].ToString()