0

I have the following requirement :

There is one text box. Whenever I type a string in textbox and press ENTER, that particular string should be appended to the datagridview and textbox should be cleared. Again the same process repeats.

I am new to C# , any suggestion where to start ?

FYI, Reference Image.

enter image description here

source code :

using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    public int count = 0;
    string[] arr = new string[5];
    ListViewItem itm;
    public Form1()
    {
        InitializeComponent();            
    }

    private void Form1_Load(object sender, EventArgs e)
    {
        listView1.View = View.Details;
        listView1.GridLines = true;
        listView1.FullRowSelect = true;    
        //Add column header
        listView1.Columns.Add("List 1", 50);
        listView1.Columns.Add("List 2", 50);
        listView1.Columns.Add("List 3", 50);
        listView1.Columns.Add("List 4", 50);
        listView1.Columns.Add("List 5", 50);    
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string productName = null;
        string price = null;
        string quantity = null;    
        productName = listView1.SelectedItems[0].SubItems[0].Text;
        price = listView1.SelectedItems[0].SubItems[1].Text;
        quantity = listView1.SelectedItems[0].SubItems[2].Text;    
        MessageBox.Show(productName + " , " + price + " , " + quantity);
    }

    private void textBox1_KeyUp(object sender, KeyEventArgs e)
    {
        //MessageBox.Show("hii");            
    }

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyData == Keys.Enter)
        {               
           // MessageBox.Show(count.ToString());
            arr[count] = textBox1.Text;    
            //if (count == 0)
            //{
                itm = new ListViewItem(arr);                    
            //}
            listView1.Items.Add(itm);
           // MessageBox.Show(arr.ToString());
            if (count < 4)
            {
                count = count + 1;
            }
            else if(count == 4)
            {
                count = 0;
            }
        }
    }

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
    {    
    }
 }
}

here i have used listbox. so my output should be like reference image.

Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
Archana Palani
  • 247
  • 1
  • 6
  • 23

2 Answers2

2

Adding the Insert to DataGrid functionality for the Answer by General-Doomer.

void textBox_KeyDown(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.Enter)
    {
        TextBox tb = (TextBox)sender;
        this.dataGridView1.Rows.Add(tb.Text);
        tb.Clear();
    }
}

EDIT :

//global variable and preferably initialize in the constructor or OnLoad Event
List<string> mystringlist = new List<string>();

void textBox_KeyDown(object sender, KeyEventArgs e)
    {
        if (e.KeyCode == Keys.Enter)
        {
            TextBox tb = (TextBox)sender;
            if(mystringlist.Count >= 5)
            {
                this.dataGridView1.Rows.Add(mystringlist.ToArray());
                mystringlist.Clear();
            }
            mystringlist.Add(tb.Text);            
            tb.Clear();
        }
    }
Community
  • 1
  • 1
Rohit Vipin Mathews
  • 11,629
  • 15
  • 57
  • 112
1

Complete sample form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WinForm
{
    public partial class frmMain : Form
    {
        /// <summary>
        /// form constructor
        /// </summary>
        public frmMain()
        {
            InitializeComponent();
        }

        private ListView listView;
        private TextBox textBox;

        /// <summary>
        /// form load
        /// </summary>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            // create list view
            listView = new ListView()
            {
                Location = new Point(8, 8),
                Size = new Size(this.ClientSize.Width - 16, this.ClientSize.Height - 42),
                Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Bottom,
                View = View.List,
            };
            listView.Columns.Add(new ColumnHeader() { Text = "Header" });
            listView.Sorting = SortOrder.Ascending;
            this.Controls.Add(listView);

            // create textbox
            textBox = new TextBox()
            {
                Location = new Point(8, listView.Bottom + 8),
                Size = new Size(this.ClientSize.Width - 16, 20),
                Anchor = AnchorStyles.Left | AnchorStyles.Bottom,
                Text = "Write some text here and press [Enter]"
            };
            this.Controls.Add(textBox);

            // bind textbox KeyDown event
            textBox.KeyDown += textBox_KeyDown;
        }

        /// <summary>
        /// KeyDown event handler
        /// </summary>
        void textBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                // [Enter] key pressed
                TextBox tb = (TextBox)sender;
                string text = tb.Text;

                if (listView.Items.ContainsKey(text))
                {
                    // item already added
                    MessageBox.Show(string.Format("String '{0}' already added", text));
                }
                else
                {
                    // add new item
                    listView.Items.Add(text, text, 0);
                    listView.Sort();
                    tb.Clear();
                }
            }
        }
    }
}

Result:

Screenshot

General-Doomer
  • 2,681
  • 13
  • 13