【问题标题】:Datagridview full row selection but get single cell valueDatagridview全行选择但获得单个单元格值
【发布时间】:2011-10-05 05:42:40
【问题描述】:

我有一个全行选择的 datagridview。 无论单击行中的哪个单元格,我如何仅从某个单元格中获取数据,因为它突出显示了整行。

【问题讨论】:

标签: c# winforms datagridview


【解决方案1】:

你可以这样做:

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 cellValue = Convert.ToString(selectedRow.Cells["enter column name"].Value);           
  }
}

【讨论】:

  • 太棒了——花很多时间来找到这篇文章
  • @pratap K,DataGridView1_SelectionChanged 事件是否会在表单加载时触发?但它正在发生在我的情况下。请帮我解决这个问题。如果需要更多详细信息,我会将其作为问题发布。
  • 我想要相反的行为,你的帖子帮助我意识到我需要做什么。 SelectedCells.Count == 1 :)
  • 谢谢。很好的答案
【解决方案2】:

如果要获取选中单元格的内容;你需要行和单元格的索引。

int rowindex = dataGridView1.CurrentCell.RowIndex;
int columnindex = dataGridView1.CurrentCell.ColumnIndex; 

dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString();

【讨论】:

    【解决方案3】:

    在 CellClick 事件中您可以编写以下代码

    string value =
          datagridviewID.Rows[e.RowIndex].Cells[e.ColumnIndex].FormattedValue.ToString();
    

    使用上面的代码,您将获得您点击的单元格的值。如果您想在点击的行中获取特定列的值,只需将 e.ColumnIndex 替换为您想要的列索引

    【讨论】:

    • 这是更好的答案,只需要更改e.ColumnIndex 与特定的字符串列名或索引,因为问题被问到了。
    【解决方案4】:

    我知道,我的答案有点晚了。但我愿意贡献。

    DataGridView.SelectedRows[0].Cells[0].Value
    

    这段代码很简单

    【讨论】:

    • 只晚了 6 年半!
    【解决方案5】:

    您也可以获取单元格的值,因为当前选择在 CurrentRow 下引用

    dataGridView1.CurrentRow.Cell[indexorname].FormattedValue
    

    这里可以使用索引或列名来获取值。

    【讨论】:

      【解决方案6】:
      【解决方案7】:

      我只想指出,你可以使用 .selectedindex 让它更干净一些

      string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString();
      

      【讨论】:

        【解决方案8】:
        string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();
        

        【讨论】:

          【解决方案9】:

          只需使用: 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()
          

          【讨论】:

            【解决方案10】:

            这让我得到数据网格视图中确切单元格的文本值

            private void dataGridView_SelectionChanged(object sender, EventArgs e)
                {
                    label1.Text = dataGridView.CurrentRow.Cells[#].Value.ToString();
                }
            

            你可以在标签中看到它并用你想要的确切单元格的索引更改#

            【讨论】:

              【解决方案11】:

              使用Cell Click,因为提到的其他方法将在数据绑定时触发,如果您想要选择的值,然后关闭表单,则无用。

              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();
                  }
              }
              

              【讨论】:

                【解决方案12】:

                对于那些无法触发点击事件的人,他们可以使用以下代码

                public Form1()
                            {
                                InitializeComponent();
                                this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
                            }
                

                【讨论】:

                  【解决方案13】:

                  根据整行选择获取一个单元格值:

                  if (dataGridView1.SelectedRows.Count > 0)
                        {
                           foreach (DataGridViewRow row in dataGridView1.Rows)
                             {
                                 TextBox1.Text = row.Cells["ColumnName"].Value.ToString();
                             }
                        }
                  else
                        {
                          MessageBox.Show("Please select item!");
                        }
                       }
                  

                  【讨论】:

                    【解决方案14】:

                    最简单的代码是DataGridView1.SelectedCells(column_index).Value

                    例如,对于第一个选定的单元格:

                    DataGridView1.SelectedCells(0).Value
                    

                    【讨论】:

                      【解决方案15】:

                      dataGridView1.SelectedRows[0].Cells[0].Value;

                      【讨论】:

                        【解决方案16】:

                        对于 vb.net 2013 我使用

                        DataGridView1.SelectedRows.Item(0).Cells(i).Value
                        

                        其中 i 是单元格编号

                        【讨论】:

                          【解决方案17】:

                          将 dgwProduct 行的单元格 1 分配给 ProductId。

                          private void btnUpdate_Click(object sender, EventArgs e)
                              {
                                      ProductId = Convert.ToInt32(dgwProduct.CurrentRow.Cells[0].Value);
                              }
                          

                          在这里,它将行中的所有单元格放入文本框。

                          private void dgwProduct_CellClick(object sender, DataGridViewCellEventArgs e)
                              {
                                  var row=dgwProduct.CurrentRow;
                                  tbxProductName.Text =row.Cells[1].Value.ToString();
                                  tbxUnitPrice.Text = row.Cells[2].Value.ToString();
                                  tbxQuantityPerUnit.Text = row.Cells[3].Value.ToString();
                                  tbxStockUpdate.Text = row.Cells[4].Value.ToString();
                              }
                          

                          这样,我们将dataGridView中的所有单元格分配给文本框。

                          【讨论】:

                          猜你喜欢
                          • 1970-01-01
                          • 2012-12-18
                          • 2015-12-05
                          • 1970-01-01
                          • 1970-01-01
                          • 2011-04-27
                          • 1970-01-01
                          • 1970-01-01
                          • 1970-01-01
                          相关资源
                          最近更新 更多