DataGridView CRUD operation with lookup combobox column

using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.OleDb; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace AdminApp { public partial class frmConversationType : Form { public frmConversationType() { InitializeComponent(); } private String connectionString = null; private OleDbConnection connection = null; private OleDbDataAdapter da = null; private OleDbCommandBuilder commandBuilder = null; private DataTable dataTable = null; private BindingSource bindingSource = null; private BindingSource bsCategory = null; private String selectQueryString = null; private void OnFormLoading(object sender, EventArgs e) { connectionString = GlobalVariables.ConnectionString;// ConfigurationManager.AppSettings[“connectionString”]; connection = new OleDbConnection(connectionString); selectQueryString = “SELECT * FROM ConversationType”; connection.Open(); da = new OleDbDataAdapter(selectQueryString, connection); commandBuilder = new OleDbCommandBuilder(da); dataTable = new DataTable(); da.Fill(dataTable); bindingSource = new BindingSource(); bindingSource.DataSource = dataTable; //first, set datasource to lookup combo column bsCategory = new BindingSource(); bsCategory.DataSource = CustomObjects.ConversationCategory.GetCategoryList(GlobalVariables.ConnectionString); DataGridViewComboBoxColumn colUserTypeId = (DataGridViewComboBoxColumn)dg.Columns[“ConversationCategoryId”]; colUserTypeId.DisplayMember = “ConversationCategoryName”; colUserTypeId.ValueMember = “ConversationCategoryId”; colUserTypeId.DataSource = bsCategory; dg.AutoGenerateColumns = false; dg.DataSource = bindingSource; //now, set datasource to datagridview dg.Columns[“ConversationTypeId”].DataPropertyName = “ConversationTypeId”; dg.Columns[“ConversationTypeName”].DataPropertyName = “ConversationTypeName”; dg.Columns[“ConversationCategoryId”].DataPropertyName = “ConversationCategoryId”; dg.Columns[“InformEhan”].DataPropertyName = “InformEhan”; } //Add & Update private void OnSaving(object sender, EventArgs e) { try { da.Update(dataTable); } catch (Exception exceptionObj) { MessageBox.Show(exceptionObj.Message.ToString()); } } private void OnDeleting(object sender, EventArgs e) { try { dg.Rows.RemoveAt(dg.CurrentRow.Index); da.Update(dataTable); } catch (Exception exceptionObj) { MessageBox.Show(exceptionObj.Message.ToString()); } } } }

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s