C# 从选定的 datagridview 行和哪个事件中获取数据?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11260843/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Getting data from selected datagridview row and which event?
提问by Samy S.Rathore
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,
我在 Windows 窗体上有一个 DataGridView (Selectionmode: FullRowSelect) 和一些文本框,所以我想要做的是每当用户选择一行(单击或双击)时,该行的内容必须显示在文本中盒子,
i tried out this codes
我试过这个代码
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...:(
还有许多其他文本框,但主要问题是似乎没有任何事件被触发,我应该使用什么事件来触发,或者是否有一些我可能设置错误的数据网格属性?任何帮助,将不胜感激...:(
采纳答案by Vale
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:
您可以使用 SelectionChanged 事件,因为您使用的是 FullRowSelect 选择模式。比在处理程序内部,您可以访问 SelectedRows 属性并从中获取数据。例子:
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...
您还可以遍历列集合而不是键入索引...
回答by Giri
You should check your designer file. Open Form1.Designer.cs and
find this line: windows Form Designer Generated Code.
Expand this and you will see a lot of code. So check Whether this line is there inside datagridview1 controls if not place it.
您应该检查您的设计器文件。打开 Form1.Designer.cs 并
找到这一行:windows Form Designer Generated Code。
展开这个,你会看到很多代码。因此,如果没有放置它,请检查此行是否在 datagridview1 控件内。
this.dataGridView1.CellClick += new System.Windows.Forms.DataGridViewCellEventHandler(this.dataGridView1_CellClick);
I hope it helps.
我希望它有帮助。
回答by vikscool
First take a label. set its visibility to false, then on the DataGridView_CellClick event write this
首先取一个标签。将其可见性设置为 false,然后在 DataGridView_CellClick 事件上写下这个
private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
{
label.Text=dataGridView1.Rows[e.RowIndex].Cells["Your Coloumn name"].Value.ToString();
// then perform your select statement according to that label.
}
//try it it might work for you
回答by helper
You can try this click event
你可以试试这个点击事件
private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex >= 0)
{
DataGridViewRow row = this.dataGridView1.Rows[e.RowIndex];
Eid_txt.Text = row.Cells["Employee ID"].Value.ToString();
Name_txt.Text = row.Cells["First Name"].Value.ToString();
Surname_txt.Text = row.Cells["Last Name"].Value.ToString();
回答by Harshal Doshi Jain
Simple solution would be as below. This is improvement of solution from vale.
简单的解决方案如下。这是对 vale 解决方案的改进。
private void dgMapTable_SelectionChanged(object sender, EventArgs e)
{
int active_map=0;
if(dgMapTable.SelectedRows.Count>0)
active_map = dgMapTable.SelectedRows[0].Index;
// User code if required Process_ROW(active_map);
}
Note for other reader, for above code to work FullRowSelectselection mode for datagridview should be used. You may extend this to give message if more than two rows selected.
请注意其他读者,FullRowSelect应使用以上代码才能使用 datagridview 的选择模式。如果选择了两行以上,您可以扩展它以提供消息。

