Datagridview 全行选择但获取单个单元格值

发布于 2024-12-08 09:23:46 字数 71 浏览 0 评论 0原文

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

I have a datagridview that is a full row select.
How would I grab the data from only a certain cell no matter what cell in the row was clicked on since it highlights the entire row.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(19

喵星人汪星人 2024-12-15 09:23:46

你可以这样做:

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

You can do like this:

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);           
  }
}
绮筵 2024-12-15 09:23:46

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

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

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

If you want to get the contents of selected cell; you need the index of row and cell.

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

dataGridView1.Rows[rowindex].Cells[columnindex].Value.ToString();
鱼窥荷 2024-12-15 09:23:46

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

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

这段代码很简单

I know, I'm a little late for the answer. But I would like to contribute.

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

This code is simple as piece of cake

蓝天白云 2024-12-15 09:23:46

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

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

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

In the CellClick event you can write following code

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

Using the bove code you will get value of the cell you cliked. If you want to get value of paricular column in the clicked row, just replace e.ColumnIndex with the column index you want

拧巴小姐 2024-12-15 09:23:46

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

dataGridView1.CurrentRow.Cell[indexorname].FormattedValue

,您可以使用索引或列名称并获取值。

you can get the values of the cell also as the current selection is referenced under CurrentRow

dataGridView1.CurrentRow.Cell[indexorname].FormattedValue

Here you can use index or column name and get the value.

时光瘦了 2024-12-15 09:23:46

我只是想指出,您可以使用 .selectedindex 使其更干净一些

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

I just want to point out, you can use .selectedindex to make it a little cleaner

string value = gridview.Rows[gridview.SelectedIndex].Cells[1].Text.ToString();
抱猫软卧 2024-12-15 09:23:46
string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();
string value = dataGridVeiw1.CurrentRow.Cells[1].Value.ToString();
云仙小弟 2024-12-15 09:23:46

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

Just Use: dataGridView1.CurrentCell.Value.ToString()

        private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            MessageBox.Show(dataGridView1.CurrentCell.Value.ToString());
        }

or

 // 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()
删除会话 2024-12-15 09:23:46

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

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

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

this get me the text value of exact cell in data grid view

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

you can see it in label and change # wih the index of the exact cell you want

北音执念 2024-12-15 09:23:46

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

Use Cell Click as other methods mentioned will fire upon data binding, not useful if you want the selected value, then the form to close.

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();
    }
}
萌面超妹 2024-12-15 09:23:46

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

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

For those who could not fire the click event, they may use following code

public Form1()
            {
                InitializeComponent();
                this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
            }
℡寂寞咖啡 2024-12-15 09:23:46

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

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

To get one cell value based on entire row selection:

if (dataGridView1.SelectedRows.Count > 0)
      {
         foreach (DataGridViewRow row in dataGridView1.Rows)
           {
               TextBox1.Text = row.Cells["ColumnName"].Value.ToString();
           }
      }
else
      {
        MessageBox.Show("Please select item!");
      }
     }
囍孤女 2024-12-15 09:23:46

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

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

DataGridView1.SelectedCells(0).Value

Simplest code is DataGridView1.SelectedCells(column_index).Value

As an example, for the first selected cell:

DataGridView1.SelectedCells(0).Value
巷子口的你 2024-12-15 09:23:46

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

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

以歌曲疗慰 2024-12-15 09:23:46

对于 vb.net 2013 我使用

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

其中 i 是手机号码

for vb.net 2013 i use

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

where i is the cell number

離人涙 2024-12-15 09:23:46

将 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 中的所有单元格分配给文本框。

Assigns cell 1 of dgwProduct lines to ProductId.

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

Here it puts all the cells in the row into the textbox.

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

In this way, we assign all the cells in the dataGridView to the textboxes.

空气里的味道 2024-12-15 09:23:46
 private void DataGridView_SelectionChanged(object sender, EventArgs e)
    {
        if (dataGridViewX3.SelectedCells.Count > 0)
        {
            int selectedrowindex = dataGridViewX3.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = dataGridViewX3.Rows[selectedrowindex];
            string cellValue = Convert.ToString(selectedRow.Cells[0].Value);
            Console.WriteLine(cellValue);
        }
    }
 private void DataGridView_SelectionChanged(object sender, EventArgs e)
    {
        if (dataGridViewX3.SelectedCells.Count > 0)
        {
            int selectedrowindex = dataGridViewX3.SelectedCells[0].RowIndex;
            DataGridViewRow selectedRow = dataGridViewX3.Rows[selectedrowindex];
            string cellValue = Convert.ToString(selectedRow.Cells[0].Value);
            Console.WriteLine(cellValue);
        }
    }
习惯成性 2024-12-15 09:23:46

要获取 DataGridView 中选定的行和单元格值:

foreach (DataGridViewRow row in this.dataGridView.Rows)
{
    if (row.Selected == true)
    {  
         foreach (DataGridView cell in rows.Cells)
         {
             string temp = cell.Value.ToString();
             // Do something with string value
         }
    }
}

To get a selected row and cell value in DataGridView:

foreach (DataGridViewRow row in this.dataGridView.Rows)
{
    if (row.Selected == true)
    {  
         foreach (DataGridView cell in rows.Cells)
         {
             string temp = cell.Value.ToString();
             // Do something with string value
         }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文