Click here to Skip to main content
15,843,623 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
how to add a combobox in a datagridview which accepts input and also allow user to add new items if the items is not there in the combobox?
Posted

1 solution

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;
using System.Data.OleDb;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
int count = 0;
DataTable tblA = new DataTable();
List<object> lstArticles = new List<object>();
public Form2()
{
InitializeComponent();
}
private void dataGridViewEx1_EditingControlShowing(object sender, DataGridViewEditingControlShowingEventArgs e)
{
if (e.Control is DataGridViewComboBoxEditingControl)
{
((ComboBox)e.Control).DropDownStyle = ComboBoxStyle.DropDown;
((ComboBox)e.Control).AutoCompleteSource = AutoCompleteSource.ListItems;
((ComboBox)e.Control).AutoCompleteMode = System.Windows.Forms.AutoCompleteMode.SuggestAppend;
((ComboBox)e.Control).Validating -= Form2_Validating;
((ComboBox)e.Control).Validating += Form2_Validating;
}
}

void Form2_Validating(object sender, CancelEventArgs e)
{
DataGridViewComboBoxEditingControl cbo = sender as DataGridViewComboBoxEditingControl;
DataGridView grid = cbo.EditingControlDataGridView;
object value = cbo.Text;
DataGridViewComboBoxCell cboCell = (DataGridViewComboBoxCell)grid.CurrentCell;
if (!value.Equals(""))
{
if (cbo.Items.IndexOf(value) == -1)
{
if (DialogResult.Yes == MessageBox.Show("New ITEM", "Are you sure?", MessageBoxButtons.YesNo))
{
cbo.Items.Add(value);
Column2.Items.Add(value);
insertNewArticle(value.ToString());
grid.CurrentCell.Value = value;
}
else
{
SendKeys.Send("{LEFT}");
}
}
else
{
cboCell.Value = value.ToString();
}
}
else
{
SendKeys.Send("{LEFT}");
count++;
if (count == 2)
{
dataGridView1.Focus();
dataGridView1.Select();
}
}
}

private void Form2_Load(object sender, EventArgs e)
{
getArticles();
foreach (String item in lstArticles)
Column2.Items.Add(item);

}
void insertNewArticle(String value)
{
string strError = "";
string query = "insert into tblArticles(Article) values(@newArticle)";
List<oledbparameter> parameters = new List<oledbparameter>();
parameters.Add(new OleDbParameter("newArticle", value));
string result = PawnManagement.SQLHelper.RunCommand(query, parameters, ref strError);
if (result == "Done")
{
MessageBox.Show("new item successffullly added");
}
else
{
MessageBox.Show(result);
}
}

void getArticles()
{
String strError = "";
String query = "select Article from tblArticles";
tblA = PawnManagement.SQLHelper.GetDataTable(query, ref strError);
if (strError != "")
{
MessageBox.Show("Errorr in retrieving articles");
}
foreach (DataRow row in tblA.Rows)
{
lstArticles.Add(row["Article"].ToString());
}
}
}
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900