Datagridview全行select,但获得单个单元格的值
我有一个完整的行selectdatagridview。 我怎样才能抓取来自某个单元格的数据,而不pipe行中的哪个单元格被点击,因为它突出了整行。
你可以这样做……
private void datagridview1_SelectionChanged(object sender, EventArgs e) { if (datagridview1.SelectedCells.Count > 0) { int selectedrowindex = datagridview1.SelectedCells[0].RowIndex; DataGridViewRow selectedRow = datagridview1.Rows[selectedrowindex]; string a = Convert.ToString(selectedRow.Cells["you have to mention you cell corresponding column name"].Value); } } 如果你想获取选定单元格的内容; 你需要行和单元格的索引。
 int rowindex = dataGridView1.CurrentCell.RowIndex; int columnindex = dataGridView1.CurrentCell.ColumnIndex; dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString(); 
在CellClick事件中,您可以编写以下代码
 string value = datagridviewID.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString(); 
使用bove代码,您将获得您所添加的单元格的值。 如果你想获得点击行中的特定列的值,只需将e.ColumnIndexreplace为你想要的列索引
 您也可以获取单元格的值,也可以在CurrentRow引用当前的select 
 dataGridView1.CurrentRow.Cell[indexorname].FormattedValue 
在这里,您可以使用索引或列名称并获取值。
我只想指出,你可以使用.selectedindex使它更清洁
 string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString(); 
 string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString(); 
  正确使用: dataGridView1.CurrentCell.Value.ToString() 
  private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e) { MessageBox.Show(dataGridView1.CurrentCell.Value.ToString()); } 
要么
  // dataGrid1.Rows[yourRowIndex ].Cells[yourColumnIndex].Value.ToString() //Example1:yourRowIndex=dataGridView1.CurrentRow.Index (from selectedRow ); dataGrid1.Rows[dataGridView1.CurrentRow.Index].Cells[2].Value.ToString() //Example2:yourRowIndex=3,yourColumnIndex=2 (select by programmatically ) dataGrid1.Rows[3].Cells[2].Value.ToString() 
 DataGridView.CurrentRow.Cells[n] 
请参阅: http : //msdn.microsoft.com/en-us/library/system.windows.forms.datagridview.currentrow.aspx
 使用Cell Click因为其他提到的方法将触发数据绑定,如果你想要select的值,那么表单将closures。 
 private void dgvProducts_CellClick(object sender, DataGridViewCellEventArgs e) { if (dgvProducts.SelectedCells.Count > 0) // Checking to see if any cell is selected { int mSelectedRowIndex = dgvProducts.SelectedCells[0].RowIndex; DataGridViewRow mSelectedRow = dgvProducts.Rows[mSelectedRowIndex]; string mCatagoryName = Convert.ToString(mSelectedRow.Cells[1].Value); SomeOtherMethod(mProductName); // Passing the name to where ever you need it this.close(); } } 
对于那些无法触发点击事件的人,他们可能会使用以下代码
 public Form1() { InitializeComponent(); this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick); } 
要根据整个行select获取一个单元格值:
 if (dataGridView1.SelectedRows.Count > 0) { foreach (DataGridViewRow row in dataGridView1.Rows) { TextBox1.Text = row.Cells["ColumnName"].Value.ToString(); } } else { MessageBox.Show("Please select item!"); } } 
 最简单的代码是DataGridView1.SelectedCells(column_index).Value 
举个例子,对于第一个选中的单元格:
 DataGridView1.SelectedCells(0).Value 
这在数据网格视图中得到了确切单元格的文本值
 private void dataGridView_SelectionChanged(object sender, EventArgs e) { label1.Text = dataGridView.CurrentRow.Cells[#].Value.ToString(); } 
你可以在标签中看到它,并改变你想要的确切单元格的索引