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.
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.