Using ComboBox with DataGridView is not that complex anymore but it’s
almost mandatory while doing some data driven software development.
I have created a DataGridView like this one. Now, I want to show “Month” and “Item” instead of “MonthID” and “ItemID” in DataGridView.
More Details :
I have created a DataGridView like this one. Now, I want to show “Month” and “Item” instead of “MonthID” and “ItemID” in DataGridView.
//Item Data Source
string selectQueryStringItem= "SELECT ItemID,ItemText FROM Table_Item";
SqlDataAdapter sqlDataAdapterItem = new SqlDataAdapter(selectQueryStringItem, sqlConnection);
SqlCommandBuilder sqlCommandBuilderItem = new SqlCommandBuilder(sqlDataAdapterItem);
DataTable dataTableItem = new DataTable();
sqlDataAdapterItem.Fill(dataTableItem);
BindingSource bindingSourceItem = new BindingSource();
bindingSourceItem.DataSource = dataTableItem;
//Month Data Source
string selectQueryStringMonth = "SELECT MonthID,MonthText FROM Table_Month";
SqlDataAdapter sqlDataAdapterMonth = new SqlDataAdapter(selectQueryStringMonth, sqlConnection);
SqlCommandBuilder sqlCommandBuilderMonth = new SqlCommandBuilder(sqlDataAdapterMonth);
DataTable dataTableMonth= new DataTable();
sqlDataAdapterMonth.Fill(dataTableMonth);
BindingSource bindingSourceMonth = new BindingSource();
bindingSourceMonth.DataSource = dataTableMonth;
Let’s start with preparing DataSource for “Item” and “Month” ComboBox.
//Adding Month Combo
DataGridViewComboBoxColumn ColumnMonth = new DataGridViewComboBoxColumn();
ColumnMonth.DataPropertyName = "MonthID";
ColumnMonth.HeaderText = "Month";
ColumnMonth.Width = 120;
ColumnMonth.DataSource = bindingSourceMonth;
ColumnMonth.ValueMember = "MonthID";
ColumnMonth.DisplayMember = "MonthText";
dataGridViewComboTrial.Columns.Add(ColumnMonth);
//Adding Year TextBox
DataGridViewTextBoxColumn ColumnYear = new DataGridViewTextBoxColumn();
ColumnYear.HeaderText = "Year";
ColumnYear.Width = 80;
ColumnYear.DataPropertyName = "Year";
dataGridViewComboTrial.Columns.Add(ColumnYear);
Set the “DataSource” and most importantly set the “DataPropertyName” accurately for “ColumnMonth” .
//Adding Item ComboBox
DataGridViewComboBoxColumn ColumnItem = new DataGridViewComboBoxColumn();
ColumnItem.DataPropertyName = "ItemID";
ColumnItem.HeaderText = "Item";
ColumnItem.Width = 120;
ColumnItem.DataSource = bindingSourceItem;
ColumnItem.ValueMember = "ItemID";
ColumnItem.DisplayMember = "ItemText";
dataGridViewComboTrial.Columns.Add(ColumnItem);
//Adding Year TextBox
DataGridViewTextBoxColumn ColumnSaleAmount = new DataGridViewTextBoxColumn();
ColumnSaleAmount.HeaderText = "Total Sale";
ColumnSaleAmount.Width = 120;
ColumnSaleAmount.DataPropertyName = "SaleAmount";
dataGridViewComboTrial.Columns.Add(ColumnSaleAmount);
//Adding Remarks TextBox
DataGridViewTextBoxColumn ColumnSaleRemarks = new DataGridViewTextBoxColumn();
ColumnSaleRemarks.HeaderText = "Remarks";
ColumnSaleRemarks.Width = 150;
ColumnSaleRemarks.DataPropertyName = "Remarks";
dataGridViewComboTrial.Columns.Add(ColumnSaleRemarks);
Set the “DataSource” and “DataPropertyName” for “ColumnItem” .
public partial class DataGridViewComboForm : Form
{
private String connectionString = null;
private SqlConnection sqlConnection = null;
private SqlDataAdapter sqlDataAdapter = null;
private SqlCommandBuilder sqlCommandBuilder = null;
private DataTable dataTable = null;
private BindingSource bindingSource = null;
private String selectQueryString = null;
public DataGridViewComboForm()
{
InitializeComponent();
}
private void DataGridViewComboForm_Load(object sender, EventArgs e)
{
connectionString = ConfigurationManager.AppSettings["connectionString"];
sqlConnection = new SqlConnection(connectionString);
sqlConnection.Open();
//DataGridView Source
selectQueryString = "SELECT * FROM Table_SaleSummary";
sqlDataAdapter = new SqlDataAdapter(selectQueryString, sqlConnection);
sqlCommandBuilder = new SqlCommandBuilder(sqlDataAdapter);
dataTable = new DataTable();
sqlDataAdapter.Fill(dataTable);
bindingSource = new BindingSource();
bindingSource.DataSource = dataTable;
Prepare DataSource For your DataGridView.
//Setting Data Source for DataGridView
dataGridViewComboTrial.DataSource = bindingSource;
Finally, set DataSource for the DataGridView and get your expected output the following one.
private void addUpadateButton_Click(object sender, EventArgs e)
{
try
{
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
private void deleteButton_Click(object sender, EventArgs e)
{
try
{
dataGridViewComboTrial.Rows.RemoveAt(dataGridViewComboTrial.CurrentRow.Index);
sqlDataAdapter.Update(dataTable);
}
catch (Exception exceptionObj)
{
MessageBox.Show(exceptionObj.Message.ToString());
}
}
More Details :
Live Training jaipur
No comments:
Post a Comment