谁能告诉我,在dataGrid中,选择了某一项,如何获取他选种的项的索引(就是第几行)??

解决方案 »

  1.   

    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
             //行索引
    int i = dataGrid1.CurrentRowIndex;
    }
      

  2.   

    楼上的仅局限在选中某一行而且是在未对DataSet进行排序操作的情况下实用。如果要真正得到某一行或几行的索引,要自己动手编写代码的!
      

  3.   

    别听楼上的,dataGrid1.CurrentRowIndex,就可以取到你想要的.有排序也没关系,你可以从datagrid1[dataGrid1.CurrentRowIndex,0]这样直接从datagrid取值,而不用从dataset取
      

  4.   

    这个currentIndex在没选定项的时候也是0,那么如果你要删除的时候默认下还是会删掉呀。
    我要的是只有选定了某一项才删,而如果选定了第一项为0,不选也为0那么他们之间如何来区分呢?
      

  5.   

    private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)
    {
             //行索引
    int i = dataGrid1.CurrentRowIndex;
    }
    如果你是选择某一行删除,这个事件不太适合。。随便到一本。NETweb开发的书看看,应该都会有说到
      

  6.   

    if(MessageBox.Show("是否确定删除该记录?","询问",MessageBoxButtons.OKCancel,MessageBoxIcon.Question,
    MessageBoxDefaultButton.Button2)== DialogResult.OK)
    {
        //删除记录
    }
    这样就不会误删了
      

  7.   

    C/S OR B/S
    C/S: this.BindContext[this.grid1.DataSource].Position
         如果数据源是Table或DataView,this.BindContext[this.grid1.DataSource].Current是DataRowView
      

  8.   

    事件是
    this.BindContext[this.grid1.DataSource].PositionChanged
      

  9.   

    .....你可以判断啊
    if(datagrid1.IsSelected(datagrid.CurrentRowIndex))
    {
         dosomething();
    }