如何从文本框中的datagridview检索数据 [英] How to retrieve data from a datagridview in textboxes

查看:66
本文介绍了如何从文本框中的datagridview检索数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在构建应用程序,并且正在构建更新表单.
我在要更新的表格字段中放置了一些文本框.
我需要单击行时,文本框会从有序行的每个单元格中检索数据.

我写了这段代码

I''m building an application and i am building an update form.
I put some textboxes for the fields of the table which i wanna update.
I need when i click a row ,the textboxes retrieve data from each cell on the row ordered.

I wrote this code

//this is my textboxes that will retrieve data
textBox5.Text = "";
textBox6.Text = "";
textBox8.Text = "";
textBox7.Text = "";
//this is the code of mine it works only when i select a row from the datagridview i need it work when i select a cell in this row
string[] arr = new string[4];
int i = 0;
foreach (DataGridViewCell item in dataGridView2.SelectedCells)
{
    arr[i] = item.Value.ToString();
    i++;
}
textBox5.Text = arr[0].ToString();
textBox6.Text = arr[1].ToString();
textBox8.Text = arr[2].ToString();
textBox7.Text = arr[3].ToString();



请帮帮我!

在此先感谢.



Please help me!

Thanks in Advance.

推荐答案

尝试关注


Try following


foreach (DataGridViewCell item in dataGridView2.CurrentRow.Cells)
{
    arr[i] = item.Value.ToString();
    i++;
}


如果您尝试使用WinForms,请进一步阅读,否则请通知我:
无论您是需要在单元格单击(如代码注释中所述)还是在选定的行上执行此作业,您都有这些事件(RowEnter,CellEnter等).事件args(DataGridViewCellEventArgs)为您提供RowIndex和ColumnIndex,您可以使用它们来获取所需的内容.这很简单,不确定您会遇到什么.
If you are trying with WinForms then read further else let me know:
Whether you need to do this job on cell click (as mentioned in the code comments) or on the row select, you have events for these (RowEnter, CellEnter etc.). And the event args (DataGridViewCellEventArgs) provide you with RowIndex and ColumnIndex which you can use to get what you want. It''s pretty straight forward, not sure what you are stuck with.


希望这篇 [ ^ ]的文章可能会对您有所帮助.
Hope this[^] article might help you.


这篇关于如何从文本框中的datagridview检索数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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