Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Fill datagridview with text from a combobox C#

$
0
0

Hi i have a combobox(used as a list of all my products) that has something like this  Drink Water 1 (for category name and price) loaded from a text file ,so far i can place the columns but can only add one product ,after adding another with a button the previous gets replaced.

            

 private void button2_Click(object sender, EventArgs e)
        {

            DataTable dt = new DataTable();

            {

                dt.Columns.Add("Category");
                dt.Columns.Add("Name");
                dt.Columns.Add("Price");     
            }
            string newline;
            newline = comboBox1.Text;
                DataRow dr = dt.NewRow();
                string[] values = newline.Split(' ');
                for (int i = 0; i < values.Length; i++)
                {
                    dr[i] = values[i];
                }
                dt.Rows.Add(dr);
            dataGridView1.DataSource = dt;

        }



Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>