我在MSDN中没有找到网上说的datagrid1.CurrentCell,怎样获得列名呢?请大家帮忙啊

解决方案 »

  1.   

    dataGridView1.Columns[1].Name或者dataGridView1.Columns[1].headertext
      

  2.   

    在给单元格控件命名时,把列名加进去:txt_ColumnName,btn_ColumnName
      

  3.   

    我想在程序中根据用户在datagrid或gridview中选择的项,来判断选择的项所在列的列名,然后再作处理,我已经在设计时绑定了列,改了HeaderText,大家帮帮忙啊,谢谢啦
      

  4.   

            private void Form1_Load(object sender, EventArgs e)
            {
                SqlConnection Conn = new SqlConnection();
                Conn.ConnectionString = "server=(local);Database=pi;uid=sa;pwd=200082442311";
                Conn.Open();
                DataSet ds = new DataSet();
                SqlDataAdapter DP = new SqlDataAdapter("select top 10 * from project", Conn);
                DP.Fill(ds, "table");
                DP.Dispose();
                Conn.Close();
                Conn.Dispose();             
                ds.Tables[0].DefaultView.AllowDelete = true;
                dataGridView1.AutoGenerateColumns = false;
                DataGridViewTextBoxColumn colName = new DataGridViewTextBoxColumn();
                colName.Name = "PKID";
                colName.HeaderText = "PKID";
                colName.DataPropertyName = "PKID";
                this.dataGridView1.Columns.Add(colName);            DataGridViewTextBoxColumn colName1 = new DataGridViewTextBoxColumn();
                colName1.Name = "SALES";
                colName1.HeaderText = "SALES";
                colName1.DataPropertyName = "SALES";
                this.dataGridView1.Columns.Add(colName1);            dataGridView1.DataSource = ds.Tables[0];//.DefaultView;//设置DataGrid的数据源
            }        private void dataGridView1_KeyDown(object sender, KeyEventArgs e)
            {
                MessageBox.Show(e.KeyCode.ToString());    
                if (e.KeyCode == Keys.Enter)
                {
                    MessageBox.Show(((DataGridView)sender).CurrentCell.OwningColumn.Name);
                }
            }
      

  5.   

    你是意思是不是怎样知道用户现在点在dataGridView1的哪个行列上?这个好办啊,取当前用户的下而后i值,再NAME就行了。
    GridView1.Rows[i].Cells[i]...
    dataGridView1.Columns[i].Name
      

  6.   

    谢谢大家,netbor,我是这个意思,但是我在gridview1.selectedIndexchanged方法里面这样写:
    gridview1.selectedIndexchanged(,)
    {
       string name;
       name = gridview1.Rows[1].Cells[1].Name;
      
    }报错说没有Name属性阿,而且怎莫gridview不能选择呢? 我是新手,让大家见笑了,请高手们指点。
      

  7.   

    gridview1.Rows[1].Cells[1].Text.ToString();
      

  8.   

    我想获取的是鼠标选中的单元格的值和此单元格所在列的列名,用gridview1.Rows[1].Cells[1].Text.ToString();只能获得第一行第一列的值,不能动态改变。
      

  9.   

    回houyy0428,能不能贴行代码?怎莫用findcontrol方法?
      

  10.   

     for (int i = 0; i < GridViewPro.Rows.Count; i++)
                        {
                            CheckBox pro = (CheckBox)GridViewPro.Rows[i].FindControl("CheckPro");
                            Label laname = (Label)GridViewPro.Rows[i].FindControl("LabProductname");
                            if (pro.Checked)
                            {                            ProMeDetailsBO pmbo = new ProMeDetailsBO();
                                string productname = laname.Text.ToString();
                                SqlParameter[] paramlist = new SqlParameter[1];
                                SqlParameter name = new SqlParameter("@productname", SqlDbType.NVarChar);
                                name.Value = productname;
                                paramlist[0] = name;
                                ProMeDetail pmd = new ProMeDetail();
                                pmd.DeleteBatch("DelectProductByProductname", paramlist);
                                Label labnote = (Label)GridViewPro.Rows[i].FindControl("Labnote");
                                labnote.Text = "D";                        }
      

  11.   

    谢谢楼上 ,可是我不知道选中的是哪个label绑定列阿,所以(Label)GridViewPro.Rows[i].FindControl( "Labnote "); 不能使用
      

  12.   

      MessageBox.Show(dataGridView1.Columns[this.dataGridView1.CurrentCell.ColumnIndex].Name);
    你试一下,dataGridView1_CellLeave事件