Click here to Skip to main content
16,004,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please can any one help me out with this problem.
I have the following code in my button click event.

I need to add item from my textbox to the gridview, but when I click the button, instead of displaying the values on the datagridview, it counts and display the length of each string in the textbox.

Please, how do I sort this out

private void btnAdd_Click(object sender, EventArgs e)
{
    List<string> items = new List<string>();
    items.Add(txtItem.Text);
    items.Add(txtName.Text);
    items.Add(txtAddress.Text);
    items.Add(txtState.Text);
           
    DGV.DataSource = items.ToList();
}</string></string>
Posted
Updated 26-Jul-10 21:28pm
v4
Comments
Dalek Dave 27-Jul-10 3:28am    
Edited for readability and Grammar.

Of the top of my head, I can't think of a simple answer. I can sort of see why it happens, but it would make more sense if it didn't.
Any way, you can do it by not having the list at all:
dataGridView1.Columns.Add("Col1", "Column 1");
dataGridView1.Rows.Add("Hello");
dataGridView1.Rows.Add("Welcome");
dataGridView1.Rows.Add("GoodBye");
or by iterating though your list.
List<string> list = new List<string>();
list.Add("Hello");
list.Add("Welcome");
list.Add("GoodBye");
dataGridView1.Columns.Add("Col1", "Column 1");
foreach (string s in list)
    {
    dataGridView1.Rows.Add(s);
    }</string></string>
Either way, you will need to manually back-modify any changes into the textboxes from the datagridview
 
Share this answer
 
Comments
Jondo24shoots 26-Jul-10 12:03pm    
Thanks alot. It worked perfectly
This is happening because gridview is allowing you to bind to a property .... ironically the string object only has one property "Length" :)

one way you can solve it is create a wrapper class for your values like

C#
class stringWrapper
        {
            public string StringValue { get; set; }

            public stringWrapper()
            { }

            public stringWrapper(string str)
            {
                this.StringValue = str;
            }
        }


the you can write your code now just modified as

List<stringwrapper> items = new List<stringwrapper>(); 
    items.Add(new stringWrapper(txtItem.Text));
    ......
    DGV.DataSource = items;
</stringwrapper></stringwrapper>

unless you wish to bind and utilize the benifits there ... I would just go with Answer 1
 
Share this answer
 
Yes the Perfect Solution is to make a New StringWrapper Class and make a List of it.....

some what this way....
public class StringWrapper
     {
        string text;
        [Bindable(true)]
        public string Text
        {
            get
            {
                return text;
            }
        }
       public  StringWrapper(string s)
       {
           text = s;
       }
     }


then use your List this way
List<<StringWrapper>> l = new List<<StringWrapper>>();
           l.Add(new StringWrapper(textBox1.Text));
           l.Add(new StringWrapper(textBox2.Text));
           l.Add(new StringWrapper(textBox3.Text));

           dataGridView1.DataSource = l.ToList<<StringWrapper>>();<stringwrapper>();</stringwrapper>



Hope this will help you and Sorry if this is a silly answer.... thanx guys for sharing your experience and needs.......
 
Share this answer
 
v2

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