datagridview选中行(datagridview选中行怎么实现)
一、c# datagridview 如何选中行,以及怎么获取选中行的数据
可以设置DataGridView的SelectionMode属性为FullRowSelect实现左键点击选取整行,右击的话就需要在鼠标点击事件里面实现了
如下:
private voiddataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
{
if(e.ColumnIndex< 0|| e.RowIndex< 0) return;
if(e.Button== System.Windows.Forms.MouseButtons.Right)
{
dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Selected= true;
}
}
右键菜单自己设置关联上dgv就可以,右键菜单的按钮点击事件内容如下:
if(dataGridView1.CurrentRow== null) return;
DataGridViewRow dgvr=dataGridView1.CurrentRow;
string val= dgvr.Cells["???"].Value.ToString();你自己要获取的数据
扩展内容c#怎么获取datagridview选中多行的每一行中的某一列的值
vardatagridview=newDataGridView();
vardataselect=datagridview.SelectedRows;
varlabel=newLabel();
foreach(DataGridViewRowrowindataselect)
{
label.Text+=row.Cells[1].Value+"\n";
}
c# datagridview如何选中行-搜狗百科
二、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;
}
}
///////////////////////////////////////////////////////////
三、c# 中如何DataGridView选中行的值
1、获得某个(指定的)单元格的值:
dataGridView1.Row[i].Cells[j].Value;
2、获得选中的总行数:
dataGridView1.SelectedRows.Count;
3、获得当前选中行的索引:
dataGridView1.CurrentRow.Index;
4、获得当前选中单元格的值:
dataGridView1.CurrentCell.Value;
5、取选中行的数据
string[]str=newstring[dataGridView.Rows.Count];
for(inti;i<dataGridView1.Rows.Count;i++)
{
if(dataGridView1.Rows[i].Selected==true)
{
str[i]=dataGridView1.Rows[i].Cells[1].Value.ToString();
}
}
6、获取选中行的某个数据
inta=dataGridView1.SelectedRows.Index;
dataGridView1.Rows[a].Cells["你想要的某一列的索引,想要几就写几"].Value;
7、获得某个(指定的)单元格的值:dataGridView1.Row[i].Cells[j].Value;Row[i]应该是Rows[i]
inta=dataGridView1.CurrentRow.Index;
stringstr=dataGridView1.Row[a].Cells["strName"].Value.Tostring();
selectedRows[0]当前选中的行
.cell[列索引].values就是当前选中行的某个单元格的值
DataGridView1.SelectedCells(0).Value.ToString取当前选择单元内容
DataGridView1.Rows(e.RowIndex).Cells(2).Value.ToString当前选择单元第N列内容
扩展资料
C#DataGridView选中多行并删除
if(this.dataGridView1.Rows.Count==0)
{
MessageBox.Show("没有记录可以下机");
return;
}
DialogResultdr=MessageBox.Show("删除后不可恢复,确定要删除选中的上机用户吗?","提示",MessageBoxButtons.OKCancel);
if(dr==DialogResult.OK)
{
for(inti=0;i<dataGridView1.SelectedRows.Count;i++)
{
if(dataGridView1.SelectedRows[i].Cells[0].Value.ToString()=="√")
{
this.dataGridView1.Rows.RemoveAt(i);
}
}
}
}