Click here to Skip to main content
15,909,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a question on C# Winforms as follows how to pass multi select datagridview data (have a single column) to single textbox in another form? Any suggestion would be appreciated. Thanks.

ProductView is my Class and pv is a list which is shown in below picture. When i click Add To List Button, all product names are kept in datagridview as a list. Then i want data on this list to pass another textbox.

https://i.stack.imgur.com/LwHrD.png

Hope that is clear.

What I have tried:

private void btnAddtoTextBox_Click(object sender, EventArgs e)
 {
     NewForm nf = new NewForm();

 List<ProductView> pv = new List<ProductView>();

     foreach (var item in pv)
     {
         if (pv != null)
         {
             nf.txtMyTextBox.Text = string.Join(",", pv.Select(c => c.ProductName.ToString()).ToArray());
         }

     }
 }
Posted
Updated 31-Dec-17 7:11am

Um.
The code you show does nothing: you create pv as a new collection:
List<ProductView> pv = new List<ProductView>();
You then immediately use it in a foreach loop:
foreach (var item in pv)
But because the collection is brand new, it's empty and the loop body is never entered. Which is actually a bonus, because if it did, it wouldn't work. Look at the body of your loop: pv can't be null - because you created a new collection immediately wearlier. You set the text box to the same text each time you go round the loop. You call ToString on an item which is (from it;s name) already a string. The text box shouldn't even be visible outside the new form itself, so unless you have changed it's declaration to public that code won;t compile - and if you have, that's a very bad idea: controls are private so that the inner workings of the form aren't exposed to the outside world and you can change it without messing the "outside world" at all.

In addition, nf is also created as a new form, but it's never shown - so even if you added items to it you wouldn't be able to see them.

To be honest, that looks like "panic code" - the type that gets thrown together when somebody doesn't think about what they are doing before attacking the keyboard. I'd strongly suggest you think about what you are doing for a while, then scrap this and start again.
 
Share this answer
 
Comments
gokhan572 31-Dec-17 10:09am    
There are already values in pv. Then i used it in foreach loop. Modifiers state of textbox in new form is already set as public. Pls let me know your solution based on this info. Thanks in advance.
Dave Kreskowiak 31-Dec-17 14:37pm    
Not in that code you posted. pv is an empty list.
gokhan572 1-Jan-18 4:51am    
i know it looks an empty list but i have another method for it. So i need only help for this field
nf.txtMyTextBox.Text = string.Join(",", pv.Select(c => c.ProductName.ToString()).ToArray());
When i pass data to textbox in the same form then it works but for another form , it does not.
OriginalGriff 1-Jan-18 5:00am    
And what does "When i pass data to textbox in the same form then it works but for another form , it does not." mean? What happens that you didn't expect, or doesn't happen that you did? And why are you trying to access controls on another form directly? I already told you it's a very bad idea...

Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with.
try
C#
NewForm nf = new NewForm(); 
            List<ProductView> pv = GetValuesFromDataGridView();
            if (pv != null) 
                 nf.txtMyTextBox.Text  =string.Join(",", pv.Select(c => c.ProductName));
            nf.Show();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900