datagridview 选中行(datagridview选中行怎么实现)
一、datagridview 单击某个单元格选中一行
首先:
this.dataGridView1.SelectionMode= DataGridViewSelectionMode.FullRowSelect;//设置为整行被选中
然后,在dataGridView的CellClick事件中提示是否要修改当前行当前单元格的内容:
DialogResult result=MessageBox.Show("是否要修改当前行中单元格的内容?","提示信息",MessageBoxButtons.YesNo,MessageBoxIcon.Question);
if(this.dataGridView1.SelectedRows.Count> 0)
{
if(result== DialogResult.Yes)
{
//修改当前行单元格的内容
}
}
获取当前行单元格内容:
string str= this.dataGridView1.CurrentRow.Cells["列名"].Value;
LZ试试吧~~直接用手敲的,没在程序中运行。希望可以帮到你。
二、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如何选中行-搜狗百科
三、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);
}
}
}
}