Hi,
I would like to know how can I save data of row in dataGridView (The event - when clicking on on of the rows in the grid).
I need to save the data of the selected row and display it on other window.
Thanks in advance.
Limor.
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Nilanka DharmadasaPosted Nov 22, 2009, 10:32 PM
It's a delegate. Delegate is like a function pointer.
Refer to this if you like to know more information.
Using delegates is the best way to resolve your problem.
If you find my answer useful, please mark "Do you like this answer' check box.
limorPosted Nov 21, 2009, 12:43 PM
Nilanka DharmadasaPosted Nov 21, 2009, 11:34 AM
I have the answer for your problem.
If you find my answer useful, please do not forget to check 'Do you like My answer'..
Here is the code.
Form1
--------
Let's say you have your datagridview in this form.
You should define a delegate in this class. Focus on the lines in red color.
public partial class Form1 : Form
{
public delegate void PassDataGridData(string text);
public PassDataGridData MyGridData;
public Form1()
{
InitializeComponent();
}
private void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
if (e.RowIndex < 0)
return;
DataGridViewRow dr = dataGridView1.Rows[e.RowIndex];
string data = "";
foreach (DataGridViewCell cell in dr.Cells)
{
if(cell.Value != null)
data += cell.Value.ToString() + ",";
}
if (MyGridData != null)
MyGridData(data);
}
}
Form2
--------
Then the other form.
Let's say you want to display the text comes from datagridview in a text box in form2.
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void Form2_Load(object sender, EventArgs e)
{
Form1 a = new Form1();
a.MyGridData = new Form1.PassDataGridData(GridData);
a.Show();
}
public void GridData(string text)
{
textBox1.Text = text;
}
}
That's all. You can slightly change this code to match your exact requirement. But I hope this will help you.
If you still have problem, please let me know.
Please do not forget to mark 'Do you like my answer' check box.
limorPosted Nov 21, 2009, 8:29 AM
do you have some mail that I can send?
Or just an explanation of which steps do I need to do in order to select a row and get its contents and display it on other forms text box.
I will appreciate your answer, I am pretty new in C# and dont know what to do.
Thanks in advance. Limor.
Kirtan PatelPosted Nov 21, 2009, 7:13 AM
limorPosted Nov 21, 2009, 6:41 AM
I saved the project into the following link -
http://www.mediafire.com/?sharekey=eff7fdd870ce16c6e62ea590dc5e5dbbdea99c482ca053c382e3a934329c7a5e
The DB - Project1.mdb
The required datagrid to select the row is on the Customers.cs form
and I would like to populated the selected content into the parent form - Work_form.cs
Thanks in advance. Limor.
Kirtan PatelPosted Nov 21, 2009, 5:20 AM
I know the solution of problem but i need your project files to tell you more specific code :)
if you can provide the code i will solve your problem
just upload your project files along with database file to http://mediafire.com
and tell me the download link they provide you :)
[email protected]