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.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Kirtan PatelRank #292 · 8.6K reputationPosted Nov 21, 2009, 12:20 AM EST
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]
limor7 reputationPosted Nov 21, 2009, 1:41 AM EST
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 PatelRank #292 · 8.6K reputationPosted Nov 21, 2009, 2:13 AM EST
limor7 reputationPosted Nov 21, 2009, 3:29 AM EST
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.
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 21, 2009, 6:34 AM EST
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.
limor7 reputationPosted Nov 21, 2009, 7:43 AM EST
Nilanka DharmadasaRank #1,875 · 794 reputationPosted Nov 22, 2009, 5:32 PM EST
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.