i have a CustomerNameComboBox, on the selectedIndexChanged event, i want to show the record of the selected customer in the datagridview of the selected customer.. up til that, i have done.. but i am not able to get the ID of the Customer.. i want to use the ID of the Customer above.. can i please get to know how can i get the id of the customer? I also want to set the visibility of my CustomerNameComboBox to read only once the customer is selected. here is my code that i have been doing so far:
private void CustomerNameComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
if (CustomerNameComboBox.Items.Count > 0)
{
SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Tyre Shop Management System;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select * from CustomerInfo where ID = " + CustomerNameComboBox.SelectedValue + "", cn);
MessageBox.Show(cmd.CommandText);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
dgCustomerDetails.DataSource = dt;
//if (CustomerNameComboBox.SelectedValue > 0)
//{
// ReadOnlyAttribute ReadOnly = new ReadOnlyAttribute(true);
// CustomerNameComboBox.Visible = ReadOnly.IsReadOnly;
//}
//if (CustomerNameComboBox.ValueMember == CustomerNameComboBox.SelectedValue)
//{
// ReadOnlyAttribute ReadOnly = new ReadOnlyAttr开发者_如何转开发ibute(true);
// CustomerNameComboBox.Visible = ReadOnly.IsReadOnly;
//}
}
}
private void BindCombo(string query, ComboBox combo, string DisplayMember, string ValueMember)
{
SqlConnection cn = new SqlConnection(@"Data Source=COMPAQ-PC-PC\SQLEXPRESS;Initial Catalog=Tyre Shop Management System;Integrated Security=True");
SqlCommand cmd = new SqlCommand(query, cn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
CustomerNameComboBox.DataSource = dt;
CustomerNameComboBox.DisplayMember = DisplayMember;
CustomerNameComboBox.ValueMember = ValueMember;
}
private void BindComboBoxAll()
{
string CustomerQuery = "Select ID,FirstName from CustomerInfo";
BindCombo(CustomerQuery, CustomerNameComboBox, "FirstName", "ID");
}
you need to call the bind method after setting datasoure
DataTable dt = new DataTable();
da.Fill(dt);
dgCustomerDetails.DataSource = dt;
dgCustomerDetails.DataBind();// Add this
精彩评论