简体   繁体   English

需要DataGridView C#帮助

[英]DataGridView C# Help Needed

So I have a DGV connected to my Visual C# via OleDB with my information in it and I need to take the text from a cell when clicking the cell. 所以我有一个DGV连接到我的Visual C#通过OleDB与我的信息,我需要在单击单元格时从单元格中取出文本。 I know I can't take it from the DGV, I need to get it from de OleDB DataSet. 我知道我不能从DGV中获取它,我需要从de OleDB DataSet获取它。 But how do I tell the program which DGV cell I need to take the text from? 但是,如何告诉程序我需要从哪个DGV单元中获取文本? And how would the DataSet Codeline be? DataSet Codeline将如何? Any help will be thanked. 任何帮助将不胜感激。

Try this to get cell value, 试试这个以获得细胞价值,

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    if (dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value != null)
    {
       MessageBox.Show(dataGridView1.Rows[e.RowIndex].Cells[e.ColumnIndex].Value.ToString());
    }
}

I would look into using the following event handler. 我将研究使用以下事件处理程序。 It fires each time the DataGridView is edited, so if you click on any cell it will fire. 每次编辑DataGridView时都会触发,因此如果单击任何单元格,它将触发。 Below is a basic example of grabbing the data from current selected cell. 下面是从当前所选单元格中获取数据的基本示例。 Have a play about with it. 玩得开心吧。

 private void dataGridView1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
        {
           var result =  dataGridView1.CurrentCell.Value;
        }

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM