저는 C# Windows 양식 응용 프로그램에서 작업하고 있습니다. 콤보 상자에서 선택한 항목의 ID를 가져 오려고했습니다. 아래 코드는 내 코드입니다.Combobox 창에서 선택한 항목의 ID 가져 오기
private void ProductForm_Shown(object sender, EventArgs e)
{
SqlCeConnection Connection = new SqlCeConnection(ConString);
Connection.Open();
SqlCeDataAdapter da = new SqlCeDataAdapter("Select * from CastingMaterial", Connection);
DataTable dt = new DataTable();
da.Fill(dt);
for (int i = 0; i < dt.Rows.Count; i++)
{
ProductsComboBox.Items.Add(dt.Rows[i]["PartName"]);
}
ProductsComboBox.DisplayMember = "PartName";
ProductsComboBox.ValueMember = "PartId";
Connection.Close();
}
private void ProductsComboBox_SelectedIndexChanged(object sender, EventArgs e)
{
int ProductIndex = ProductsComboBox.SelectedIndex;
string productName = ProductsComboBox.Text.ToString();
int ProductId =Convert.ToInt32(ProductsComboBox.SelectedValue);
SqlCeConnection Connection = new SqlCeConnection(ConString);
Connection.Open();
String Query = "SELECT * From CastingMaterial where [email protected]";
SqlCeDataAdapter da = new SqlCeDataAdapter(Query, Connection);
da.SelectCommand.Parameters.AddWithValue("PartId", ProductId);
DataSet ds = new DataSet();
SqlCeCommandBuilder commandBuilder = new SqlCeCommandBuilder(da);
BindingSource bsource = new BindingSource();
da.Fill(ds, "CastingMaterial");
bsource.DataSource = ds.Tables["CastingMaterial"];
Productgv.DataSource = bsource;
Connection.Close();
}
모든 도움을 주시면 감사하겠습니다.