从选定的 datagridview 行和哪个事件中获取数据? [英] Getting data from selected datagridview row and which event?

查看:23
本文介绍了从选定的 datagridview 行和哪个事件中获取数据?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Windows 窗体上有一个 DataGridView(Selectionmode:FullRowSelect)和一些文本框,所以我想要做的是每当用户选择一行(单击或双击)时,必须显示该行的内容在文本框中,

I have a DataGridView (Selectionmode: FullRowSelect) on a windows form along with some textboxes, so what i want to do is that whenever a user selects a row(click or double_click maybe), the contents of that row must be displayed in the text boxes,

我试过这个代码

private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
    MessageBox.Show("CEll Double_Click event calls");
    int rowIndex = e.RowIndex;
    DataGridViewRow row = dataGridView1.Rows[rowIndex];
    textBox5.Text = row.Cells[1].Value;
}

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
    int rowIndex = e.RowIndex;
    DataGridViewRow row = dataGridView1.Rows[rowIndex];
    textBox5.Text = dataGridView1.Rows[1].Cells[1].Value.ToString();// row.Cells[1].Value;
}

还有许多其他文本框,但主要问题是似乎没有任何事件被触发,我应该使用什么事件来触发,或者是否有一些我可能设置错误的数据网格属性?任何帮助将不胜感激...:(

there are many other textboxes, but the main problem is that none of the event seems to be triggered, what event should i use to do so, or is there some property of datagrid that i might have set wrong? Any help would be appreciated...:(

推荐答案

您可以使用 SelectionChanged 事件,因为您使用的是 FullRowSelect 选择模式.比在处理程序内部,您可以访问 SelectedRows 属性并从中获取数据.示例:

You can use SelectionChanged event since you are using FullRowSelect selection mode. Than inside the handler you can access SelectedRows property and get data from it. Example:

private void dataGridView_SelectionChanged(object sender, EventArgs e) 
{
    foreach (DataGridViewRow row in dataGridView.SelectedRows) 
    {
        string value1 = row.Cells[0].Value.ToString();
        string value2 = row.Cells[1].Value.ToString();
        //...
    }
}

您还可以遍历列集合而不是键入索引...

You can also walk through the column collection instead of typing indexes...

这篇关于从选定的 datagridview 行和哪个事件中获取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆