TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Meena S
NA
48
4.3k
autofetch based on combobox selection in datagridview
May 8 2019 12:03 PM
I have 4 columns in datagrid.
sno Itemcode description uom in which itemcode is datagridcombobox
I want to fill datagrid other columns based on itemcode columncombobox selected value.
Am getting error in the below code and give me some idea to acheive this logic.
protected
void
bindvalues()
{
OleDbConnection con =
new
OleDbConnection(@
"Provider=SQLOLEDB;Data Source=DESKTOP\SQLEXPRESS;User ID=sa; Password = aaa; Initial Catalog=bbb; Integrated secutity=SSPI"
);
DataSet ds =
new
DataSet();
con.Open();
OleDbCommand cmd =
new
OleDbCommand((
"Select itemcode from Item"
), con);
OleDbDataAdapter da =
new
OleDbDataAdapter();
da.SelectCommand = cmd;
DataTable dt =
new
DataTable();
da.Fill(ds);
dataGridView1.DataSource = ds;
DataGridViewComboBoxColumn ic =
new
DataGridViewComboBoxColumn();
ic.HeaderText =
"ItemCode"
;
ic.Name =
"ItemCode"
;
ic.MaxDropDownItems = 6;
ic.DataSource = ds.Tables[0];
ic.DisplayMember =
"ItemCode"
;
dataGridView1.Columns.Insert(1, ic);
this
.dataGridView1.EditingControlShowing +=
new
DataGridViewEditingControlShowingEventHandler(dataGridView1_EditingControlShowing);
con.Close();
}
private
void
editGridCellComboBox_SelectedIndexChanged(
object
sender, EventArgs e)
{
if
(editGridCellComboBox.SelectedItem !=
null
)
{
ComboBox cb = (ComboBox)sender;
int
result = ((ComboBox)sender).SelectedIndex;
editGridCellComboBox.SelectedIndexChanged -= editGridCellComboBox_SelectedIndexChanged;
if
(cb.SelectedItem ==
null
)
return
;
//if (dataGridView1.Columns[dataGridView1.CurrentCell.ColumnIndex].Name = "Itemcode")
//{
// string Description = ((DataRowView)editGridCellComboBox.SelectedItem).Row["Itemcode"].ToString(); **not able to get selected value
//}
//DataRow row = (dataGridView1.CurrentRow.DataBoundItem as DataRowView).Row;
//String val = (String)(cb.SelectedItem as DataRowView).Row("description"); ***this logic also not working
OleDbConnection con =
new
OleDbConnection(@
"Provider=SQLOLEDB;Data Source=DESKTOP\SQLEXPRESS;User ID=sa; Password = aaa; Initial Catalog=bbb; Integrated secutity=SSPI"
);
DataSet ds =
new
DataSet();
OleDbCommand cmd1 =
new
OleDbCommand((
"Select t2.description from Item t1 INNER JOIN ItemGroup t2 ON t1.Itemcode = '"
+ cb.SelectedItem.ToString() +
"'"
), con);
OleDbDataAdapter da =
new
OleDbDataAdapter();
da.SelectCommand = cmd1;
da.Fill(ds);
dataGridView1.Columns[2].DataPropertyName =
"Itemdescription"
;
editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;
}
}
ComboBox editGridCellComboBox =
new
ComboBox();
private
void
dataGridView1_EditingControlShowing(
object
sender, DataGridViewEditingControlShowingEventArgs e)
{
editGridCellComboBox = (ComboBox)e.Control;
if
((editGridCellComboBox !=
null
))
{
editGridCellComboBox.SelectedIndexChanged += editGridCellComboBox_SelectedIndexChanged;
}
}
Reply
Answers (
1
)
Lambda expression vs LINQ Query Expression in c#
Touch screen vb.net