如何在c#的有序列表中显示文本框中输入的字符串?

时间:2015-06-02 05:46:04

标签: c# datagridview

我有以下要求:

有一个文本框。每当我在文本框中键入一个字符串并按ENTER键时,该特定字符串应该附加到datagridview,并且应该清除文本框。同样的过程再次重复。

我是C#的新手,有什么建议从哪里开始?

仅供参考,参考图像。

enter image description here

源代码:

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)
    {    
    }
 }
}

这里我使用了listbox。所以我的输出应该像参考图像。

2 个答案:

答案 0 :(得分:2)

通过General-Doomer为Answer添加Insert to DataGrid功能。

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

编辑

//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();
        }
    }

答案 1 :(得分:1)

完整的示例表单代码:

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();
                }
            }
        }
    }
}

结果:

Screenshot