古詩詞大全網 - 成語查詢 - winform中獲取datagridview如何獲取選中的行,返回值object

winform中獲取datagridview如何獲取選中的行,返回值object

,使用時需要先判斷this.dataGridView1.SelectedCells.count,不為0 在進行上面的操作。

獲取總行數:dataGridView1.Rows.Count;

獲取當前選中行索引:int i = this.dataGridView1.CurrentRow.Index;

獲取當前選中列索引:int j = this.dataGridView1.CurrentCell.ColumnIndex;

方法壹:

this.dgvStuList.SelectedRows[0].Cells["列"].Value.ToString()

方法二:

dgvStuList.Rows[dgvStuList.CurrentRow.Index].Cells["列名"].Value.ToString()

/////////////////////////////////////////////////

點擊已有數據行時只允許修改和刪除,不可以添加,點擊空白行時只運行添加,不允許刪除和修改,那問題是如何來判斷用戶點擊的是空白行呢,按鈕是否可用的判斷代碼放在哪裏呢?

解決方法:給datagrid添加mouse_enter事件

private void userInfo_MouseEnter(object sender,System.EventArgs e) {

int row = userInfo.CurrentCell .RowNumber;

if(row == list.Size) //list 是我用來存放數據表的對象集合, {

menu.MenuItems[0].Enabled = true;

}

else

{

menu.MenuItems[0].Enabled = false;

}

}

///////////////////////////////////////////////////////////