【发布时间】:2015-08-15 18:48:28
【问题描述】:
我有以下要求:
只有一个文本框。每当我在文本框中键入一个字符串并按 ENTER 时,该特定字符串应附加到 datagridview 并应清除文本框。再次重复相同的过程。
我是 C# 新手,有什么建议从哪里开始吗?
仅供参考,参考图片。
源代码:
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)
{
}
}
}
这里我使用了列表框。所以我的输出应该像参考图像。
【问题讨论】:
-
您可能需要使用
TextBox类的TextChanged事件。 -
要插入到datagridview,您可以参考this。
-
你应该向我们展示你所做的努力以及你在哪里击中。
标签: c# datagridview