如何根据行号获取列内容

时间:2014-07-08 05:10:39

标签: c# winforms datagridview

如何根据行号获取列内容(我已编写代码以获取所单击单元格的特定行号...我想要做的就是检索该特定行的所有列内容)在数据网格中使用c#查看。

这是我的代码:

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
  this.dataGridView1.CellBeginEdit += new DataGridViewCellCancelEventHandler(dataGridView1_CellBeginEdit);

  this.dataGridView1.CellEndEdit += new DataGridViewCellEventHandler(dataGridView1_CellEndEdit_1);

  this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;

  this.comboBox1.Visible = true;

}


void dataGridView1_CellBeginEdit(object sender, DataGridViewCellCancelEventArgs e)
{

  this.comboBox1.SelectedIndex = 0;

  this.comboBox1.Location = this.dataGridView1.GetCellDisplayRectangle(e.ColumnIndex, e.RowIndex, true).Location;

  this.comboBox1.Size = this.dataGridView1.CurrentCell.Size;
  this.comboBox1.Visible = true;
}

private void dataGridView1_CellEndEdit_1(object sender, DataGridViewCellEventArgs e)
{
  int i;
  if (this.comboBox1.SelectedItem != null)
  {

    //this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
    this.dataGridView1.CurrentCell.Value = this.comboBox1.SelectedValue;
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
      var value = (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].RowIndex.ToString());
      MessageBox.Show(value); // right here i am getting the row number of clicked cell.       

1 个答案:

答案 0 :(得分:0)

假设您有一个DataTable作为dataGridView1的数据源

如果你知道行和列索引,下面的代码会给出单元格的内容:

string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX].ItemArray[COLUMNINDEX].ToString();

如果你知道行索引和列名,下面的代码会给出单元格的内容:

string data = ((DataTable)dataGridView1.DataSource).Rows[ROWINDEX]["COLUMNNAME"].ToString();