views:

96

answers:

1

How i can add the multiple values from a datagrid to textbox in C#? In my project, it includes a datagrid, a textbox and a button.

when the datagrid is populated from the database, the add button will be enable. after that when i click the one entry followed by hitting the add button the selected value will be populated in textbox by seperating a comma. How it will done.

+1  A: 

You will need something like this:

on btn click

{
   if(dg.selectedItem != null)
   {
      if (txt.text.length !=0)
      {
        txt.text = txt.text + ", ";
      }
      txt.text = txt.text + dg.selectedItem.text;
   }
}

The dg part is probably wrong it is more likely something like dg.selectedRow[ColName].text.

Now if you decide to add a delete or remove btn you might what to come up with another idea.

NitroxDM
Thanks, it works as fine..
Dilse Naaz
You could make that more compact and use: txt.text += ", "andtxt.text += dg.selectedItem.text
link664
or more compact txt.text += (((txt.text.length != 0) ? "," :"")+dg.selectedItem.text); at all
Toto