Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://stackoverflow.com/q/31848021/1641556
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- namespace DataTableDuplicateValuesCheck
- {
- public partial class Form1 : Form
- {
- public Form1()
- {
- InitializeComponent();
- }
- //used .net framework 4
- //add single textbox,datagridview and button into form.
- private void Form1_Load(object sender, EventArgs e)
- {
- DataTable dt = new DataTable();
- dt.Columns.Add("Item Code", typeof(string));
- dataGridView1.DataSource = dt;
- }
- private void button1_Click(object sender, EventArgs e)
- {
- bool isDuplicate = false;
- DataTable refTable = (DataTable)dataGridView1.DataSource;
- // What i needs to do,
- // to improve the performance by removing foreach with linq
- //I dont not it can be done or not or it can improve the performance by using linq.
- // your coments are welcome.....
- #region replace_this_code_with_linq
- foreach (DataRow r in refTable.Rows)
- {
- if (r["Item Code"].ToString() == textBox1.Text) {
- isDuplicate = true;
- }
- }
- #endregion
- if (!isDuplicate)
- {
- DataRow row = refTable.NewRow();
- row["Item Code"] = textBox1.Text;
- refTable.Rows.Add(row);
- dataGridView1.DataSource = refTable;
- }
- else {
- MessageBox.Show(string.Format("This item ({0}) already added!",textBox1.Text));
- }
- }
- }
- }
Add Comment
Please, Sign In to add comment